Skip to content
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

Don't allow .ts to appear in an import #9646

Merged
10 commits merged into from
Aug 19, 2016
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
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,8 +1366,10 @@ namespace ts {

if (moduleNotFoundError) {
// report errors only if it was requested
if (hasTypeScriptFileExtensionNonDts(moduleName)) {
error(moduleReferenceLiteral, Diagnostics.Module_name_should_not_include_a_ts_extension_Colon_0, moduleName);
const nonDtsExtension = tryExtractTypeScriptExtensionNonDts(moduleName);
if (nonDtsExtension) {
const diag = Diagnostics.An_import_path_should_not_end_with_a_0_extension_Consider_importing_1_instead;
error(moduleReferenceLiteral, diag, nonDtsExtension, removeExtension(moduleName, nonDtsExtension));
}
else {
error(moduleReferenceLiteral, moduleNotFoundError, moduleName);
Expand Down
21 changes: 18 additions & 3 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ namespace ts {
return undefined;
}

/** Works like Array.prototype.find. */
export function find<T>(array: T[], predicate: (element: T, index: number) => boolean): T | undefined {
for (let i = 0, len = array.length; i < len; i++) {
const value = array[i];
if (predicate(value, i)) {
return value;
}
}
return undefined;
}

export function contains<T>(array: T[], value: T): boolean {
if (array) {
for (const v of array) {
Expand Down Expand Up @@ -941,7 +952,7 @@ namespace ts {
* [^./] # matches everything up to the first . character (excluding directory seperators)
* (\\.(?!min\\.js$))? # matches . characters but not if they are part of the .min.js file extension
*/
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentOther = "[^/]*";

export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") {
Expand Down Expand Up @@ -1271,8 +1282,12 @@ namespace ts {
return path;
}

export function tryRemoveExtension(path: string, extension: string): string {
return fileExtensionIs(path, extension) ? path.substring(0, path.length - extension.length) : undefined;
export function tryRemoveExtension(path: string, extension: string): string | undefined {
return fileExtensionIs(path, extension) ? removeExtension(path, extension) : undefined;
}

export function removeExtension(path: string, extension: string): string {
return path.substring(0, path.length - extension.length);
}

export function isJsxOrTsxExtension(ext: string): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,7 @@
"category": "Error",
"code": 2690
},
"Module name should not include a '.ts' extension: '{0}'.": {
"An import path should not end with a '{0}' extension. Consider importing '{1}' instead.": {
Copy link
Member

Choose a reason for hiding this comment

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

"cannot end with"

"category": "Error",
"code": 2691
},
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2726,8 +2726,9 @@ namespace ts {
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension));
}

export function hasTypeScriptFileExtensionNonDts(fileName: string) {
return forEach(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension));
/** Return ".ts" or ".tsx" if that is the extension. */
Copy link
Member

Choose a reason for hiding this comment

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

".d.ts"?

export function tryExtractTypeScriptExtensionNonDts(fileName: string): string | undefined {
return find(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension));
Copy link
Member

Choose a reason for hiding this comment

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

Don't you want to give the same error if you use .d.ts as well?

Copy link
Member

Choose a reason for hiding this comment

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

Also, can you add a test that imports from a file ending in .d.ts?

Copy link
Author

Choose a reason for hiding this comment

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

If people add a .d.ts extension they may be doing unnecessary work, but it won't cause runtime errors because declaration references don't translate to imports in the output. In contrast, a .ts extension will either fail to import (because the output file has a .js extension) or ask the JS engine to load typescript code.

Copy link
Member

@DanielRosenwasser DanielRosenwasser Aug 2, 2016

Choose a reason for hiding this comment

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

If people add a .d.ts extension they may be doing unnecessary work, but it won't cause runtime errors because declaration references don't translate to imports in the output.

.d.ts imports don't just get elided. If you use any imported entities as values, then we still emit the import itself. Try it out:

foo.d.ts

export declare var foo;

bar.ts

import * as v from "./foo.d.ts";

v.foo;

The current emit for bar.js is

"use strict";
var v = require("./foo.d.ts");
v.foo;

}

/**
Expand Down
23 changes: 17 additions & 6 deletions tests/baselines/reference/moduleResolutionNoTs.errors.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
tests/cases/compiler/user.ts(4,15): error TS2690: Module name should not include a '.ts' extension: './m.ts'.
tests/cases/compiler/user.ts(4,15): error TS2691: An import path should not end with a '.ts' extension. Consider importing './x' instead.
tests/cases/compiler/user.ts(5,15): error TS2691: An import path should not end with a '.tsx' extension. Consider importing './y' instead.


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

import x from "./m.ts";
import x from "./x.ts";
~~~~~~~~
!!! error TS2690: Module name should not include a '.ts' extension: './m.ts'.
!!! error TS2691: An import path should not end with a '.ts' extension. Consider importing './x' instead.
import y from "./y.tsx";
~~~~~~~~~
!!! error TS2691: An import path should not end with a '.tsx' extension. Consider importing './y' instead.

==== tests/cases/compiler/m.ts (0 errors) ====
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";

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

==== tests/cases/compiler/y.tsx (0 errors) ====
export default 0;

22 changes: 17 additions & 5 deletions tests/baselines/reference/moduleResolutionNoTs.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
//// [tests/cases/compiler/moduleResolutionNoTs.ts] ////

//// [m.ts]
//// [x.ts]
export default 0;

//// [y.tsx]
export default 0;

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

import x from "./x.ts";
import y from "./y.tsx";

import x from "./m.ts";
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";


//// [m.js]
//// [x.js]
"use strict";
exports.__esModule = true;
exports["default"] = 0;
//// [y.js]
"use strict";
exports.__esModule = true;
exports["default"] = 0;
//// [user.js]
// '.ts' extension is OK in a reference
///<reference path="./m.ts"/>
///<reference path="./x.ts"/>
"use strict";
14 changes: 11 additions & 3 deletions tests/cases/compiler/moduleResolutionNoTs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// @filename: m.ts
// @filename: x.ts
export default 0;

// @filename: y.tsx
export default 0;

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

import x from "./x.ts";
import y from "./y.tsx";

import x from "./m.ts";
// Making sure the suggested fixes are valid:
import x2 from "./x";
import y2 from "./y";