UNPKG

2.29 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 (
15 rule.selector.indexOf("[") === -1 ||
16 rule.selector.indexOf("=") === -1
17 ) {
18 return;
19 }
20
21 let hasFixed = false;
22 const selector = rule.raws.selector
23 ? rule.raws.selector.raw
24 : rule.selector;
25
26 const fixedSelector = parseSelector(
27 selector,
28 options.result,
29 rule,
30 selectorTree => {
31 selectorTree.walkAttributes(attributeNode => {
32 const operator = attributeNode.operator;
33
34 if (!operator) {
35 return;
36 }
37
38 const attributeNodeString = attributeNode.toString();
39
40 styleSearch(
41 { source: attributeNodeString, target: operator },
42 match => {
43 const index = options.checkBeforeOperator
44 ? match.startIndex
45 : match.endIndex - 1;
46
47 checkOperator(
48 attributeNodeString,
49 index,
50 rule,
51 attributeNode,
52 operator
53 );
54 }
55 );
56 });
57 }
58 );
59
60 if (hasFixed) {
61 if (!rule.raws.selector) {
62 rule.selector = fixedSelector;
63 } else {
64 rule.raws.selector.raw = fixedSelector;
65 }
66 }
67
68 function checkOperator(source, index, node, attributeNode, operator) {
69 options.locationChecker({
70 source,
71 index,
72 err: m => {
73 if (options.fix && options.fix(attributeNode)) {
74 hasFixed = true;
75
76 return;
77 }
78
79 report({
80 message: m.replace(
81 options.checkBeforeOperator
82 ? operator[0]
83 : operator[operator.length - 1],
84 operator
85 ),
86 node,
87 index: attributeNode.sourceIndex + index,
88 result: options.result,
89 ruleName: options.checkedRuleName
90 });
91 }
92 });
93 }
94 });
95};