forked from stylelint/stylelint
/
selectorAttributeOperatorSpaceChecker.js
73 lines (60 loc) · 1.89 KB
/
selectorAttributeOperatorSpaceChecker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const isStandardSyntaxRule = require('../utils/isStandardSyntaxRule');
const isStandardSyntaxSelector = require('../utils/isStandardSyntaxSelector');
const parseSelector = require('../utils/parseSelector');
const report = require('../utils/report');
const styleSearch = require('style-search');
module.exports = function(options) {
options.root.walkRules((rule) => {
if (!isStandardSyntaxRule(rule) || !isStandardSyntaxSelector(rule.selector)) {
return;
}
if (!rule.selector.includes('[') || !rule.selector.includes('=')) {
return;
}
let hasFixed = false;
const selector = rule.raws.selector ? rule.raws.selector.raw : rule.selector;
const fixedSelector = parseSelector(selector, options.result, rule, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
const operator = attributeNode.operator;
if (!operator) {
return;
}
const attributeNodeString = attributeNode.toString();
styleSearch({ source: attributeNodeString, target: operator }, (match) => {
const index = options.checkBeforeOperator ? match.startIndex : match.endIndex - 1;
checkOperator(attributeNodeString, index, rule, attributeNode, operator);
});
});
});
if (hasFixed) {
if (!rule.raws.selector) {
rule.selector = fixedSelector;
} else {
rule.raws.selector.raw = fixedSelector;
}
}
function checkOperator(source, index, node, attributeNode, operator) {
options.locationChecker({
source,
index,
err: (m) => {
if (options.fix && options.fix(attributeNode)) {
hasFixed = true;
return;
}
report({
message: m.replace(
options.checkBeforeOperator ? operator[0] : operator[operator.length - 1],
operator,
),
node,
index: attributeNode.sourceIndex + index,
result: options.result,
ruleName: options.checkedRuleName,
});
},
});
}
});
};