forked from stylelint/stylelint
/
index.js
160 lines (134 loc) · 4.14 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
'use strict';
const hasBlock = require('../../utils/hasBlock');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const optionsMatches = require('../../utils/optionsMatches');
const parser = require('postcss-selector-parser');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isAtRule, isDeclaration, isRoot, isRule } = require('../../utils/typeGuards');
const { isNumber, isRegExp, isString } = require('../../utils/validateTypes');
const ruleName = 'max-nesting-depth';
const messages = ruleMessages(ruleName, {
expected: (depth) => `Expected nesting depth to be no more than ${depth}`,
});
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
/**
* @param {import('postcss').Node} node
*/
const isIgnoreAtRule = (node) =>
isAtRule(node) && optionsMatches(secondaryOptions, 'ignoreAtRules', node.name);
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: [isNumber],
},
{
optional: true,
actual: secondaryOptions,
possible: {
ignore: ['blockless-at-rules', 'pseudo-classes'],
ignoreAtRules: [isString, isRegExp],
ignorePseudoClasses: [isString, isRegExp],
},
},
);
if (!validOptions) return;
root.walkRules(checkStatement);
root.walkAtRules(checkStatement);
/**
* @param {import('postcss').Rule | import('postcss').AtRule} statement
*/
function checkStatement(statement) {
if (isIgnoreAtRule(statement)) {
return;
}
if (!hasBlock(statement)) {
return;
}
if (isRule(statement) && !isStandardSyntaxRule(statement)) {
return;
}
const depth = nestingDepth(statement, 0);
if (depth > primary) {
report({
ruleName,
result,
node: statement,
message: messages.expected(primary),
});
}
}
};
/**
* @param {import('postcss').Node} node
* @param {number} level
* @returns {number}
*/
function nestingDepth(node, level) {
const parent = node.parent;
if (parent == null) {
throw new Error('The parent node must exist');
}
if (isIgnoreAtRule(parent)) {
return 0;
}
// The nesting depth level's computation has finished
// when this function, recursively called, receives
// a node that is not nested -- a direct child of the
// root node
if (isRoot(parent) || (isAtRule(parent) && parent.parent && isRoot(parent.parent))) {
return level;
}
/**
* @param {string} selector
*/
function containsPseudoClassesOnly(selector) {
const normalized = parser().processSync(selector, { lossless: false });
const selectors = normalized.split(',');
return selectors.every((sel) => extractPseudoRule(sel));
}
/**
* @param {string[]} selectors
* @returns {boolean}
*/
function containsIgnoredPseudoClassesOnly(selectors) {
if (!(secondaryOptions && secondaryOptions.ignorePseudoClasses)) return false;
return selectors.every((selector) => {
const pseudoRule = extractPseudoRule(selector);
if (!pseudoRule) return false;
return optionsMatches(secondaryOptions, 'ignorePseudoClasses', pseudoRule);
});
}
if (
(optionsMatches(secondaryOptions, 'ignore', 'blockless-at-rules') &&
isAtRule(node) &&
node.every((child) => !isDeclaration(child))) ||
(optionsMatches(secondaryOptions, 'ignore', 'pseudo-classes') &&
isRule(node) &&
containsPseudoClassesOnly(node.selector)) ||
(isRule(node) && containsIgnoredPseudoClassesOnly(node.selectors))
) {
return nestingDepth(parent, level);
}
// Unless any of the conditions above apply, we want to
// add 1 to the nesting depth level and then check the parent,
// continuing to add and move up the hierarchy
// until we hit the root node
return nestingDepth(parent, level + 1);
}
};
/**
* @param {string} selector
* @returns {string | undefined}
*/
function extractPseudoRule(selector) {
return selector.startsWith('&:') && selector[2] !== ':' ? selector.substr(2) : undefined;
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;