Skip to content

Commit 8804caf

Browse files
crisbetommalerba
authored andcommitted
build: add stylelint rule to prevent usage of /deep/ (#4841)
* build: add stylelint rule to prevent usage of /deep/ Adds a custom Stylelint rule to prevent usages of `/deep/` inside selectors. Relates #4726. * fix: address feedback
1 parent 964033c commit 8804caf

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

stylelint-config.json

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

0 commit comments

Comments
 (0)