Skip to content

Commit 49e6983

Browse files
authored
Preserve non-module errors for imports without attributes (#64884)
1 parent 8a62bbe commit 49e6983

7 files changed

Lines changed: 331 additions & 3 deletions

‎tsc/internal/checker/checker.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15700,9 +15700,11 @@ func (c *Checker) resolveExternalModule(
1570015700
}
1570115701
return c.tryResolvePatternAmbientModule(c.getMergedSymbol(sourceFile.Symbol), moduleReference, importAttributesType)
1570215702
}
15703-
patternAmbientModule := c.tryResolvePatternAmbientModule(nil /*resolvedSymbol*/, moduleReference, importAttributesType)
15704-
if patternAmbientModule != nil {
15705-
return patternAmbientModule
15703+
if !c.isEmptyObjectType(importAttributesType) {
15704+
patternAmbientModule := c.tryResolvePatternAmbientModule(nil /*resolvedSymbol*/, moduleReference, importAttributesType)
15705+
if patternAmbientModule != nil {
15706+
return patternAmbientModule
15707+
}
1570615708
}
1570715709
if errorNode != nil && moduleNotFoundError != nil && !isSideEffectImport(errorNode) {
1570815710
c.error(errorNode, diagnostics.File_0_is_not_a_module, resolvedModule.ResolvedFileName)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/src/main.ts(4,8): error TS1192: Module '"/src/script"' has no default export.
2+
/src/main.ts(5,7): error TS2322: Type 'typeof import("/stub")' is not assignable to type 'number'.
3+
/src/main.ts(6,7): error TS2322: Type 'typeof import("/stub")' is not assignable to type 'number'.
4+
/src/main.ts(7,7): error TS2322: Type 'typeof import("/generated/component.svelte")' is not assignable to type 'number'.
5+
6+
7+
==== /tsconfig.json (0 errors) ====
8+
{
9+
"compilerOptions": {
10+
"paths": { "x.svelte": ["./stub.d.ts"] },
11+
"rootDirs": ["./src", "./generated"]
12+
},
13+
"files": ["ambient.d.ts", "src/main.ts"]
14+
}
15+
16+
==== /ambient.d.ts (0 errors) ====
17+
declare module "*.svelte" {
18+
const value: number;
19+
export default value;
20+
}
21+
declare module "*.svelte" with { type: "text" } {
22+
const value: string;
23+
export default value;
24+
}
25+
declare module "*.js" {
26+
const value: number;
27+
export default value;
28+
}
29+
30+
==== /src/main.ts (4 errors) ====
31+
import fromPaths from "x.svelte";
32+
import emptyAttributes from "x.svelte" with {};
33+
import fromRootDirs from "./component.svelte";
34+
import fromRelative from "./script.js";
35+
~~~~~~~~~~~~
36+
!!! error TS1192: Module '"/src/script"' has no default export.
37+
const fromPathsValue: number = fromPaths;
38+
~~~~~~~~~~~~~~
39+
!!! error TS2322: Type 'typeof import("/stub")' is not assignable to type 'number'.
40+
const emptyAttributesValue: number = emptyAttributes;
41+
~~~~~~~~~~~~~~~~~~~~
42+
!!! error TS2322: Type 'typeof import("/stub")' is not assignable to type 'number'.
43+
const fromRootDirsValue: number = fromRootDirs;
44+
~~~~~~~~~~~~~~~~~
45+
!!! error TS2322: Type 'typeof import("/generated/component.svelte")' is not assignable to type 'number'.
46+
47+
import text from "x.svelte" with { type: "text" };
48+
const textValue: string = text;
49+
50+
import missing from "./missing.svelte";
51+
const missingValue: number = missing;
52+
53+
==== /stub.d.ts (0 errors) ====
54+
export {};
55+
56+
==== /generated/component.svelte.d.ts (0 errors) ====
57+
export {};
58+
59+
==== /src/script.ts (0 errors) ====
60+
export {};
61+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/src/main.ts(1,23): error TS2306: File '/stub.d.ts' is not a module.
2+
/src/main.ts(2,29): error TS2306: File '/stub.d.ts' is not a module.
3+
/src/main.ts(3,26): error TS2306: File '/generated/component.svelte.d.ts' is not a module.
4+
/src/main.ts(4,26): error TS2306: File '/src/script.ts' is not a module.
5+
6+
7+
==== /tsconfig.json (0 errors) ====
8+
{
9+
"compilerOptions": {
10+
"paths": { "x.svelte": ["./stub.d.ts"] },
11+
"rootDirs": ["./src", "./generated"]
12+
},
13+
"files": ["ambient.d.ts", "src/main.ts"]
14+
}
15+
16+
==== /ambient.d.ts (0 errors) ====
17+
declare module "*.svelte" {
18+
const value: number;
19+
export default value;
20+
}
21+
declare module "*.svelte" with { type: "text" } {
22+
const value: string;
23+
export default value;
24+
}
25+
declare module "*.js" {
26+
const value: number;
27+
export default value;
28+
}
29+
30+
==== /src/main.ts (4 errors) ====
31+
import fromPaths from "x.svelte";
32+
~~~~~~~~~~
33+
!!! error TS2306: File '/stub.d.ts' is not a module.
34+
import emptyAttributes from "x.svelte" with {};
35+
~~~~~~~~~~
36+
!!! error TS2306: File '/stub.d.ts' is not a module.
37+
import fromRootDirs from "./component.svelte";
38+
~~~~~~~~~~~~~~~~~~~~
39+
!!! error TS2306: File '/generated/component.svelte.d.ts' is not a module.
40+
import fromRelative from "./script.js";
41+
~~~~~~~~~~~~~
42+
!!! error TS2306: File '/src/script.ts' is not a module.
43+
const fromPathsValue: string = fromPaths;
44+
45+
import text from "x.svelte" with { type: "text" };
46+
const textValue: string = text;
47+
48+
import missing from "./missing.svelte";
49+
const missingValue: number = missing;
50+
51+
==== /stub.d.ts (0 errors) ====
52+
// A resolved script must not fall back to a pattern ambient module without attributes.
53+
54+
==== /generated/component.svelte.d.ts (0 errors) ====
55+
// A script resolved through rootDirs.
56+
57+
==== /src/script.ts (0 errors) ====
58+
// A script resolved through a relative import.
59+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/src/main.ts(3,26): error TS2306: File '/generated/component.svelte.d.ts' is not a module.
2+
/src/main.ts(4,26): error TS2306: File '/src/script.ts' is not a module.
3+
/src/main.ts(5,7): error TS2322: Type 'number' is not assignable to type 'string'.
4+
5+
6+
==== /tsconfig.json (0 errors) ====
7+
{
8+
"compilerOptions": {
9+
"rootDirs": ["./src", "./generated"]
10+
},
11+
"files": ["ambient.d.ts", "src/main.ts"]
12+
}
13+
14+
==== /ambient.d.ts (0 errors) ====
15+
declare module "*.svelte" {
16+
const value: number;
17+
export default value;
18+
}
19+
declare module "*.svelte" with { type: "text" } {
20+
const value: string;
21+
export default value;
22+
}
23+
declare module "*.js" {
24+
const value: number;
25+
export default value;
26+
}
27+
28+
==== /src/main.ts (3 errors) ====
29+
import fromPaths from "x.svelte";
30+
import emptyAttributes from "x.svelte" with {};
31+
import fromRootDirs from "./component.svelte";
32+
~~~~~~~~~~~~~~~~~~~~
33+
!!! error TS2306: File '/generated/component.svelte.d.ts' is not a module.
34+
import fromRelative from "./script.js";
35+
~~~~~~~~~~~~~
36+
!!! error TS2306: File '/src/script.ts' is not a module.
37+
const fromPathsValue: string = fromPaths;
38+
~~~~~~~~~~~~~~
39+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
40+
41+
import text from "x.svelte" with { type: "text" };
42+
const textValue: string = text;
43+
44+
import missing from "./missing.svelte";
45+
const missingValue: number = missing;
46+
47+
==== /stub.d.ts (0 errors) ====
48+
// This file is not resolved without a paths mapping.
49+
50+
==== /generated/component.svelte.d.ts (0 errors) ====
51+
// A script resolved through rootDirs.
52+
53+
==== /src/script.ts (0 errors) ====
54+
// A script resolved through a relative import.
55+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @strict: true
2+
// @module: esnext
3+
// @moduleResolution: bundler
4+
// @noEmit: true
5+
// @noTypesAndSymbols: true
6+
7+
// @filename: /tsconfig.json
8+
{
9+
"compilerOptions": {
10+
"paths": { "x.svelte": ["./stub.d.ts"] },
11+
"rootDirs": ["./src", "./generated"]
12+
},
13+
"files": ["ambient.d.ts", "src/main.ts"]
14+
}
15+
16+
// @filename: /ambient.d.ts
17+
declare module "*.svelte" {
18+
const value: number;
19+
export default value;
20+
}
21+
declare module "*.svelte" with { type: "text" } {
22+
const value: string;
23+
export default value;
24+
}
25+
declare module "*.js" {
26+
const value: number;
27+
export default value;
28+
}
29+
30+
// @filename: /stub.d.ts
31+
export {};
32+
33+
// @filename: /generated/component.svelte.d.ts
34+
export {};
35+
36+
// @filename: /src/script.ts
37+
export {};
38+
39+
// @filename: /src/main.ts
40+
import fromPaths from "x.svelte";
41+
import emptyAttributes from "x.svelte" with {};
42+
import fromRootDirs from "./component.svelte";
43+
import fromRelative from "./script.js";
44+
const fromPathsValue: number = fromPaths;
45+
const emptyAttributesValue: number = emptyAttributes;
46+
const fromRootDirsValue: number = fromRootDirs;
47+
48+
import text from "x.svelte" with { type: "text" };
49+
const textValue: string = text;
50+
51+
import missing from "./missing.svelte";
52+
const missingValue: number = missing;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// @strict: true
2+
// @module: esnext
3+
// @moduleResolution: bundler
4+
// @noEmit: true
5+
// @noTypesAndSymbols: true
6+
7+
// @filename: /tsconfig.json
8+
{
9+
"compilerOptions": {
10+
"paths": { "x.svelte": ["./stub.d.ts"] },
11+
"rootDirs": ["./src", "./generated"]
12+
},
13+
"files": ["ambient.d.ts", "src/main.ts"]
14+
}
15+
16+
// @filename: /ambient.d.ts
17+
declare module "*.svelte" {
18+
const value: number;
19+
export default value;
20+
}
21+
declare module "*.svelte" with { type: "text" } {
22+
const value: string;
23+
export default value;
24+
}
25+
declare module "*.js" {
26+
const value: number;
27+
export default value;
28+
}
29+
30+
// @filename: /stub.d.ts
31+
// A resolved script must not fall back to a pattern ambient module without attributes.
32+
33+
// @filename: /generated/component.svelte.d.ts
34+
// A script resolved through rootDirs.
35+
36+
// @filename: /src/script.ts
37+
// A script resolved through a relative import.
38+
39+
// @filename: /src/main.ts
40+
import fromPaths from "x.svelte";
41+
import emptyAttributes from "x.svelte" with {};
42+
import fromRootDirs from "./component.svelte";
43+
import fromRelative from "./script.js";
44+
const fromPathsValue: string = fromPaths;
45+
46+
import text from "x.svelte" with { type: "text" };
47+
const textValue: string = text;
48+
49+
import missing from "./missing.svelte";
50+
const missingValue: number = missing;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// @strict: true
2+
// @module: esnext
3+
// @moduleResolution: bundler
4+
// @noEmit: true
5+
// @noTypesAndSymbols: true
6+
7+
// @filename: /tsconfig.json
8+
{
9+
"compilerOptions": {
10+
"rootDirs": ["./src", "./generated"]
11+
},
12+
"files": ["ambient.d.ts", "src/main.ts"]
13+
}
14+
15+
// @filename: /ambient.d.ts
16+
declare module "*.svelte" {
17+
const value: number;
18+
export default value;
19+
}
20+
declare module "*.svelte" with { type: "text" } {
21+
const value: string;
22+
export default value;
23+
}
24+
declare module "*.js" {
25+
const value: number;
26+
export default value;
27+
}
28+
29+
// @filename: /stub.d.ts
30+
// This file is not resolved without a paths mapping.
31+
32+
// @filename: /generated/component.svelte.d.ts
33+
// A script resolved through rootDirs.
34+
35+
// @filename: /src/script.ts
36+
// A script resolved through a relative import.
37+
38+
// @filename: /src/main.ts
39+
import fromPaths from "x.svelte";
40+
import emptyAttributes from "x.svelte" with {};
41+
import fromRootDirs from "./component.svelte";
42+
import fromRelative from "./script.js";
43+
const fromPathsValue: string = fromPaths;
44+
45+
import text from "x.svelte" with { type: "text" };
46+
const textValue: string = text;
47+
48+
import missing from "./missing.svelte";
49+
const missingValue: number = missing;

0 commit comments

Comments
 (0)