-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
168 lines (135 loc) · 4.61 KB
/
index.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const resolvedNestedSelector = require('postcss-resolve-nested-selector');
const { selectorSpecificity: calculate, compare } = require('@csstools/selector-specificity');
const findAtRuleContext = require('../../utils/findAtRuleContext');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const isStandardSyntaxSelector = require('../../utils/isStandardSyntaxSelector');
const { pseudoElements } = require('../../reference/selectors');
const nodeContextLookup = require('../../utils/nodeContextLookup');
const optionsMatches = require('../../utils/optionsMatches');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const ruleName = 'no-descending-specificity';
const messages = ruleMessages(ruleName, {
rejected: (b, a) => `Expected selector "${b}" to come before selector "${a}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/no-descending-specificity',
};
/** @typedef {{ selector: string, specificity: import('@csstools/selector-specificity').Specificity }} Entry */
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
},
{
optional: true,
actual: secondaryOptions,
possible: {
ignore: ['selectors-within-list'],
},
},
);
if (!validOptions) {
return;
}
const ignoreSelectorsWithinList = optionsMatches(
secondaryOptions,
'ignore',
'selectors-within-list',
);
const selectorContextLookup = nodeContextLookup();
root.walkRules((ruleNode) => {
// Ignore nested property `foo: {};`
if (!isStandardSyntaxRule(ruleNode)) {
return;
}
const selectors = ruleNode.selectors;
// Ignores selectors within list of selectors
if (ignoreSelectorsWithinList && selectors.length > 1) {
return;
}
/** @type {Map<string, Entry[]>} */
const comparisonContext = selectorContextLookup.getContext(
ruleNode,
findAtRuleContext(ruleNode),
);
for (const selector of selectors) {
// Ignore `.selector, { }`
if (selector.trim() === '') {
continue;
}
// Resolve any nested selectors before checking
for (const resolvedSelector of resolvedNestedSelector(selector, ruleNode)) {
if (!isStandardSyntaxSelector(resolvedSelector)) {
continue;
}
parseSelector(resolvedSelector, result, ruleNode, (s) => {
checkSelector(resolvedSelector, s, ruleNode, comparisonContext);
});
}
}
});
/**
* @param {string} selector
* @param {import('postcss-selector-parser').Root} selectorNode
* @param {import('postcss').Rule} ruleNode
* @param {Map<string, Entry[]>} comparisonContext
*/
function checkSelector(selector, selectorNode, ruleNode, comparisonContext) {
const referenceSelector = lastCompoundSelectorWithoutPseudoClasses(selectorNode);
if (!referenceSelector) return;
const selectorSpecificity = calculate(selectorNode);
const entry = { selector, specificity: selectorSpecificity };
const priorComparableSelectors = comparisonContext.get(referenceSelector);
if (!priorComparableSelectors) {
comparisonContext.set(referenceSelector, [entry]);
return;
}
for (const priorEntry of priorComparableSelectors) {
if (compare(selectorSpecificity, priorEntry.specificity) < 0) {
report({
ruleName,
result,
node: ruleNode,
message: messages.rejected,
messageArgs: [selector, priorEntry.selector],
word: selector,
});
break;
}
}
priorComparableSelectors.push(entry);
}
};
};
/**
* @param {import('postcss-selector-parser').Root} selectorNode
* @returns {string | undefined}
*/
function lastCompoundSelectorWithoutPseudoClasses(selectorNode) {
const firstChild = selectorNode.nodes[0];
if (!firstChild) return undefined;
const nodesByCombinator = firstChild.split((node) => node.type === 'combinator');
const nodesAfterLastCombinator = nodesByCombinator[nodesByCombinator.length - 1];
if (!nodesAfterLastCombinator) return undefined;
const nodesWithoutPseudoClasses = nodesAfterLastCombinator.filter((node) => {
return (
node.type !== 'pseudo' ||
node.value.startsWith('::') ||
pseudoElements.has(node.value.replace(/:/g, ''))
);
});
if (nodesWithoutPseudoClasses.length === 0) return undefined;
return nodesWithoutPseudoClasses.join('');
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;