Skip to content

Commit de29e53

Browse files
authored
Revert "remove Cannot import module 'xxx' error (#292)"
This reverts commit 97aec15.
1 parent 97aec15 commit de29e53

5 files changed

Lines changed: 114 additions & 31 deletions

File tree

.changeset/icy-turkeys-joke.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/codegen/src/project.test.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,21 @@ 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/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
94+
'src/b.module.css': '@import "./a.module.css";', // directly
95+
'src/c.module.css': '@value a_1 from "./b.module.css";', // indirectly
9896
});
9997
const project = createProject({ project: iff.rootDir });
10098
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
10199
[
102100
{
103101
"category": "error",
104102
"fileName": "<rootDir>/src/b.module.css",
105-
"length": 3,
103+
"length": 14,
106104
"start": {
107-
"column": 8,
105+
"column": 10,
108106
"line": 1,
109107
},
110-
"text": "Module './a.module.css' has no exported token 'a_1'.",
108+
"text": "Cannot import module './a.module.css'",
111109
},
112110
{
113111
"category": "error",
@@ -117,12 +115,12 @@ describe('addFile', () => {
117115
"column": 8,
118116
"line": 1,
119117
},
120-
"text": "Module './re-export.module.css' has no exported token 'a_1'.",
118+
"text": "Module './b.module.css' has no exported token 'a_1'.",
121119
},
122120
]
123121
`);
124122
await writeFile(iff.join('src/a.module.css'), '@value a_1: red;');
125-
project.updateFile(iff.join('src/a.module.css'));
123+
project.addFile(iff.join('src/a.module.css'));
126124
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`[]`);
127125
});
128126
test('changes the resolution results of import specifiers in other files', async () => {
@@ -206,19 +204,19 @@ describe('updateFile', () => {
206204
`);
207205

208206
// New semantic diagnostics are reported
209-
await writeFile(iff.join('src/a.module.css'), `.a-1 {}`);
207+
await writeFile(iff.join('src/a.module.css'), `@import './non-existent.module.css';`);
210208
project.updateFile(iff.join('src/a.module.css'));
211209
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
212210
[
213211
{
214212
"category": "error",
215213
"fileName": "<rootDir>/src/a.module.css",
216-
"length": 3,
214+
"length": 25,
217215
"start": {
218-
"column": 2,
216+
"column": 10,
219217
"line": 1,
220218
},
221-
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
219+
"text": "Cannot import module './non-existent.module.css'",
222220
},
223221
]
224222
`);
@@ -313,16 +311,25 @@ describe('removeFile', () => {
313311
const iff = await createIFF({
314312
'tsconfig.json': '{ "cmkOptions": { "enabled": true } }',
315313
'src/a.module.css': '@value a_1: red;',
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
314+
'src/b.module.css': '@import "./a.module.css";', // directly
315+
'src/c.module.css': '@value a_1 from "./b.module.css";', // indirectly
319316
});
320317
const project = createProject({ project: iff.rootDir });
321318
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`[]`);
322319
await rm(iff.join('src/a.module.css'));
323320
project.removeFile(iff.join('src/a.module.css'));
324321
expect(formatDiagnostics(project.getDiagnostics(), iff.rootDir)).toMatchInlineSnapshot(`
325322
[
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+
},
326333
{
327334
"category": "error",
328335
"fileName": "<rootDir>/src/c.module.css",
@@ -331,7 +338,7 @@ describe('removeFile', () => {
331338
"column": 8,
332339
"line": 1,
333340
},
334-
"text": "Module './re-export.module.css' has no exported token 'a_1'.",
341+
"text": "Module './b.module.css' has no exported token 'a_1'.",
335342
},
336343
]
337344
`);
@@ -459,8 +466,8 @@ describe('getDiagnostics', () => {
459466
test('returns semantic diagnostics', async () => {
460467
const iff = await createIFF({
461468
'tsconfig.json': '{ "cmkOptions": { "enabled": true } }',
462-
'src/a.module.css': `.a-1 {}`,
463-
'src/b.module.css': `.b-1 {}`,
469+
'src/a.module.css': `@import './non-existent-1.module.css';`,
470+
'src/b.module.css': `@import './non-existent-2.module.css';`,
464471
});
465472
const project = createProject({ project: iff.rootDir });
466473
const diagnostics = project.getDiagnostics();
@@ -469,22 +476,22 @@ describe('getDiagnostics', () => {
469476
{
470477
"category": "error",
471478
"fileName": "<rootDir>/src/a.module.css",
472-
"length": 3,
479+
"length": 27,
473480
"start": {
474-
"column": 2,
481+
"column": 10,
475482
"line": 1,
476483
},
477-
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
484+
"text": "Cannot import module './non-existent-1.module.css'",
478485
},
479486
{
480487
"category": "error",
481488
"fileName": "<rootDir>/src/b.module.css",
482-
"length": 3,
489+
"length": 27,
483490
"start": {
484-
"column": 2,
491+
"column": 10,
485492
"line": 1,
486493
},
487-
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
494+
"text": "Cannot import module './non-existent-2.module.css'",
488495
},
489496
]
490497
`);

packages/core/src/checker.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,59 @@ 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+
});
288341
test('report diagnostics for non-exported token', () => {
289342
const args = prepareCheckerArgs([
290343
fakeCSSModule({

packages/core/src/checker.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
Location,
99
MatchesPattern,
1010
Resolver,
11+
TokenImporter,
1112
} from './type.js';
1213
import { isValidAsJSIdentifier } from './util.js';
1314

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

4448
if (tokenImporter.type === 'value') {
4549
const exportRecord = exportBuilder.build(imported);
@@ -73,6 +77,16 @@ export function checkCSSModule(
7377
return diagnostics;
7478
}
7579

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+
7690
function createModuleHasNoExportedTokenDiagnostic(
7791
cssModule: CSSModule,
7892
tokenImporter: AtValueTokenImporter,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ 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+
},
8296
]
8397
`);
8498
});

0 commit comments

Comments
 (0)