10000 Don't allow `.ts` to appear in an import · Pull Request #9646 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

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

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
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
Prev Previous commit
Next Next commit
Don't allow ".d.ts" extension in an import either.
  • Loading branch information
Andy Hanson committed Aug 3, 2016
commit 359c8b12ef3a8339940edd9203dcb2a10c08615b
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,10 +1366,10 @@ namespace ts {

if (moduleNotFoundError) {
// report errors only if it was requested
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));
const tsExtension = tryExtractTypeScriptExtension(moduleName);
if (tsExtension) {
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
error(moduleReferenceLiteral, diag, tsExtension, removeExtension(moduleName, tsExtension));
}
else {
error(moduleReferenceLiteral, moduleNotFoundError, moduleName);
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,7 @@ namespace ts {
/**
* List of supported extensions in order of file resolution precedence.
*/
export const supportedTypeScriptExtensionsNonDts = [".ts", ".tsx"];
export const supportedTypeScriptExtensions = supportedTypeScriptExtensionsNonDts.concat([".d.ts"]);
export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"];
export const supportedJavascriptExtensions = [".js", ".jsx"];
const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions);

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
},
"An import path should not end with a '{0}' extension. Consider importing '{1}' instead.": {
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": {
"category": "Error",
"code": 2691
},
Expand Down
16 changes: 4 additions & 12 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,7 @@ namespace ts {
* in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations.
*/
function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
// If the candidate already has an extension load that or quit.
if (hasTypeScriptFileExtension(candidate)) {
// Don't allow `.ts` to appear at the end
if (!fileExtensionIs(candidate, ".d.ts")) {
return undefined;
}
return tryFile(candidate, failedLookupLocation, onlyRecordFailures, state);
}

// Next, try adding an extension.
// We don't allow an import of "foo.ts" to be matched by "foo.ts.ts", but we do allow "foo.js" to be matched by "foo.js.ts".
// First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts"
Copy link
Member

Choose a reason for hiding this comment

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

do we know that candidate is extensionless already? tryAddingExtensions looks like it blindly adds ext to the end of candidate

Copy link
Author

Choose a reason for hiding this comment

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

That's right, so if someone imports "foo.bar" they can get "foo.bar.ts".

const resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state);
if (resolvedByAddingExtension) {
return resolvedByAddingExtension;
Expand Down Expand Up @@ -736,7 +726,9 @@ namespace ts {
}
const typesFile = tryReadTypesSection(packageJsonPath, candidate, state);
if (typesFile) {
const result = loadModuleFromFile(typesFile, extensions, failedLookupLocation, !directoryProbablyExists(getDirectoryPath(typesFile), state.host), state);
const onlyRecordFailures = !directoryProbablyExists(getDirectoryPath(typesFile), state.host);
// The package.json "typings" property must specify the file with extension, so just try that exact filename.
const result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures, state);
if (result) {
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2727,9 +2727,11 @@ namespace ts {
}

/** 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));
export function tryExtractTypeScriptExtension(fileName: string): string | undefined {
return find(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension));
}
// Must have '.d.ts' first because if '.ts' goes first, that will be detected as the extension instead of '.d.ts'.
const supportedTypescriptExtensionsWithDtsFirst = supportedTypeScriptExtensions.slice().reverse();
Copy link
Member

Choose a reason for hiding this comment

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

why not just change supportedTypeScriptExtensions ?

Copy link
Author

Choose a reason for hiding this comment

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

supportedTypeScriptExtensions is in order of file resolution precedence, meaning we want .d.ts to be tried last.

Copy link
Member

Choose a reason for hiding this comment

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

Seems like something you probably want to make explicit in case anyone relies on that behavior.

Copy link
Author

Choose a reason for hiding this comment

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

supportedTypescriptExtensions currently has a comment on it saying that it's in file resolution precedence. What else could we add?
I might want to change this code to just duplicate the list, though, because if anyone ever changes the order of supportedTypescriptExtensions, this one will no longer have .d.ts first.

Copy link
Member

Choose a reason for hiding this comment

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

The fact that the list is in resolution order is independent of the fact that you're trying to give the longer extension a higher priority. Does that make sense?


/**
* Replace each instance of non-ascii characters by one, two, three, or four escape sequences
Expand Down
2 changes: 1 addition & 1 deletion src/harness/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CompilerBaselineRunner extends RunnerBase {
private makeUnitName(name: string, root: string) {
const path = ts.toPath(name, root, (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
return path.replace(pathStart, "/");
return pathStart === "" ? path : path.replace(pathStart, "/");
};

public checkTestCodeOutput(fileName: string) {
Expand Down
32 changes: 19 additions & 13 deletions tests/baselines/reference/moduleResolutionNoTs.errors.txt
10000
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
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,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/user.ts (2 errors) ====
// '.ts' extension is OK in a reference
///<reference path="./x.ts"/>
==== tests/cases/compiler/x.ts (0 errors) ====
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 should not end with a '.ts' extension. Consider importing './x' instead.
!!! 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 should not end with a '.tsx' extension. Consider importing './y' instead.
!!! 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";

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

==== tests/cases/compiler/y.tsx (0 errors) ====
export default 0;
import z2 from "./z";

11 changes: 6 additions & 5 deletions tests/baselines/reference/moduleResolutionNoTs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ export default 0;
//// [y.tsx]
export default 0;

//// [z.d.ts]
declare const x: number;
export default x;

//// [user.ts]
// '.ts' extension is OK in a reference
///<reference path="./x.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]
Expand All @@ -27,6 +30,4 @@ exports["default"] = 0;
exports.__esModule = true;
exports["default"] = 0;
//// [user.js]
// '.ts' extension is OK in a reference
///<reference path="./x.ts"/>
"use strict";
9 changes: 6 additions & 3 deletions tests/cases/compiler/moduleResolutionNoTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ export default 0;
// @filename: y.tsx
export default 0;

// @filename: user.ts
// '.ts' extension is OK in a reference
///<reference path="./x.ts"/>
// @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";
0