Skip to content

Commit

Permalink
Add specific error message for unwanted '.ts' extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed Jul 12, 2016
1 parent f59b3a5 commit c6d91f9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,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 @@ -1134,7 +1134,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"];
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}'.": {
"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";

0 comments on commit c6d91f9

Please sign in to comment.