@@ -5,7 +5,7 @@ const ruleName = 'material/theme-mixin-api';
5
5
6
6
/** Regular expression that matches all theme mixins. */
7
7
const themeMixinRegex =
8
- / ^ (?: _ ( m a t - .+ ) - ( d e n s i t y ) | ( m a t - .+ ) - ( d e n s i t y | c o l o r | t y p o g r a p h y | t h e m e ) ) \( ( [ ^ , ] + ) \) $ / ;
8
+ / ^ (?: _ ( m a t - .+ ) - ( d e n s i t y ) | ( m a t - .+ ) - ( d e n s i t y | c o l o r | t y p o g r a p h y | t h e m e ) ) \( ( . * ) \) $ / ;
9
9
10
10
/**
11
11
* Stylelint plugin which ensures that theme mixins have a consistent API. Besides
@@ -30,23 +30,31 @@ const plugin = stylelint.createPlugin(ruleName, (isEnabled, options, context) =>
30
30
return ;
31
31
}
32
32
33
+ // Name of the component with prefix. e.g. `mat-mdc-button` or `mat-slide-toggle`.
33
34
const componentName = matches [ 1 ] || matches [ 3 ] ;
35
+ // Type of the theme mixin. e.g. `density`, `color`, `theme`.
34
36
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 ( ',' ) ;
36
42
37
43
if ( type === 'theme' ) {
38
- validateThemeMixin ( node , componentName , variableName ) ;
44
+ validateThemeMixin ( node , componentName , arguments ) ;
39
45
} else {
40
- validateIndividualSystemMixins ( node , type , variableName ) ;
46
+ validateIndividualSystemMixins ( node , type , arguments ) ;
41
47
}
42
48
} ) ;
43
49
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' ) {
46
54
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' ) ;
48
56
} 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`.' ) ;
50
58
}
51
59
}
52
60
@@ -79,12 +87,14 @@ const plugin = stylelint.createPlugin(ruleName, (isEnabled, options, context) =>
79
87
}
80
88
}
81
89
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' ) {
84
94
if ( context . fix ) {
85
- node . params = node . params . replace ( variableName , '$config-or-theme' ) ;
95
+ node . params = node . params . replace ( arguments [ 0 ] , '$config-or-theme' ) ;
86
96
} 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`.' ) ;
88
98
}
89
99
}
90
100
@@ -98,7 +108,7 @@ const plugin = stylelint.createPlugin(ruleName, (isEnabled, options, context) =>
98
108
node . insertBefore ( 0 , { prop : expectedProperty , value : expectedValue } ) ;
99
109
} else {
100
110
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 } ` ) ;
102
112
}
103
113
}
104
114
}
0 commit comments