UNPKG

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