Skip to content

build: add stylelint rule to avoid top-level ampersands in themes #10280

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 1 commit into from
Mar 6, 2018
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
8 changes: 4 additions & 4 deletions src/lib/radio/_radio-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@import '../core/theming/theming';
@import '../core/typography/typography-utils';

@mixin mat-radio-color($palette) {
@mixin _mat-radio-color($palette) {
&.mat-radio-checked .mat-radio-outer-circle {
border-color: mat-color($palette);
}
Expand Down Expand Up @@ -43,15 +43,15 @@

.mat-radio-button {
&.mat-primary {
@include mat-radio-color($primary);
@include _mat-radio-color($primary);
}

&.mat-accent {
@include mat-radio-color($accent);
@include _mat-radio-color($accent);
}

&.mat-warn {
@include mat-radio-color($warn);
@include _mat-radio-color($warn);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion stylelint-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"./tools/stylelint/selector-nested-pattern-scoped/index.js",
"./tools/stylelint/selector-no-deep/index.js",
"./tools/stylelint/no-nested-mixin/index.js",
"./tools/stylelint/no-concrete-rules/index.js"
"./tools/stylelint/no-concrete-rules/index.js",
"./tools/stylelint/no-top-level-ampersand-in-mixin/index.js"
],
"rules": {
"material/no-prefixes": [["last 2 versions", "not ie <= 10", "not ie_mob <= 10"]],
Expand All @@ -17,6 +18,9 @@
"material/no-concrete-rules": [true, {
"filePattern": "^_.*\\.scss$"
}],
"material/no-top-level-ampersand-in-mixin": [true, {
"filePattern": "-theme\\.scss$"
}],

"color-hex-case": "lower",
"color-no-invalid-hex": true,
Expand Down
44 changes: 44 additions & 0 deletions tools/stylelint/no-top-level-ampersand-in-mixin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const stylelint = require('stylelint');
const path = require('path');
const ruleName = 'material/no-top-level-ampersand-in-mixin';
const messages = stylelint.utils.ruleMessages(ruleName, {
expected: () => `Selectors starting with an ampersand ` +
`are not allowed inside top-level mixin rules`
});

/**
* Stylelint rule that doesn't allow for the top-level rules inside a
* mixin to start with an ampersand. Does not apply to internal mixins.
*/
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.walkAtRules(node => {
// Skip non-mixin atrules and internal mixins.
if (node.name !== 'mixin' || node.params.indexOf('_') === 0) {
return;
}

node.nodes.forEach(childNode => {
if (childNode.type === 'rule' && childNode.selector.indexOf('&') === 0) {
stylelint.utils.report({
result,
ruleName,
message: messages.expected(),
node: childNode
});
}
});
});
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
module.exports = plugin;