Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add specific error message for unwanted '.ts' extension
  • Loading branch information
Andy Hanson committed Jul 21, 2016
commit a8c05a98e98b0e2f7d50d983ac08d385d6dd3641
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,12 @@ namespace ts {

if (moduleNotFoundError) {
// report errors only if it was requested
error(moduleReferenceLiteral, moduleNotFoundError, moduleName);
if (hasTypeScriptFileExtensionNonDts(moduleName)) {
error(moduleReferenceLiteral, Diagnostics.Module_name_should_not_include_a_ts_extension_Colon_0, moduleName);
}
else {
error(moduleReferenceLiteral, moduleNotFoundError, moduleName);
}
}
return undefined;
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,8 @@ namespace ts {
/**
* List of supported extensions in order of file resolution precedence.
*/
export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"];
export const supportedTypeScriptExtensionsNonDts = [".ts", ".tsx"];
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you'll need this if you correctly support errors for importing .d.ts files

export const supportedTypeScriptExtensions = supportedTypeScriptExtensionsNonDts.concat([".d.ts"]);
export const supportedJavascriptExtensions = [".js", ".jsx"];
const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions);

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,10 @@
"category": "Error",
"code": 2689
},
"Module name should not include a '.ts' extension: '{0}'.": {
Copy link
Member

Choose a reason for hiding this comment

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

Try making this

An import path should not include a '.ts' extension. Consider importing '{0}' instead.

Copy link
Member

Choose a reason for hiding this comment

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

An import path should not end with a '{0}' extension. Consider importing '{1}' instead.

Since this error is being used for .tsx as well.

"category": "Error",
"code": 2690
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,10 @@ namespace ts {
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension));
}

export function hasTypeScriptFileExtensionNonDts(fileName: string) {
return forEach(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension));
}

/**
* Replace each instance of non-ascii characters by one, two, three, or four escape sequences
* representing the UTF-8 encoding of the character, and return the expanded char code list.
Expand Down
13 changes: 8 additions & 5 deletions tests/baselines/reference/moduleResolutionNoTs.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
tests/cases/compiler/user.ts(1,15): error TS2307: Cannot find module './m.ts'.
tests/cases/compiler/user.ts(4,15): error TS2690: Module name should not include a '.ts' extension: './m.ts'.


==== tests/cases/compiler/m.ts (0 errors) ====
export default 0;

==== tests/cases/compiler/user.ts (1 errors) ====
// '.ts' extension is OK in a reference
///<reference path="./m.ts"/>

import x from "./m.ts";
~~~~~~~~
!!! error TS2307: Cannot find module './m.ts'.
!!! error TS2690: Module name should not include a '.ts' extension: './m.ts'.

==== tests/cases/compiler/m.ts (0 errors) ====
export default 0;

5 changes: 5 additions & 0 deletions tests/baselines/reference/moduleResolutionNoTs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
export default 0;

//// [user.ts]
// '.ts' extension is OK in a reference
///<reference path="./m.ts"/>

import x from "./m.ts";


Expand All @@ -12,4 +15,6 @@ import x from "./m.ts";
exports.__esModule = true;
exports["default"] = 0;
//// [user.js]
// '.ts' extension is OK in a reference
///<reference path="./m.ts"/>
"use strict";
3 changes: 3 additions & 0 deletions tests/cases/compiler/moduleResolutionNoTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
export default 0;

// @filename: user.ts
// '.ts' extension is OK in a reference
///<reference path="./m.ts"/>

import x from "./m.ts";