debian-mirror-gitlab/scripts/frontend/stylelint/stylelint-utils.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
const md5 = require('md5');
2021-04-17 20:07:23 +05:30
const stylelint = require('stylelint');
2019-07-07 11:18:12 +05:30
module.exports.createPropertiesHashmap = (
ruleRoot,
result,
ruleName,
messages,
selectorGroups,
addSelectors,
) => {
2021-03-08 18:12:59 +05:30
ruleRoot.walkRules((rule) => {
2019-07-07 11:18:12 +05:30
const selector = rule.selector.replace(/(?:\r\n|\r|\n)/g, ' ');
if (
rule &&
rule.parent &&
2021-04-17 20:07:23 +05:30
rule.parent.type !== 'atrule' &&
2019-07-07 11:18:12 +05:30
!(
selector.includes('-webkit-') ||
selector.includes('-moz-') ||
selector.includes('-o-') ||
selector.includes('-ms-') ||
selector.includes(':')
)
) {
let cssArray = [];
2021-04-17 20:07:23 +05:30
rule.nodes.forEach((property) => {
2019-07-07 11:18:12 +05:30
const { prop, value } = property;
if (property && value) {
const propval = `${prop}${value}${property.important ? '!important' : ''}`;
cssArray.push(propval);
}
});
cssArray = cssArray.sort();
const cssContent = cssArray.toString();
if (cssContent) {
const hashValue = md5(cssContent);
const selObj = selectorGroups[hashValue];
const selectorLine = `${selector} (${
2021-04-17 20:07:23 +05:30
rule.source.input.file ? `${rule.source.input.file} -` : ''
2019-07-07 11:18:12 +05:30
}${rule.source.start.line}:${rule.source.start.column})`;
if (selObj) {
2021-04-17 20:07:23 +05:30
if (selectorGroups[hashValue].selectors.indexOf(selector) === -1) {
2019-07-07 11:18:12 +05:30
let lastSelector =
selectorGroups[hashValue].selectors[selectorGroups[hashValue].selectors.length - 1];
// So we have nicer formatting if it is the same file, we remove the filename
lastSelector = lastSelector.replace(`${rule.source.input.file} - `, '');
if (messages) {
stylelint.utils.report({
result,
ruleName,
message: messages.expected(selector, lastSelector),
node: rule,
word: rule.node,
});
}
if (addSelectors) {
selectorGroups[hashValue].selectors.push(selectorLine);
}
}
} else if (addSelectors) {
2021-04-17 20:07:23 +05:30
// eslint-disable-next-line no-param-reassign
2019-07-07 11:18:12 +05:30
selectorGroups[hashValue] = {
selectors: [selectorLine],
};
}
}
}
});
};