1
- import { createPlugin , Plugin , utils } from 'stylelint' ;
1
+ import { createPlugin , utils } from 'stylelint' ;
2
2
import { basename , join } from 'path' ;
3
- import { Result , Root } from 'postcss' ;
4
3
5
4
const ruleName = 'material/no-unused-import' ;
6
5
const messages = utils . ruleMessages ( ruleName , {
@@ -11,8 +10,8 @@ const messages = utils.ruleMessages(ruleName, {
11
10
} ) ;
12
11
13
12
/** Stylelint plugin that flags unused `@use` statements. */
14
- const factory = ( isEnabled : boolean , _options : never , context : { fix : boolean } ) => {
15
- return ( root : Root , result : Result ) => {
13
+ const plugin = createPlugin ( ruleName , ( isEnabled : boolean , _options , context ) => {
14
+ return ( root , result ) => {
16
15
if ( ! isEnabled ) {
17
16
return ;
18
17
}
@@ -26,29 +25,27 @@ const factory = (isEnabled: boolean, _options: never, context: {fix: boolean}) =
26
25
// Flag namespaces we didn't manage to parse so that we can fix the parsing logic.
27
26
if ( ! namespace ) {
28
27
utils . report ( {
29
- // We need these `as any` casts, because Stylelint uses an older version
30
- // of the postcss typings that don't match up with our anymore.
31
- result : result as any ,
28
+ result,
32
29
ruleName,
33
30
message : messages . invalid ( rule . params ) ,
34
- node : rule as any ,
31
+ node : rule ,
35
32
} ) ;
36
33
} else if ( ! fileContent . includes ( namespace + '.' ) ) {
37
34
if ( context . fix ) {
38
35
rule . remove ( ) ;
39
36
} else {
40
37
utils . report ( {
41
- result : result as any ,
38
+ result,
42
39
ruleName,
43
40
message : messages . expected ( namespace ) ,
44
- node : rule as any ,
41
+ node : rule ,
45
42
} ) ;
46
43
}
47
44
}
48
45
}
49
46
} ) ;
50
47
} ;
51
- } ;
48
+ } ) ;
52
49
53
50
/** Extracts the namespace of an `@use` rule from its parameters. */
54
51
function extractNamespaceFromUseStatement ( params : string ) : string | null {
@@ -88,9 +85,4 @@ function extractNamespaceFromUseStatement(params: string): string | null {
88
85
return null ;
89
86
}
90
87
91
- // Note: We need to cast the value explicitly to `Plugin` because the stylelint types
92
- // do not type the context parameter. https://stylelint.io/developer-guide/rules#add-autofix
93
- const plugin = createPlugin ( ruleName , factory as unknown as Plugin ) ;
94
- plugin . ruleName = ruleName ;
95
- plugin . messages = messages ;
96
- module . exports = plugin ;
88
+ export default plugin ;
0 commit comments