Skip to content

build: add stylelint rule to prevent usage of /deep/ #4841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stylelint-config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"plugins": [
"./tools/stylelint/no-prefixes/no-prefixes.js",
"./tools/stylelint/selector-nested-pattern-scoped/index.js"
"./tools/stylelint/selector-nested-pattern-scoped/index.js",
"./tools/stylelint/selector-no-deep/index.js"
],
"rules": {
"material/no-prefixes": [["last 2 versions", "not ie <= 10", "not ie_mob <= 10"]],
"material/selector-no-deep": true,
"material/selector-nested-pattern-scoped": [".*[^&]$", {
"message": "The & operator is not allowed at the end of theme selectors.",
"filePattern": "-theme\\.scss$"
Expand Down
37 changes: 37 additions & 0 deletions tools/stylelint/selector-no-deep/index.js
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;