Skip to content

Commit 97aec15

Browse files
authored
remove Cannot import module 'xxx' error (#292)
1 parent d75f75f commit 97aec15

5 files changed

Lines changed: 31 additions & 114 deletions

File tree

.changeset/icy-turkeys-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@css-modules-kit/core': minor
3+
---
4+
5+
feat!: remove `Cannot import module 'xxx'` error

packages/codegen/src/project.test.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,23 @@ describe('addFile', () => {
9191
// - The check stage cache for files that indirectly import the added file should also be invalidated.
9292
const iff = await createIFF({
9393
'tsconfig.json': '{ "cmkOptions": { "enabled": true } }',
94-
'src/b.module.css': '@import "./a.module.css";', // directly
95-
'src/c.module.css': '@value a_1 from "./b.module.css";', // indirectly
94+
'src/a.module.css': '',
95+
'src/b.module.css': '@value a_1 from "./a.module.css";', // directly
96+
'src/re-export.module.css': '@import "./a.module.css";',
97+
'src/c.module.css': '@value a_1 from "./re-export.module.css";', // indirectly
9698
});
9799
const project = createProject({ project: iff.rootDir });
98100
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
99101
[
100102
{
101103
"category": "error",
102104
"fileName": "<rootDir>/src/b.module.css",
103-
"length": 14,
105+
"length": 3,
104106
"start": {
105-
"column": 10,
107+
"column": 8,
106108
"line": 1,
107109
},
108-
"text": "Cannot import module './a.module.css'",
110+
"text": "Module './a.module.css' has no exported token 'a_1'.",
109111
},
110112
{
111113
"category": "error",
@@ -115,12 +117,12 @@ describe('addFile', () => {
115117
"column": 8,
116118
"line": 1,
117119
},
118-
"text": "Module './b.module.css' has no exported token 'a_1'.",
120+
"text": "Module './re-export.module.css' has no exported token 'a_1'.",
119121
},
120122
]
121123
`);
122124
await writeFile(iff.join('src/a.module.css'), '@value a_1: red;');
123-
project.addFile(iff.join('src/a.module.css'));
125+
project.updateFile(iff.join('src/a.module.css'));
124126
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`[]`);
125127
});
126128
test('changes the resolution results of import specifiers in other files', async () => {
@@ -204,19 +206,19 @@ describe('updateFile', () => {
204206
`);
205207

206208
// New semantic diagnostics are reported
207-
await writeFile(iff.join('src/a.module.css'), `@import './non-existent.module.css';`);
209+
await writeFile(iff.join('src/a.module.css'), `.a-1 {}`);
208210
project.updateFile(iff.join('src/a.module.css'));
209211
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
210212
[
211213
{
212214
"category": "error",
213215
"fileName": "<rootDir>/src/a.module.css",
214-
"length": 25,
216+
"length": 3,
215217
"start": {
216-
"column": 10,
218+
"column": 2,
217219
"line": 1,
218220
},
219-
"text": "Cannot import module './non-existent.module.css'",
221+
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
220222
},
221223
]
222224
`);
@@ -311,25 +313,16 @@ describe('removeFile', () => {
311313
const iff = await createIFF({
312314
'tsconfig.json': '{ "cmkOptions": { "enabled": true } }',
313315
'src/a.module.css': '@value a_1: red;',
314-
'src/b.module.css': '@import "./a.module.css";', // directly
315-
'src/c.module.css': '@value a_1 from "./b.module.css";', // indirectly
316+
'src/b.module.css': '@value a_1 from "./a.module.css";', // directly
317+
'src/re-export.module.css': '@import "./a.module.css";',
318+
'src/c.module.css': '@value a_1 from "./re-export.module.css";', // indirectly
316319
});
317320
const project = createProject({ project: iff.rootDir });
318321
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`[]`);
319322
await rm(iff.join('src/a.module.css'));
320323
project.removeFile(iff.join('src/a.module.css'));
321324
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
322325
[
323-
{
324-
"category": "error",
325-
"fileName": "<rootDir>/src/b.module.css",
326-
"length": 14,
327-
"start": {
328-
"column": 10,
329-
"line": 1,
330-
},
331-
"text": "Cannot import module './a.module.css'",
332-
},
333326
{
334327
"category": "error",
335328
"fileName": "<rootDir>/src/c.module.css",
@@ -338,7 +331,7 @@ describe('removeFile', () => {
338331
"column": 8,
339332
"line": 1,
340333
},
341-
"text": "Module './b.module.css' has no exported token 'a_1'.",
334+
"text": "Module './re-export.module.css' has no exported token 'a_1'.",
342335
},
343336
]
344337
`);
@@ -466,8 +459,8 @@ describe('getDiagnostics', () => {
466459
test('returns semantic diagnostics', async () => {
467460
const iff = await createIFF({
468461
'tsconfig.json': '{ "cmkOptions": { "enabled": true } }',
469-
'src/a.module.css': `@import './non-existent-1.module.css';`,
470-
'src/b.module.css': `@import './non-existent-2.module.css';`,
462+
'src/a.module.css': `.a-1 {}`,
463+
'src/b.module.css': `.b-1 {}`,
471464
});
472465
const project = createProject({ project: iff.rootDir });
473466
const diagnostics = project.getDiagnostics();
@@ -476,22 +469,22 @@ describe('getDiagnostics', () => {
476469
{
477470
"category": "error",
478471
"fileName": "<rootDir>/src/a.module.css",
479-
"length": 27,
472+
"length": 3,
480473
"start": {
481-
"column": 10,
474+
"column": 2,
482475
"line": 1,
483476
},
484-
"text": "Cannot import module './non-existent-1.module.css'",
477+
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
485478
},
486479
{
487480
"category": "error",
488481
"fileName": "<rootDir>/src/b.module.css",
489-
"length": 27,
482+
"length": 3,
490483
"start": {
491-
"column": 10,
484+
"column": 2,
492485
"line": 1,
493486
},
494-
"text": "Cannot import module './non-existent-2.module.css'",
487+
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
495488
},
496489
]
497490
`);

packages/core/src/checker.test.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -285,59 +285,6 @@ describe('checkCSSModule', () => {
285285
]
286286
`);
287287
});
288-
test('report diagnostics for non-existing module', () => {
289-
const args = prepareCheckerArgs([
290-
fakeCSSModule({
291-
fileName: '/a.module.css',
292-
tokenImporters: [
293-
fakeAtImportTokenImporter({ from: './b.module.css', fromLoc: fakeLoc({ column: 1 }) }),
294-
fakeAtValueTokenImporter({
295-
from: './c.module.css',
296-
fromLoc: fakeLoc({ column: 2 }),
297-
values: [fakeAtValueTokenImporterValue({ name: 'c_1', loc: fakeLoc({ column: 3 }) })],
298-
}),
299-
],
300-
}),
301-
]);
302-
const diagnostics = checkCSSModule(
303-
args.cssModules[0],
304-
args.config,
305-
args.exportBuilder,
306-
args.matchesPattern,
307-
args.resolver,
308-
args.getCSSModule,
309-
);
310-
expect(diagnostics).toMatchInlineSnapshot(`
311-
[
312-
{
313-
"category": "error",
314-
"file": {
315-
"fileName": "/a.module.css",
316-
"text": "",
317-
},
318-
"length": 0,
319-
"start": {
320-
"column": 1,
321-
"line": 1,
322-
},
323-
"text": "Cannot import module './b.module.css'",
324-
},
325-
{
326-
"category": "error",
327-
"file": {
328-
"fileName": "/a.module.css",
329-
"text": "",
330-
},
331-
"length": 0,
332-
"start": {
333-
"column": 2,
334-
"line": 1,
335-
},
336-
"text": "Cannot import module './c.module.css'",
337-
},
338-
]
339-
`);
340-
});
341288
test('report diagnostics for non-exported token', () => {
342289
const args = prepareCheckerArgs([
343290
fakeCSSModule({

packages/core/src/checker.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
Location,
99
MatchesPattern,
1010
Resolver,
11-
TokenImporter,
1211
} from './type.js';
1312
import { isValidAsJSIdentifier } from './util.js';
1413

@@ -40,10 +39,7 @@ export function checkCSSModule(
4039
const from = resolver(tokenImporter.from, { request: cssModule.fileName });
4140
if (!from || !matchesPattern(from)) continue;
4241
const imported = getCSSModule(from);
43-
if (!imported) {
44-
diagnostics.push(createCannotImportModuleDiagnostic(cssModule, tokenImporter));
45-
continue;
46-
}
42+
if (!imported) continue;
4743

4844
if (tokenImporter.type === 'value') {
4945
const exportRecord = exportBuilder.build(imported);
@@ -77,16 +73,6 @@ export function checkCSSModule(
7773
return diagnostics;
7874
}
7975

80-
function createCannotImportModuleDiagnostic(cssModule: CSSModule, tokenImporter: TokenImporter): Diagnostic {
81-
return {
82-
text: `Cannot import module '${tokenImporter.from}'`,
83-
category: 'error',
84-
file: { fileName: cssModule.fileName, text: cssModule.text },
85-
start: { line: tokenImporter.fromLoc.start.line, column: tokenImporter.fromLoc.start.column },
86-
length: tokenImporter.fromLoc.end.offset - tokenImporter.fromLoc.start.offset,
87-
};
88-
}
89-
9076
function createModuleHasNoExportedTokenDiagnostic(
9177
cssModule: CSSModule,
9278
tokenImporter: AtValueTokenImporter,

packages/ts-plugin/e2e-test/feature/semantic-diagnostics.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,6 @@ test('Semantic Diagnostics', async () => {
7979
},
8080
"text": "Module './c.module.css' has no exported token 'c_3'.",
8181
},
82-
{
83-
"category": "error",
84-
"code": 0,
85-
"end": {
86-
"line": 5,
87-
"offset": 24,
88-
},
89-
"source": "css-modules-kit",
90-
"start": {
91-
"line": 5,
92-
"offset": 10,
93-
},
94-
"text": "Cannot import module './d.module.css'",
95-
},
9682
]
9783
`);
9884
});

0 commit comments

Comments
 (0)