@mrhenry/stylelint-mrhenry-nesting 2.2.3 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
import selectorParser from 'postcss-selector-parser';
|
|
3
3
|
|
|
4
4
|
const ruleName = "@mrhenry/stylelint-mrhenry-nesting";
|
|
5
5
|
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
@@ -25,13 +25,29 @@ const meta = {
|
|
|
25
25
|
fixable: true,
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
/** @type {import('stylelint').Rule<true|null, Array<string | RegExp>>} */
|
|
29
|
+
const ruleFunction = (primaryOption, secondaryOption, context) => {
|
|
29
30
|
return (postcssRoot, postcssResult) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const validPrimary = stylelint.utils.validateOptions(postcssResult, ruleName, {
|
|
32
|
+
actual: primaryOption,
|
|
33
|
+
possible: [true]
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/* c8 ignore next */
|
|
37
|
+
if (!validPrimary) return;
|
|
38
|
+
|
|
39
|
+
const validSecondary = stylelint.utils.validateOptions(postcssResult, ruleName, {
|
|
40
|
+
actual: secondaryOption,
|
|
41
|
+
possible: {
|
|
42
|
+
ignoreAtRules: [isString, isRegExp],
|
|
43
|
+
},
|
|
44
|
+
optional: true,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/* c8 ignore next */
|
|
48
|
+
if (!validSecondary) return;
|
|
33
49
|
|
|
34
|
-
const ignoreAtRulesOptions =
|
|
50
|
+
const ignoreAtRulesOptions = secondaryOption?.ignoreAtRules ?? [];
|
|
35
51
|
|
|
36
52
|
postcssRoot.walkAtRules((atrule) => {
|
|
37
53
|
let name = atrule.name.toLowerCase();
|
|
@@ -39,7 +55,8 @@ const ruleFunction = (primaryOption, secondaryOptionObject, context) => {
|
|
|
39
55
|
name === 'media' ||
|
|
40
56
|
name === 'supports' ||
|
|
41
57
|
name === 'container' ||
|
|
42
|
-
name === 'scope'
|
|
58
|
+
name === 'scope' ||
|
|
59
|
+
name === 'starting-style'
|
|
43
60
|
) {
|
|
44
61
|
// always allowed
|
|
45
62
|
return;
|
|
@@ -119,7 +136,7 @@ const ruleFunction = (primaryOption, secondaryOptionObject, context) => {
|
|
|
119
136
|
const selectorsAST = selectorParser().astSync(rule.selector);
|
|
120
137
|
const firstCompoundOrSelf = getFirstCompoundOrSelf(selectorsAST);
|
|
121
138
|
|
|
122
|
-
isRelativeSelector = false;
|
|
139
|
+
let isRelativeSelector = false;
|
|
123
140
|
if (firstCompoundOrSelf.nodes[0]?.type === 'combinator') {
|
|
124
141
|
isRelativeSelector = true;
|
|
125
142
|
}
|
|
@@ -172,7 +189,7 @@ const ruleFunction = (primaryOption, secondaryOptionObject, context) => {
|
|
|
172
189
|
// .foo { .bar {} }
|
|
173
190
|
if (selectorAST.nodes[0]?.type !== 'nesting') {
|
|
174
191
|
if (context.fix) {
|
|
175
|
-
fixSelector(rule, selectorsAST, selectorAST);
|
|
192
|
+
fixSelector(rule, selectorsAST, selectorAST, isRelativeSelector);
|
|
176
193
|
continue;
|
|
177
194
|
}
|
|
178
195
|
|
|
@@ -242,7 +259,7 @@ const ruleFunction = (primaryOption, secondaryOptionObject, context) => {
|
|
|
242
259
|
};
|
|
243
260
|
};
|
|
244
261
|
|
|
245
|
-
function fixSelector(rule, selectorsAST, selectorAST) {
|
|
262
|
+
function fixSelector(rule, selectorsAST, selectorAST, isRelativeSelector) {
|
|
246
263
|
if (
|
|
247
264
|
selectorAST.nodes.length === 1 &&
|
|
248
265
|
selectorAST.nodes[0].type === 'pseudo'
|
|
@@ -294,19 +311,32 @@ ruleFunction.ruleName = ruleName;
|
|
|
294
311
|
ruleFunction.messages = messages;
|
|
295
312
|
ruleFunction.meta = meta;
|
|
296
313
|
|
|
297
|
-
|
|
314
|
+
export default stylelint.createPlugin(ruleName, ruleFunction);
|
|
298
315
|
|
|
299
316
|
function getFirstCompoundOrSelf(x) {
|
|
317
|
+
/* c8 ignore next */
|
|
300
318
|
if (!x.nodes) {
|
|
319
|
+
/* c8 ignore next */
|
|
301
320
|
return x;
|
|
321
|
+
/* c8 ignore next */
|
|
302
322
|
}
|
|
303
323
|
|
|
304
324
|
for (let i = 0; i < x.nodes.length; i++) {
|
|
305
325
|
/* c8 ignore next */
|
|
306
326
|
if (x.nodes[i].type !== 'selector') {
|
|
327
|
+
/* c8 ignore next */
|
|
307
328
|
return x;
|
|
329
|
+
/* c8 ignore next */
|
|
308
330
|
}
|
|
309
331
|
}
|
|
310
332
|
|
|
311
333
|
return x.nodes[0];
|
|
312
334
|
}
|
|
335
|
+
|
|
336
|
+
function isRegExp(value) {
|
|
337
|
+
return value instanceof RegExp;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function isString(value) {
|
|
341
|
+
return typeof value === 'string' || value instanceof String;
|
|
342
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrhenry/stylelint-mrhenry-nesting",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Mr. Henry's preferred way of writing nested CSS",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "index.mjs",
|
|
9
10
|
"scripts": {
|
|
10
|
-
"test": "
|
|
11
|
-
"test:jest": "c8 --100 jest"
|
|
11
|
+
"test": "c8 --100 node --test"
|
|
12
12
|
},
|
|
13
13
|
"author": "Mr. Henry",
|
|
14
14
|
"license": "MIT",
|
|
@@ -19,13 +19,10 @@
|
|
|
19
19
|
"directory": "packages/nesting"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
|
-
"
|
|
22
|
+
"index.mjs",
|
|
23
23
|
"LICENSE",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
-
"jest": {
|
|
27
|
-
"preset": "jest-preset-stylelint"
|
|
28
|
-
},
|
|
29
26
|
"keywords": [
|
|
30
27
|
"stylelint",
|
|
31
28
|
"stylelint-plugin"
|
|
@@ -34,9 +31,9 @@
|
|
|
34
31
|
"postcss-selector-parser": "^6.0.11"
|
|
35
32
|
},
|
|
36
33
|
"peerDependencies": {
|
|
37
|
-
"stylelint": "^
|
|
34
|
+
"stylelint": "^16.0.0"
|
|
38
35
|
},
|
|
39
36
|
"devDependencies": {
|
|
40
|
-
"stylelint": "^
|
|
37
|
+
"stylelint": "^16.0.2"
|
|
41
38
|
}
|
|
42
39
|
}
|