From 97f87f348c23ba826d1e8c7cd64142774dbc5eb6 Mon Sep 17 00:00:00 2001 From: Christian Lewis Date: Sun, 3 Jan 2021 02:26:04 -0600 Subject: [PATCH 1/3] Adjust TS2691 message for .ts import sources --- src/compiler/checker.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6c240dce41291..74f922062dc92 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3309,7 +3309,18 @@ 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 target = getEmitScriptTarget(compilerOptions); + const moduleKind = getEmitModuleKind(compilerOptions); + if (target >= ScriptTarget.ES2015 && moduleKind >= ModuleKind.ES2015) { + replacedImportSource += ".js"; + } + error(errorNode, diag, tsExtension, replacedImportSource); } else if (!compilerOptions.resolveJsonModule && fileExtensionIs(moduleReference, Extension.Json) && From 72145bb57d25fc44e84c1a78a6a58b34dd756b6a Mon Sep 17 00:00:00 2001 From: Christian Lewis Date: Mon, 4 Jan 2021 15:18:17 -0600 Subject: [PATCH 2/3] Only ModuleKind is needed for TS2691 logic --- src/compiler/checker.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 74f922062dc92..e8325816c14a8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3315,9 +3315,8 @@ namespace ts { * Direct users to import source with .js extension if outputting an ES module. * @see https://github.com/microsoft/TypeScript/issues/42151 */ - const target = getEmitScriptTarget(compilerOptions); const moduleKind = getEmitModuleKind(compilerOptions); - if (target >= ScriptTarget.ES2015 && moduleKind >= ModuleKind.ES2015) { + if (moduleKind >= ModuleKind.ES2015) { replacedImportSource += ".js"; } error(errorNode, diag, tsExtension, replacedImportSource); From f34e5e49c8a04da8b32f678f6e23b663ae8943f8 Mon Sep 17 00:00:00 2001 From: Christian Lewis Date: Mon, 4 Jan 2021 18:33:56 -0600 Subject: [PATCH 3/3] Added tests for TS2691 --- .../moduleResolutionNoTsCJS.errors.txt | 33 +++++++++++++++++ .../reference/moduleResolutionNoTsCJS.js | 37 +++++++++++++++++++ .../reference/moduleResolutionNoTsCJS.symbols | 35 ++++++++++++++++++ .../reference/moduleResolutionNoTsCJS.types | 35 ++++++++++++++++++ .../moduleResolutionNoTsESM.errors.txt | 33 +++++++++++++++++ .../reference/moduleResolutionNoTsESM.js | 32 ++++++++++++++++ .../reference/moduleResolutionNoTsESM.symbols | 35 ++++++++++++++++++ .../reference/moduleResolutionNoTsESM.types | 35 ++++++++++++++++++ .../cases/compiler/moduleResolutionNoTsCJS.ts | 23 ++++++++++++ ...tionNoTs.ts => moduleResolutionNoTsESM.ts} | 3 ++ 10 files changed, 301 insertions(+) create mode 100644 tests/baselines/reference/moduleResolutionNoTsCJS.errors.txt create mode 100644 tests/baselines/reference/moduleResolutionNoTsCJS.js create mode 100644 tests/baselines/reference/moduleResolutionNoTsCJS.symbols create mode 100644 tests/baselines/reference/moduleResolutionNoTsCJS.types create mode 100644 tests/baselines/reference/moduleResolutionNoTsESM.errors.txt create mode 100644 tests/baselines/reference/moduleResolutionNoTsESM.js create mode 100644 tests/baselines/reference/moduleResolutionNoTsESM.symbols create mode 100644 tests/baselines/reference/moduleResolutionNoTsESM.types create mode 100644 tests/cases/compiler/moduleResolutionNoTsCJS.ts rename tests/cases/compiler/{moduleResolutionNoTs.ts => moduleResolutionNoTsESM.ts} (86%) diff --git a/tests/baselines/reference/moduleResolutionNoTsCJS.errors.txt b/tests/baselines/reference/moduleResolutionNoTsCJS.errors.txt new file mode 100644 index 0000000000000..0da402020af80 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsCJS.errors.txt @@ -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"; + \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionNoTsCJS.js b/tests/baselines/reference/moduleResolutionNoTsCJS.js new file mode 100644 index 0000000000000..53c6503cfbd57 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsCJS.js @@ -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; diff --git a/tests/baselines/reference/moduleResolutionNoTsCJS.symbols b/tests/baselines/reference/moduleResolutionNoTsCJS.symbols new file mode 100644 index 0000000000000..c71f18a0790a1 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsCJS.symbols @@ -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)) + diff --git a/tests/baselines/reference/moduleResolutionNoTsCJS.types b/tests/baselines/reference/moduleResolutionNoTsCJS.types new file mode 100644 index 0000000000000..419180d79761a --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsCJS.types @@ -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 + diff --git a/tests/baselines/reference/moduleResolutionNoTsESM.errors.txt b/tests/baselines/reference/moduleResolutionNoTsESM.errors.txt new file mode 100644 index 0000000000000..6c067f2a5c571 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsESM.errors.txt @@ -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"; + \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionNoTsESM.js b/tests/baselines/reference/moduleResolutionNoTsESM.js new file mode 100644 index 0000000000000..69c33d3faa3e4 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsESM.js @@ -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 {}; diff --git a/tests/baselines/reference/moduleResolutionNoTsESM.symbols b/tests/baselines/reference/moduleResolutionNoTsESM.symbols new file mode 100644 index 0000000000000..eaf61024fb0e8 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsESM.symbols @@ -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)) + diff --git a/tests/baselines/reference/moduleResolutionNoTsESM.types b/tests/baselines/reference/moduleResolutionNoTsESM.types new file mode 100644 index 0000000000000..199a513549de4 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTsESM.types @@ -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 + diff --git a/tests/cases/compiler/moduleResolutionNoTsCJS.ts b/tests/cases/compiler/moduleResolutionNoTsCJS.ts new file mode 100644 index 0000000000000..d3d67538bddfd --- /dev/null +++ b/tests/cases/compiler/moduleResolutionNoTsCJS.ts @@ -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"; diff --git a/tests/cases/compiler/moduleResolutionNoTs.ts b/tests/cases/compiler/moduleResolutionNoTsESM.ts similarity index 86% rename from tests/cases/compiler/moduleResolutionNoTs.ts rename to tests/cases/compiler/moduleResolutionNoTsESM.ts index fc92d72946432..9d9e2aef73615 100644 --- a/tests/cases/compiler/moduleResolutionNoTs.ts +++ b/tests/cases/compiler/moduleResolutionNoTsESM.ts @@ -1,3 +1,6 @@ +// ESM output +// @module: es2015 + // @jsx: Preserve // @filename: x.ts export default 0;