@@ -10,8 +10,9 @@ import type {
10
10
SvelteStyleDirective
11
11
} from 'svelte-eslint-parser/lib/ast' ;
12
12
import type { AnyNode } from 'postcss' ;
13
- import { default as selectorParser , type Node as SelectorNode } from 'postcss-selector-parser' ;
13
+ import type { Node as SelectorNode } from 'postcss-selector-parser' ;
14
14
import { getSourceCode } from '../utils/compat.js' ;
15
+ import type { SourceCode } from '../types.js' ;
15
16
16
17
export default createRule ( 'no-unused-class-name' , {
17
18
meta : {
@@ -61,7 +62,9 @@ export default createRule('no-unused-class-name', {
61
62
return ;
62
63
}
63
64
const classesUsedInStyle =
64
- styleContext . status === 'success' ? findClassesInPostCSSNode ( styleContext . sourceAst ) : [ ] ;
65
+ styleContext . status === 'success'
66
+ ? findClassesInPostCSSNode ( styleContext . sourceAst , sourceCode . parserServices )
67
+ : [ ] ;
65
68
for ( const className in classesUsedInTemplate ) {
66
69
if ( ! allowedClassNames . includes ( className ) && ! classesUsedInStyle . includes ( className ) ) {
67
70
context . report ( {
@@ -102,15 +105,17 @@ function findClassesInAttribute(
102
105
/**
103
106
* Extract all class names used in a PostCSS node.
104
107
*/
105
- function findClassesInPostCSSNode ( node : AnyNode ) : string [ ] {
108
+ function findClassesInPostCSSNode (
109
+ node : AnyNode ,
110
+ parserServices : SourceCode [ 'parserServices' ]
111
+ ) : string [ ] {
106
112
if ( node . type === 'rule' ) {
107
- let classes = node . nodes . flatMap ( findClassesInPostCSSNode ) ;
108
- const processor = selectorParser ( ) ;
109
- classes = classes . concat ( findClassesInSelector ( processor . astSync ( node . selector ) ) ) ;
113
+ let classes = node . nodes . flatMap ( ( node ) => findClassesInPostCSSNode ( node , parserServices ) ) ;
114
+ classes = classes . concat ( findClassesInSelector ( parserServices . getStyleSelectorAST ( node ) ) ) ;
110
115
return classes ;
111
116
}
112
117
if ( ( node . type === 'root' || node . type === 'atrule' ) && node . nodes !== undefined ) {
113
- return node . nodes . flatMap ( findClassesInPostCSSNode ) ;
118
+ return node . nodes . flatMap ( ( node ) => findClassesInPostCSSNode ( node , parserServices ) ) ;
114
119
}
115
120
return [ ] ;
116
121
}
0 commit comments