Skip to content

Commit bf01bee

Browse files
authored
ts-plugin report the import of files where .d.ts exists but .module.css does not exist (#193)
* make ts-plugin report the import of files where .d.ts exists but .module.css does not exist * add changelog
1 parent 300fd2c commit bf01bee

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

.changeset/grumpy-dogs-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@css-modules-kit/ts-plugin': patch
3+
---
4+
5+
fix: make ts-plugin report the import of files where .d.ts exists but .module.css does not exist
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import dedent from 'dedent';
2+
import { expect, test } from 'vitest';
3+
import { createIFF } from './test-util/fixture.js';
4+
import { launchTsserver } from './test-util/tsserver.js';
5+
6+
test('report the import of files where .d.ts exists but .module.css does not exist', async () => {
7+
const tsserver = launchTsserver();
8+
const iff = await createIFF({
9+
// `a.module.css` is not exist, and the .d.ts file is exist.
10+
// But `'./a.module.css'` should report an error.
11+
'index.ts': dedent`
12+
import styles from './a.module.css';
13+
`,
14+
'generated/a.module.css.d.ts': dedent`
15+
const styles: {};
16+
export default styles;
17+
`,
18+
'tsconfig.json': dedent`
19+
{
20+
"compilerOptions": {
21+
"rootDirs": [".", "generated"],
22+
}
23+
}
24+
`,
25+
});
26+
await tsserver.sendUpdateOpen({
27+
openFiles: [{ file: iff.paths['index.ts'] }],
28+
});
29+
const res = await tsserver.sendSemanticDiagnosticsSync({
30+
file: iff.paths['index.ts'],
31+
});
32+
expect(res.body).toMatchInlineSnapshot(`
33+
[
34+
{
35+
"category": "error",
36+
"code": 2307,
37+
"end": {
38+
"line": 1,
39+
"offset": 36,
40+
},
41+
"start": {
42+
"line": 1,
43+
"offset": 20,
44+
},
45+
"text": "Cannot find module './a.module.css' or its corresponding type declarations.",
46+
},
47+
]
48+
`);
49+
});

packages/ts-plugin/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ const plugin = createLanguageServicePlugin((ts, info) => {
3333
}
3434
}
3535

36+
// tsserver should report a “Cannot find module” error for import statements in CSS Modules that
37+
// do not exist. However, if `dtsOutDir` is included in `rootDirs` and old .d.ts files remain
38+
// in `dtsOutDir`, the error will not be reported. Therefore, remove `dtsOutDir` from `rootDirs`.
39+
const getCompilationSettings = info.languageServiceHost.getCompilationSettings.bind(info.languageServiceHost);
40+
info.languageServiceHost.getCompilationSettings = () => {
41+
const settings = { ...getCompilationSettings() };
42+
if (settings.rootDirs) {
43+
// TODO: If the `dtsOutDir` is not in `rootDirs`, warn the user.
44+
settings.rootDirs = settings.rootDirs.filter((dir) => dir !== config.dtsOutDir);
45+
}
46+
return settings;
47+
};
48+
3649
const moduleResolutionCache = info.languageServiceHost.getModuleResolutionCache?.();
3750
const resolver = createResolver(config.compilerOptions, moduleResolutionCache);
3851
const matchesPattern = createMatchesPattern(config);

0 commit comments

Comments
 (0)