Skip to content

feat: adjust TS2691 message for .ts import sources #42184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3309,7 +3309,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) &&
Expand Down
33 changes: 33 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsCJS.errors.txt
Original file line number Diff line number Diff line change
@@ -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.
Comment on lines +1 to +3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change for CJS module target.



==== 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";

37 changes: 37 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsCJS.js
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 35 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsCJS.symbols
Original file line number Diff line number Diff line change
@@ -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))

35 changes: 35 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsCJS.types
Original file line number Diff line number Diff line change
@@ -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

33 changes: 33 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsESM.errors.txt
Original file line number Diff line number Diff line change
@@ -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.
Comment on lines +1 to +3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change for ES module target.



==== 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";

32 changes: 32 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsESM.js
Original file line number Diff line number Diff line change
@@ -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 {};
35 changes: 35 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsESM.symbols
Original file line number Diff line number Diff line change
@@ -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))

35 changes: 35 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTsESM.types
Original file line number Diff line number Diff line change
@@ -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

23 changes: 23 additions & 0 deletions tests/cases/compiler/moduleResolutionNoTsCJS.ts
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// ESM output
// @module: es2015

// @jsx: Preserve
// @filename: x.ts
export default 0;
Expand Down