11import type { CMKConfig , Resolver } from '@css-modules-kit/core' ;
2- import { isComponentFileName } from '@css-modules-kit/core' ;
2+ import { isComponentFileName , isCSSModuleFile } from '@css-modules-kit/core' ;
33import type { Language } from '@volar/language-core' ;
44import ts from 'typescript' ;
55import { isCSSModuleScript } from '../../language-plugin.js' ;
@@ -23,6 +23,7 @@ export function getCodeFixesAtPosition(
2323
2424 if ( config . namedExports && ! config . prioritizeNamedImports ) {
2525 convertDefaultImportsToNamespaceImports ( prior , fileName , resolver ) ;
26+ excludeNamedImports ( prior , fileName , resolver ) ;
2627 }
2728
2829 if ( isComponentFileName ( fileName ) ) {
@@ -39,10 +40,29 @@ export function getCodeFixesAtPosition(
3940 }
4041 }
4142
42- return prior ;
43+ return prior . filter ( ( codeFix ) => codeFix . changes . length > 0 ) ;
4344 } ;
4445}
4546
47+ /**
48+ * Exclude code fixes that add named imports (e.g. `import { foo } from './a.module.css'`)
49+ */
50+ function excludeNamedImports ( codeFixes : ts . CodeFixAction [ ] , fileName : string , resolver : Resolver ) : void {
51+ for ( const codeFix of codeFixes ) {
52+ if ( codeFix . fixName !== 'import' ) continue ;
53+ const match = codeFix . description . match ( / ^ A d d i m p o r t f r o m " ( .* ) " $ / u) ;
54+ if ( ! match ) continue ;
55+ const specifier = match [ 1 ] ! ;
56+ const resolved = resolver ( specifier , { request : fileName } ) ;
57+ if ( ! resolved || ! isCSSModuleFile ( resolved ) ) continue ;
58+
59+ for ( const change of codeFix . changes ) {
60+ change . textChanges = change . textChanges . filter ( ( textChange ) => ! textChange . newText . startsWith ( `import {` ) ) ;
61+ }
62+ codeFix . changes = codeFix . changes . filter ( ( change ) => change . textChanges . length > 0 ) ;
63+ }
64+ }
65+
4666interface TokenConsumer {
4767 /** The token name (e.g. `foo` in `styles.foo`) */
4868 tokenName : string ;
0 commit comments