UNPKG

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