Skip to content

Commit 1384594

Browse files
committed
exclude code fixes for named import
1 parent e6247f2 commit 1384594

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

  • packages/ts-plugin/src/language-service/feature

packages/ts-plugin/src/language-service/feature/code-fix.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 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';
33
import type { Language } from '@volar/language-core';
44
import ts from 'typescript';
55
import { 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(/^Add import from "(.*)"$/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+
4666
interface TokenConsumer {
4767
/** The token name (e.g. `foo` in `styles.foo`) */
4868
tokenName: string;

0 commit comments

Comments
 (0)