-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.mjs
147 lines (120 loc) · 3.65 KB
/
index.mjs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { compare, selectorSpecificity } from '@csstools/selector-specificity';
import selectorParser from 'postcss-selector-parser';
import { assertNumber, isRegExp, isString } from '../../utils/validateTypes.mjs';
import isStandardSyntaxRule from '../../utils/isStandardSyntaxRule.mjs';
import isStandardSyntaxSelector from '../../utils/isStandardSyntaxSelector.mjs';
import optionsMatches from '../../utils/optionsMatches.mjs';
import report from '../../utils/report.mjs';
import resolveNestedSelectorsForRule from '../../utils/resolveNestedSelectorsForRule.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
import validateOptions from '../../utils/validateOptions.mjs';
const ruleName = 'selector-max-specificity';
const messages = ruleMessages(ruleName, {
expected: (selector, max) => `Expected "${selector}" to have a specificity no more than "${max}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/selector-max-specificity',
};
/** @typedef {import('@csstools/selector-specificity').Specificity} Specificity */
/**
* @param {import('postcss-selector-parser').Selector} selector
* @param {(selector: string) => boolean} isIgnored
* @returns {import('postcss-selector-parser').Selector}
*/
function removeIgnoredNodes(selector, isIgnored) {
selector.walk((node) => {
let value = '';
switch (node.type) {
case 'attribute':
case 'class':
case 'id':
case 'tag':
value = node.toString();
break;
case 'pseudo':
value = node.value;
break;
default:
return;
}
if (!value) {
return;
}
if (isIgnored(value)) {
node.replaceWith(
selectorParser.universal({
value: '*',
sourceIndex: node.sourceIndex,
source: node.source,
}),
);
}
});
return selector;
}
/** @type {import('stylelint').Rule<string>} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: [
// Check that the max specificity is in the form "a,b,c"
(spec) => isString(spec) && /^\d+,\d+,\d+$/.test(spec),
],
},
{
actual: secondaryOptions,
possible: {
ignoreSelectors: [isString, isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
/** @type {(selector: string) => boolean} */
const isSelectorIgnored = (selector) => {
return optionsMatches(secondaryOptions, 'ignoreSelectors', selector);
};
const [a, b, c] = primary.split(',').map((s) => Number.parseFloat(s));
assertNumber(a);
assertNumber(b);
assertNumber(c);
const maxSpecificity = { a, b, c };
root.walkRules((ruleNode) => {
if (!isStandardSyntaxRule(ruleNode)) {
return;
}
const selectorList = resolveNestedSelectorsForRule(ruleNode, result);
if (!selectorList) return;
selectorList.forEach(({ selector, resolvedSelectors }) => {
const selectorStr = selector.toString();
resolvedSelectors.forEach((resolvedSelector) => {
resolvedSelector = removeIgnoredNodes(resolvedSelector, isSelectorIgnored);
if (!isStandardSyntaxSelector(resolvedSelector.toString())) return;
// Check if the selector specificity exceeds the allowed maximum
if (compare(selectorSpecificity(resolvedSelector), maxSpecificity) > 0) {
const index = selector.first?.sourceIndex ?? 0;
report({
ruleName,
result,
node: ruleNode,
index,
endIndex: index + selectorStr.length,
message: messages.expected,
messageArgs: [selectorStr.trim(), primary],
});
}
});
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
export default rule;