Skip to content

Commit 99b9339

Browse files
committed
Address feedback (cleanup stylelint rule)
1 parent 558c84d commit 99b9339

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/material/core/typography/_typography.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
}
7171

7272
// Adds the base typography styles, based on a config.
73+
/* stylelint-disable-next-line material/theme-mixin-api */
7374
@mixin mat-base-typography($config, $selector: '.mat-typography') {
7475
.mat-h1, .mat-headline, #{$selector} h1 {
7576
@include mat-typography-level-to-styles($config, headline);

tools/stylelint/theme-mixin-api.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const ruleName = 'material/theme-mixin-api';
55

66
/** Regular expression that matches all theme mixins. */
77
const themeMixinRegex =
8-
/^(?:_(mat-.+)-(density)|(mat-.+)-(density|color|typography|theme))\(([^,]+)\)$/;
8+
/^(?:_(mat-.+)-(density)|(mat-.+)-(density|color|typography|theme))\((.*)\)$/;
99

1010
/**
1111
* Stylelint plugin which ensures that theme mixins have a consistent API. Besides
@@ -30,23 +30,31 @@ const plugin = stylelint.createPlugin(ruleName, (isEnabled, options, context) =>
3030
return;
3131
}
3232

33+
// Name of the component with prefix. e.g. `mat-mdc-button` or `mat-slide-toggle`.
3334
const componentName = matches[1] || matches[3];
35+
// Type of the theme mixin. e.g. `density`, `color`, `theme`.
3436
const type = matches[2] ||matches[4];
35-
const variableName = matches[5];
37+
// Naively assumes that mixin arguments can be easily retrieved by splitting based on
38+
// a comma. This is not always correct because Sass maps can be constructed in parameters.
39+
// These would contain commas that throw of the argument retrieval. It's acceptable that
40+
// this rule will fail in such edge-cases. There is no AST for `postcss.AtRule` params.
41+
const arguments = matches[5].split(',');
3642

3743
if (type === 'theme') {
38-
validateThemeMixin(node, componentName, variableName);
44+
validateThemeMixin(node, componentName, arguments);
3945
} else {
40-
validateIndividualSystemMixins(node, type, variableName);
46+
validateIndividualSystemMixins(node, type, arguments);
4147
}
4248
});
4349

44-
function validateThemeMixin(node, componentName, variableName) {
45-
if (variableName !== '$theme-or-color-config') {
50+
function validateThemeMixin(node, componentName, arguments) {
51+
if (arguments.length !== 1) {
52+
reportError(node, 'Expected theme mixin to only declare a single argument.');
53+
} else if (arguments[0] !== '$theme-or-color-config') {
4654
if (context.fix) {
47-
node.params = node.params.replace(variableName, '$theme-or-color-config');
55+
node.params = node.params.replace(arguments[0], '$theme-or-color-config');
4856
} else {
49-
reportError(node, 'Expected mixin argument to be called `$theme-or-color-config`.');
57+
reportError(node, 'Expected first mixin argument to be called `$theme-or-color-config`.');
5058
}
5159
}
5260

@@ -79,12 +87,14 @@ const plugin = stylelint.createPlugin(ruleName, (isEnabled, options, context) =>
7987
}
8088
}
8189

82-
function validateIndividualSystemMixins(node, type, variableName) {
83-
if (variableName !== '$config-or-theme') {
90+
function validateIndividualSystemMixins(node, type, arguments) {
91+
if (arguments.length !== 1) {
92+
reportError(node, 'Expected mixin to only declare a single argument.');
93+
} if (arguments[0] !== '$config-or-theme') {
8494
if (context.fix) {
85-
node.params = node.params.replace(variableName, '$config-or-theme');
95+
node.params = node.params.replace(arguments[0], '$config-or-theme');
8696
} else {
87-
reportError(node, 'Expected mixin argument to be called `$config-or-theme`.');
97+
reportError(node, 'Expected first mixin argument to be called `$config-or-theme`.');
8898
}
8999
}
90100

@@ -98,7 +108,7 @@ const plugin = stylelint.createPlugin(ruleName, (isEnabled, options, context) =>
98108
node.insertBefore(0, {prop: expectedProperty, value: expectedValue});
99109
} else {
100110
reportError(node, `Config is not extracted. Consumers could pass a theme object. ` +
101-
`Extract the configuration by using: $config: ${expectedValue}`);
111+
`Extract the configuration by using: ${expectedProperty}: ${expectedValue}`);
102112
}
103113
}
104114
}

0 commit comments

Comments
 (0)