Skip to content
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
5 changes: 2 additions & 3 deletions stylelint-config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"plugins": [
"./tools/stylelint/no-prefixes/no-prefixes.js",
"./tools/stylelint/selector-nested-pattern-scoped/index.js",
"./tools/stylelint/no-ampersand-beyond-selector-start/index.js",
"./tools/stylelint/selector-no-deep/index.js",
"./tools/stylelint/no-nested-mixin/index.js",
"./tools/stylelint/no-concrete-rules/index.js",
Expand All @@ -11,8 +11,7 @@
"material/no-prefixes": [["last 2 versions", "not ie <= 10", "not ie_mob <= 10"]],
"material/selector-no-deep": true,
"material/no-nested-mixin": true,
"material/selector-nested-pattern-scoped": [".*[^&]$", {
"message": "The & operator is not allowed at the end of theme selectors.",
"material/no-ampersand-beyond-selector-start": [true, {
"filePattern": "-theme\\.scss$"
}],
"material/no-concrete-rules": [true, {
Expand Down
66 changes: 66 additions & 0 deletions tools/stylelint/no-ampersand-beyond-selector-start/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const stylelint = require('stylelint');
const path = require('path');
const isStandardSyntaxRule = require('stylelint/lib/utils/isStandardSyntaxRule');
const isStandardSyntaxSelector = require('stylelint/lib/utils/isStandardSyntaxSelector');

const ruleName = 'material/no-ampersand-beyond-selector-start';
const messages = stylelint.utils.ruleMessages(ruleName, {
expected: () => 'Ampersand is only allowed at the beginning of a selector',
});

/**
* Stylelint rule that doesn't allow for an ampersand to be used anywhere
* except at the start of a selector. Skips private mixins.
*
* Based off the `selector-nested-pattern` Stylelint rule.
* Source: https://github.com/stylelint/stylelint/blob/master/lib/rules/selector-nested-pattern/
*/
const plugin = stylelint.createPlugin(ruleName, (isEnabled, options) => {
return (root, result) => {
if (!isEnabled) return;

const filePattern = new RegExp(options.filePattern);
const fileName = path.basename(root.source.input.file);

if (!filePattern.test(fileName)) return;

root.walkRules(rule => {
if (
rule.parent.type === 'rule' &&
isStandardSyntaxRule(rule) &&
isStandardSyntaxSelector(rule.selector) &&
// Using the ampersand at the beginning is fine, anything else can cause issues in themes.
rule.selector.indexOf('&') > 0) {

const mixinName = getClosestMixinName(rule);

// Skip rules inside private mixins.
if (!mixinName || !mixinName.startsWith('_')) {
stylelint.utils.report({
result,
ruleName,
message: messages.expected(),
node: rule
});
}
}
});
};

/** Walks up the AST and finds the name of the closest mixin. */
function getClosestMixinName(node) {
let parent = node.parent;

while (parent) {
if (parent.type === 'atrule' && parent.name === 'mixin') {
return parent.params;
}

parent = parent.parent;
}
}
});

plugin.ruleName = ruleName;
plugin.messages = messages;
module.exports = plugin;
46 changes: 0 additions & 46 deletions tools/stylelint/selector-nested-pattern-scoped/index.js

This file was deleted.