Skip to content

Commit 38b365c

Browse files
crisbetojelbourn
authored andcommitted
build: add stylelint rule to prevent nested mixins (#5354)
Adds a Stylelint rule that will prevent uses of nested mixins. This will help prevent issues like #5232 in the future.
1 parent 6dcacd2 commit 38b365c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

stylelint-config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"plugins": [
33
"./tools/stylelint/no-prefixes/no-prefixes.js",
44
"./tools/stylelint/selector-nested-pattern-scoped/index.js",
5-
"./tools/stylelint/selector-no-deep/index.js"
5+
"./tools/stylelint/selector-no-deep/index.js",
6+
"./tools/stylelint/no-nested-mixin/index.js"
67
],
78
"rules": {
89
"material/no-prefixes": [["last 2 versions", "not ie <= 10", "not ie_mob <= 10"]],
910
"material/selector-no-deep": true,
11+
"material/no-nested-mixin": true,
1012
"material/selector-nested-pattern-scoped": [".*[^&]$", {
1113
"message": "The & operator is not allowed at the end of theme selectors.",
1214
"filePattern": "-theme\\.scss$"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const stylelint = require('stylelint');
2+
3+
const ruleName = 'material/no-nested-mixin';
4+
const messages = stylelint.utils.ruleMessages(ruleName, {
5+
expected: () => 'Nested mixins are not allowed.',
6+
});
7+
8+
9+
/**
10+
* Stylelint plugin that prevents nesting SASS mixins.
11+
*/
12+
const plugin = stylelint.createPlugin(ruleName, isEnabled => {
13+
return (root, result) => {
14+
if (!isEnabled) return;
15+
16+
root.walkAtRules(rule => {
17+
if (rule.name !== 'mixin') return;
18+
19+
rule.walkAtRules(childRule => {
20+
if (childRule.name !== 'mixin') return;
21+
22+
stylelint.utils.report({
23+
result,
24+
ruleName,
25+
message: messages.expected(),
26+
node: childRule
27+
});
28+
});
29+
});
30+
};
31+
});
32+
33+
plugin.ruleName = ruleName;
34+
plugin.messages = messages;
35+
module.exports = plugin;

0 commit comments

Comments
 (0)