Skip to content

Commit fc3aa6a

Browse files
committed
remove Cannot import module 'xxx' error
1 parent d75f75f commit fc3aa6a

5 files changed

Lines changed: 21 additions & 122 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: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,6 @@ describe('addFile', () => {
9797
const project = createProject({ project: iff.rootDir });
9898
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
9999
[
100-
{
101-
"category": "error",
102-
"fileName": "<rootDir>/src/b.module.css",
103-
"length": 14,
104-
"start": {
105-
"column": 10,
106-
"line": 1,
107-
},
108-
"text": "Cannot import module './a.module.css'",
109-
},
110100
{
111101
"category": "error",
112102
"fileName": "<rootDir>/src/c.module.css",
@@ -206,20 +196,7 @@ describe('updateFile', () => {
206196
// New semantic diagnostics are reported
207197
await writeFile(iff.join('src/a.module.css'), `@import './non-existent.module.css';`);
208198
project.updateFile(iff.join('src/a.module.css'));
209-
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
210-
[
211-
{
212-
"category": "error",
213-
"fileName": "<rootDir>/src/a.module.css",
214-
"length": 25,
215-
"start": {
216-
"column": 10,
217-
"line": 1,
218-
},
219-
"text": "Cannot import module './non-existent.module.css'",
220-
},
221-
]
222-
`);
199+
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`[]`);
223200
});
224201
test('changes diagnostics in files that import it directly or indirectly', async () => {
225202
// This test case suggests the following facts:
@@ -320,16 +297,6 @@ describe('removeFile', () => {
320297
project.removeFile(iff.join('src/a.module.css'));
321298
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
322299
[
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-
},
333300
{
334301
"category": "error",
335302
"fileName": "<rootDir>/src/c.module.css",
@@ -471,30 +438,7 @@ describe('getDiagnostics', () => {
471438
});
472439
const project = createProject({ project: iff.rootDir });
473440
const diagnostics = project.getDiagnostics();
474-
expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(`
475-
[
476-
{
477-
"category": "error",
478-
"fileName": "<rootDir>/src/a.module.css",
479-
"length": 27,
480-
"start": {
481-
"column": 10,
482-
"line": 1,
483-
},
484-
"text": "Cannot import module './non-existent-1.module.css'",
485-
},
486-
{
487-
"category": "error",
488-
"fileName": "<rootDir>/src/b.module.css",
489-
"length": 27,
490-
"start": {
491-
"column": 10,
492-
"line": 1,
493-
},
494-
"text": "Cannot import module './non-existent-2.module.css'",
495-
},
496-
]
497-
`);
441+
expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(`[]`);
498442
});
499443
test('skips semantic diagnostics when project or syntactic diagnostics exist', async () => {
500444
const iff = await createIFF({

packages/core/src/checker.test.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ describe('checkCSSModule', () => {
285285
]
286286
`);
287287
});
288-
test('report diagnostics for non-existing module', () => {
288+
test('ignore non-existing module', () => {
289289
const args = prepareCheckerArgs([
290290
fakeCSSModule({
291291
fileName: '/a.module.css',
@@ -307,36 +307,7 @@ describe('checkCSSModule', () => {
307307
args.resolver,
308308
args.getCSSModule,
309309
);
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-
`);
310+
expect(diagnostics).toMatchInlineSnapshot(`[]`);
340311
});
341312
test('report diagnostics for non-exported token', () => {
342313
const args = prepareCheckerArgs([
@@ -384,22 +355,29 @@ describe('checkCSSModule', () => {
384355
]
385356
`);
386357
});
387-
test('ignore token importers for unresolvable modules', () => {
358+
test('ignore non-existing module', () => {
388359
const args = prepareCheckerArgs([
389360
fakeCSSModule({
390361
fileName: '/a.module.css',
391-
tokenImporters: [fakeAtImportTokenImporter({ from: './unresolvable.module.css' })],
362+
tokenImporters: [
363+
fakeAtImportTokenImporter({ from: './b.module.css', fromLoc: fakeLoc({ column: 1 }) }),
364+
fakeAtValueTokenImporter({
365+
from: './c.module.css',
366+
fromLoc: fakeLoc({ column: 2 }),
367+
values: [fakeAtValueTokenImporterValue({ name: 'c_1', loc: fakeLoc({ column: 3 }) })],
368+
}),
369+
],
392370
}),
393371
]);
394372
const diagnostics = checkCSSModule(
395373
args.cssModules[0],
396374
args.config,
397375
args.exportBuilder,
398376
args.matchesPattern,
399-
() => undefined, // Simulate unresolvable module
377+
args.resolver,
400378
args.getCSSModule,
401379
);
402-
expect(diagnostics).toEqual([]);
380+
expect(diagnostics).toMatchInlineSnapshot(`[]`);
403381
});
404382
test('ignore token importers that do not match the pattern', () => {
405383
const args = prepareCheckerArgs([

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)