-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const stylelint = require('stylelint'); | ||
const isStandardSyntaxRule = require('stylelint/lib/utils/isStandardSyntaxRule'); | ||
const isStandardSyntaxSelector = require('stylelint/lib/utils/isStandardSyntaxSelector'); | ||
|
||
const ruleName = 'material/selector-no-deep'; | ||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
expected: selector => `Usage of the /deep/ in "${selector}" is not allowed`, | ||
}); | ||
|
||
|
||
/** | ||
* Stylelint plugin that prevents uses of /deep/ in selectors. | ||
*/ | ||
const plugin = stylelint.createPlugin(ruleName, isEnabled => { | ||
return (root, result) => { | ||
if (!isEnabled) return; | ||
|
||
root.walkRules(rule => { | ||
if (rule.parent.type === 'rule' && | ||
isStandardSyntaxRule(rule) && | ||
isStandardSyntaxSelector(rule.selector) && | ||
rule.selector.includes('/deep/')) { | ||
|
||
stylelint.utils.report({ | ||
result, | ||
ruleName, | ||
message: messages.expected(rule.selector), | ||
node: rule | ||
}); | ||
} | ||
}); | ||
}; | ||
}); | ||
|
||
plugin.ruleName = ruleName; | ||
plugin.messages = messages; | ||
module.exports = plugin; |