8000 Adjust TS2691 message for .ts import sources (#42184) · typescript-bot/TypeScript@7a5aadc · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a5aadc

Browse files
authored
Adjust TS2691 message for .ts import sources (microsoft#42184)
* Adjust TS2691 message for .ts import sources * Only ModuleKind is needed for TS2691 logic * Added tests for TS2691
1 parent 64d0538 commit 7a5aadc

11 files changed

+312
-1
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3310,7 +3310,17 @@ namespace ts {
33103310
const tsExtension = tryExtractTSExtension(moduleReference);
33113311
if (tsExtension) {
33123312
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
3313-
error(errorNode, diag, tsExtension, removeExtension(moduleReference, tsExtension));
3313+
const importSourceWithoutExtension = removeExtension(moduleReference, tsExtension);
3314+
let replacedImportSource = importSourceWithoutExtension;
3315+
/**
3316+
* Direct users to import source with .js extension if outputting an ES module.
3317+
* @see https://github.com/microsoft/TypeScript/issues/42151
3318+
*/
3319+
const moduleKind = getEmitModuleKind(compilerOptions);
3320+
if (moduleKind >= ModuleKind.ES2015) {
3321+
replacedImportSource += ".js";
3322+
}
3323+
error(errorNode, diag, tsExtension, replacedImportSource);
33143324
}
33153325
else if (!compilerOptions.resolveJsonModule &&
33163326
fileExtensionIs(moduleReference, Extension.Json) &&
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead.
2+
tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead.
3+
tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
4+
5+
6+
==== tests/cases/compiler/x.ts (0 errors) ====
7+
// CommonJS output
8+
9+
export default 0;
10+
11+
==== tests/cases/compiler/y.tsx (0 errors) ====
12+
export default 0;
13+
14+
==== tests/cases/compiler/z.d.ts (0 errors) ====
15+
declare const x: number;
16+
export default x;
17+
18+
==== tests/cases/compiler/user.ts (3 errors) ====
19+
import x from "./x.ts";
20+
~~~~~~~~
21+
!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead.
22+
import y from "./y.tsx";
23+
~~~~~~~~~
24+
!!! error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead.
25+
import z from "./z.d.ts";
26+
~~~~~~~~~~
27+
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
28+
29+
// Making sure the suggested fixes are valid:
30+
import x2 from "./x";
31+
import y2 from "./y";
32+
import z2 from "./z";
33+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [tests/cases/compiler/moduleResolutionNoTsCJS.ts] ////
2+
3+
//// [x.ts]
4+
// CommonJS output
5+
6+
export default 0;
7+
8+
//// [y.tsx]
9+
export default 0;
10+
11+
//// [z.d.ts]
12+
declare const x: number;
13+
export default x;
14+
15+
//// [user.ts]
16+
import x from "./x.ts";
17+
import y from "./y.tsx";
18+
import z from "./z.d.ts";
19+
20+
// Making sure the suggested fixes are valid:
21+
import x2 from "./x";
22+
import y2 from "./y";
23+
import z2 from "./z";
24+
25+
26+
//// [x.js]
27+
"use strict";
28+
// CommonJS output
29+
exports.__esModule = true;
30+
exports["default"] = 0;
31+
//// [y.jsx]
32+
"use strict";
33+
exports.__esModule = true;
34+
exports["default"] = 0;
35+
//// [user.js]
36+
"use strict";
37+
exports.__esModule = true;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/x.ts ===
2+
// CommonJS output
3+
No type information for this code.
4+
No type information for this code.export default 0;
5+
No type information for this code.
6+
No type information for this code.=== tests/cases/compiler/y.tsx ===
7+
export default 0;
8+
No type information for this code.
9+
No type information for this code.=== te F438 sts/cases/compiler/z.d.ts ===
10+
declare const x: number;
11+
>x : Symbol(x, Decl(z.d.ts, 0, 13))
12+
13+
export default x;
14+
>x : Symbol(x, Decl(z.d.ts, 0, 13))
15+
16+
=== tests/cases/compiler/user.ts ===
17+
import x from "./x.ts";
18+
>x : Symbol(x, Decl(user.ts, 0, 6))
19+
20+
import y from "./y.tsx";
21+
>y : Symbol(y, Decl(user.ts, 1, 6))
22+
23+
import z from "./z.d.ts";
24+
>z : Symbol(z, Decl(user.ts, 2, 6))
25+
26+
// Making sure the suggested fixes are valid:
27+
import x2 from "./x";
28+
>x2 : Symbol(x2, Decl(user.ts, 5, 6))
29+
30+
import y2 from "./y";
31+
>y2 : Symbol(y2, Decl(user.ts, 6, 6))
32+
33+
import z2 from "./z";
34+
>z2 : Symbol(z2, Decl(user.ts, 7, 6))
35+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/x.ts ===
2+
// CommonJS output
3+
No type information for this code.
4+
No type information for this code.export default 0;
5+
No type information for this code.
6+
No type information for this code.=== tests/cases/compiler/y.tsx ===
7+
export default 0;
8+
No type information for this code.
9+
No type information for this code.=== tests/cases/compiler/z.d.ts ===
10+
declare const x: number;
11+
>x : number
12+
13+
export default x;
14+
>x : number
15+
16+
=== tests/cases/compiler/user.ts ===
17+
import x from "./x.ts";
18+
>x : any
19+
20+
import y from "./y.tsx";
21+
>y : any
22+
23+
import z from "./z.d.ts";
24+
>z : any
25+
26+
// Making sure the suggested fixes are valid:
27+
import x2 from "./x";
28+
>x2 : 0
29+
30+
import y2 from "./y";
31+
>y2 : 0
32+
33+
import z2 from "./z";
34+
>z2 : number
35+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x.js' instead.
2+
tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y.js' instead.
3+
tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z.js' instead.
4+
5+
6+
==== tests/cases/compiler/x.ts (0 errors) ====
7+
// ESM output
8+
9+
export default 0;
10+
11+
==== tests/cases/compiler/y.tsx (0 errors) ====
12+
export default 0;
13+
14+
==== tests/cases/compiler/z.d.ts (0 errors) ====
15+
declare const x: number;
16+
export default x;
17+
18+
==== tests/cases/compiler/user.ts (3 errors) ====
19+
import x from "./x.ts";
20+
~~~~~~~~
21+
!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x.js' instead.
22+
import y from "./y.tsx";
23+
~~~~~~~~~
24+
!!! error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y.js' instead.
25+
import z from "./z.d.ts";
26+
~~~~~~~~~~
27+
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z.js' instead.
28+
29+
// Making sure the suggested fixes are valid:
30+
import x2 from "./x";
31+
import y2 from "./y";
32+
import z2 from "./z";
33+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [tests/cases/compiler/moduleResolutionNoTsESM.ts] ////
2+
3+
//// [x.ts]
4+
// ESM output
5+
6+
export default 0;
7+
8+
//// [y.tsx]
9+
export default 0;
10+
11+
//// [z.d.ts]
12+
declare const x: number;
13+
export default x;
14+
15+
//// [user.ts]
16+
import x from "./x.ts";
17+
import y from "./y.tsx";
18+
import z from "./z.d.ts";
19+
20+
// Making sure the suggested fixes are valid:
21+
import x2 from "./x";
22+
import y2 from "./y";
23+
import z2 from "./z";
24+
25+
26+
//// [x.js]
27+
// ESM output
28+
export default 0;
29+
//// [y.jsx]
30+
export default 0;
31+
//// [user.js]
32+
export {};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/x.ts ===
2+
// ESM output
3+
No type information for this code.
4+
No type information for this code.export default 0;
5+
No type information for this code.
6+
No type information for this code.=== tests/cases/compiler/y.tsx ===
7+
export default 0;
8+
No type information for this code.
9+
No type information for this code.=== tests/cases/compiler/z.d.ts ===
10+
declare const x: number;
11+
>x : Symbol(x, Decl(z.d.ts, 0, 13))
12+
13+
export default x;
14+
>x : Symbol(x, Decl(z.d.ts, 0, 13))
15+
16+
=== tests/cases/compiler/user.ts ===
17+
import x from "./x.ts";
18+
>x : Symbol(x, Decl(user.ts, 0, 6))
19+
20+
import y from "./y.tsx";
21+
>y : Symbol(y, Decl(user.ts, 1, 6))
22+
23+
import z from "./z.d.ts";
24+
>z : Symbol(z, Decl(user.ts, 2, 6))
25+
26+
// Making sure the suggested fixes are valid:
27+
import x2 from "./x";
28+
>x2 : Symbol(x2, Decl(user.ts, 5, 6))
29+
30+
import y2 from "./y";
31+
>y2 : Symbol(y2, Decl(user.ts, 6, 6))
32+
33+
import z2 from "./z";
34+
>z2 : Symbol(z2, Decl(user.ts, 7, 6))
35+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/x.ts ===
2+
// ESM output
3+
No type information for this code.
4+
No type information for this code.export default 0;
5+
No type information for this code.
6+
No type information for this code.=== tests/cases/compiler/y.tsx ===
7+
export default 0;
8+
No type information for this code.
9+
No type information for this code.=== tests/cases/compiler/z.d.ts ===
10+
declare const x: number;
11+
>x : number
12+
13+
export default x;
14+
>x : number
15+
16+
=== tests/cases/compiler/user.ts ===
17+
import x from "./x.ts";
18+
>x : any
19+
20+
import y from "./y.tsx";
21+
>y : any
22+
23+
import z from "./z.d.ts";
24+
>z : any
25+
26+
// Making sure the suggested fixes are valid:
27+
import x2 from "./x";
28+
>x2 : 0
29+
30+
import y2 from "./y";
31+
>y2 : 0
32+
33+
import z2 from "./z";
34+
>z2 : number
35+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// CommonJS output
2+
// @module: commonjs
3+
4+
// @jsx: Preserve
5+
// @filename: x.ts
6+
export default 0;
7+
8+
// @filename: y.tsx
9+
export default 0;
10+
11+
// @filename: z.d.ts
12+
declare const x: number;
13+
export default x;
14+
15+
// @filename: user.ts
16+
import x from "./x.ts";
17+
import y from "./y.tsx";
18+
import z from "./z.d.ts";
19+
20+
// Making sure the suggested fixes are valid:
21+
import x2 from "./x";
22+
import y2 from "./y";
23+
import z2 from "./z";

tests/cases/compiler/moduleResolutionNoTs.ts renamed to tests/cases/compiler/moduleResolutionNoTsESM.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// ESM output
2+
// @module: es2015
3+
14
// @jsx: Preserve
25
// @filename: x.ts
36
export default 0;

0 commit comments

Comments
 (0)
0