Adjust TS2691 message for .ts import sources (#42184)

* Adjust TS2691 message for .ts import sources

* Only ModuleKind is needed for TS2691 logic

* Added tests for TS2691
This commit is contained in:
Christian
2021-01-05 13:10:04 -06:00
committed by GitHub
parent 64d0538920
commit 7a5aadca69
11 changed files with 312 additions and 1 deletions

View File

@@ -3310,7 +3310,17 @@ namespace ts {
const tsExtension = tryExtractTSExtension(moduleReference);
if (tsExtension) {
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
error(errorNode, diag, tsExtension, removeExtension(moduleReference, tsExtension));
const importSourceWithoutExtension = removeExtension(moduleReference, tsExtension);
let replacedImportSource = importSourceWithoutExtension;
/**
* Direct users to import source with .js extension if outputting an ES module.
* @see https://github.com/microsoft/TypeScript/issues/42151
*/
const moduleKind = getEmitModuleKind(compilerOptions);
if (moduleKind >= ModuleKind.ES2015) {
replacedImportSource += ".js";
}
error(errorNode, diag, tsExtension, replacedImportSource);
}
else if (!compilerOptions.resolveJsonModule &&
fileExtensionIs(moduleReference, Extension.Json) &&

View File

@@ -0,0 +1,33 @@
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead.
tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead.
tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
==== tests/cases/compiler/x.ts (0 errors) ====
// CommonJS output
export default 0;
==== tests/cases/compiler/y.tsx (0 errors) ====
export default 0;
==== tests/cases/compiler/z.d.ts (0 errors) ====
declare const x: number;
export default x;
==== tests/cases/compiler/user.ts (3 errors) ====
import x from "./x.ts";
~~~~~~~~
!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead.
import y from "./y.tsx";
~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead.
import z from "./z.d.ts";
~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";
import z2 from "./z";

View File

@@ -0,0 +1,37 @@
//// [tests/cases/compiler/moduleResolutionNoTsCJS.ts] ////
//// [x.ts]
// CommonJS output
export default 0;
//// [y.tsx]
export default 0;
//// [z.d.ts]
declare const x: number;
export default x;
//// [user.ts]
import x from "./x.ts";
import y from "./y.tsx";
import z from "./z.d.ts";
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";
import z2 from "./z";
//// [x.js]
"use strict";
// CommonJS output
exports.__esModule = true;
exports["default"] = 0;
//// [y.jsx]
"use strict";
exports.__esModule = true;
exports["default"] = 0;
//// [user.js]
"use strict";
exports.__esModule = true;

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/x.ts ===
// CommonJS output
No type information for this code.
No type information for this code.export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/y.tsx ===
export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/z.d.ts ===
declare const x: number;
>x : Symbol(x, Decl(z.d.ts, 0, 13))
export default x;
>x : Symbol(x, Decl(z.d.ts, 0, 13))
=== tests/cases/compiler/user.ts ===
import x from "./x.ts";
>x : Symbol(x, Decl(user.ts, 0, 6))
import y from "./y.tsx";
>y : Symbol(y, Decl(user.ts, 1, 6))
import z from "./z.d.ts";
>z : Symbol(z, Decl(user.ts, 2, 6))
// Making sure the suggested fixes are valid:
import x2 from "./x";
>x2 : Symbol(x2, Decl(user.ts, 5, 6))
import y2 from "./y";
>y2 : Symbol(y2, Decl(user.ts, 6, 6))
import z2 from "./z";
>z2 : Symbol(z2, Decl(user.ts, 7, 6))

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/x.ts ===
// CommonJS output
No type information for this code.
No type information for this code.export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/y.tsx ===
export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/z.d.ts ===
declare const x: number;
>x : number
export default x;
>x : number
=== tests/cases/compiler/user.ts ===
import x from "./x.ts";
>x : any
import y from "./y.tsx";
>y : any
import z from "./z.d.ts";
>z : any
// Making sure the suggested fixes are valid:
import x2 from "./x";
>x2 : 0
import y2 from "./y";
>y2 : 0
import z2 from "./z";
>z2 : number

View File

@@ -0,0 +1,33 @@
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x.js' instead.
tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y.js' instead.
tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z.js' instead.
==== tests/cases/compiler/x.ts (0 errors) ====
// ESM output
export default 0;
==== tests/cases/compiler/y.tsx (0 errors) ====
export default 0;
==== tests/cases/compiler/z.d.ts (0 errors) ====
declare const x: number;
export default x;
==== tests/cases/compiler/user.ts (3 errors) ====
import x from "./x.ts";
~~~~~~~~
!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x.js' instead.
import y from "./y.tsx";
~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y.js' instead.
import z from "./z.d.ts";
~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z.js' instead.
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";
import z2 from "./z";

View File

@@ -0,0 +1,32 @@
//// [tests/cases/compiler/moduleResolutionNoTsESM.ts] ////
//// [x.ts]
// ESM output
export default 0;
//// [y.tsx]
export default 0;
//// [z.d.ts]
declare const x: number;
export default x;
//// [user.ts]
import x from "./x.ts";
import y from "./y.tsx";
import z from "./z.d.ts";
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";
import z2 from "./z";
//// [x.js]
// ESM output
export default 0;
//// [y.jsx]
export default 0;
//// [user.js]
export {};

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/x.ts ===
// ESM output
No type information for this code.
No type information for this code.export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/y.tsx ===
export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/z.d.ts ===
declare const x: number;
>x : Symbol(x, Decl(z.d.ts, 0, 13))
export default x;
>x : Symbol(x, Decl(z.d.ts, 0, 13))
=== tests/cases/compiler/user.ts ===
import x from "./x.ts";
>x : Symbol(x, Decl(user.ts, 0, 6))
import y from "./y.tsx";
>y : Symbol(y, Decl(user.ts, 1, 6))
import z from "./z.d.ts";
>z : Symbol(z, Decl(user.ts, 2, 6))
// Making sure the suggested fixes are valid:
import x2 from "./x";
>x2 : Symbol(x2, Decl(user.ts, 5, 6))
import y2 from "./y";
>y2 : Symbol(y2, Decl(user.ts, 6, 6))
import z2 from "./z";
>z2 : Symbol(z2, Decl(user.ts, 7, 6))

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/x.ts ===
// ESM output
No type information for this code.
No type information for this code.export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/y.tsx ===
export default 0;
No type information for this code.
No type information for this code.=== tests/cases/compiler/z.d.ts ===
declare const x: number;
>x : number
export default x;
>x : number
=== tests/cases/compiler/user.ts ===
import x from "./x.ts";
>x : any
import y from "./y.tsx";
>y : any
import z from "./z.d.ts";
>z : any
// Making sure the suggested fixes are valid:
import x2 from "./x";
>x2 : 0
import y2 from "./y";
>y2 : 0
import z2 from "./z";
>z2 : number

View File

@@ -0,0 +1,23 @@
// CommonJS output
// @module: commonjs
// @jsx: Preserve
// @filename: x.ts
export default 0;
// @filename: y.tsx
export default 0;
// @filename: z.d.ts
declare const x: number;
export default x;
// @filename: user.ts
import x from "./x.ts";
import y from "./y.tsx";
import z from "./z.d.ts";
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";
import z2 from "./z";

View File

@@ -1,3 +1,6 @@
// ESM output
// @module: es2015
// @jsx: Preserve
// @filename: x.ts
export default 0;