-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Changes from 6 commits
448a480
a8c05a9
2821d98
0f134ed
359c8b1
3de8c22
2eb159e
8fc17af
297cb50
b452469
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -661,51 +661,52 @@ namespace ts { | |
* @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary | ||
* 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 { | ||
// First try to keep/add an extension: importing "./foo.ts" can be matched by a file "./foo.ts", and "./foo" by "./foo.d.ts" | ||
const resolvedByAddingOrKeepingExtension = loadModuleFromFileWorker(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); | ||
if (resolvedByAddingOrKeepingExtension) { | ||
return resolvedByAddingOrKeepingExtension; | ||
function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { | ||
// First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we know that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
// Then try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one, e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" | ||
|
||
// If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one; | ||
// e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" | ||
if (hasJavaScriptFileExtension(candidate)) { | ||
const extensionless = removeFileExtension(candidate); | ||
if (state.traceEnabled) { | ||
const extension = candidate.substring(extensionless.length); | ||
trace(state.host, Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); | ||
} | ||
return loadModuleFromFileWorker(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); | ||
return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); | ||
} | ||
} | ||
|
||
function loadModuleFromFileWorker(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string { | ||
/** Try to return an existing file that adds one of the `extensions` to `candidate`. */ | ||
function tryAddingExtensions(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { | ||
if (!onlyRecordFailures) { | ||
// check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing | ||
const directory = getDirectoryPath(candidate); | ||
if (directory) { | ||
onlyRecordFailures = !directoryProbablyExists(directory, state.host); | ||
} | ||
} | ||
return forEach(extensions, tryLoad); | ||
return forEach(extensions, ext => | ||
!(state.skipTsx && isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state)); | ||
} | ||
|
||
function tryLoad(ext: string): string { | ||
if (state.skipTsx && isJsxOrTsxExtension(ext)) { | ||
return undefined; | ||
} | ||
const fileName = fileExtensionIs(candidate, ext) ? candidate : candidate + ext; | ||
if (!onlyRecordFailures && state.host.fileExists(fileName)) { | ||
if (state.traceEnabled) { | ||
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); | ||
} | ||
return fileName; | ||
/** Return the file if it exists. */ | ||
function tryFile(fileName: string, failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { | ||
if (!onlyRecordFailures && state.host.fileExists(fileName)) { | ||
if (state.traceEnabled) { | ||
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); | ||
} | ||
else { | ||
if (state.traceEnabled) { | ||
trace(state.host, Diagnostics.File_0_does_not_exist, fileName); | ||
} | ||
failedLookupLocation.push(fileName); | ||
return undefined; | ||
return fileName; | ||
} | ||
else { | ||
if (state.traceEnabled) { | ||
trace(state.host, Diagnostics.File_0_does_not_exist, fileName); | ||
} | ||
failedLookupLocation.push(fileName); | ||
return undefined; | ||
} | ||
} | ||
|
||
|
@@ -718,7 +719,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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2713,6 +2713,13 @@ namespace ts { | |
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension)); | ||
} | ||
|
||
/** Return ".ts" or ".tsx" if that is the extension. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ".d.ts"? |
||
export function tryExtractTypeScriptExtension(fileName: string): string | undefined { | ||
return tryFind(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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
* representing the UTF-8 encoding of the character, and return the expanded char code list. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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/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 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"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//// [tests/cases/compiler/moduleResolutionNoTs.ts] //// | ||
|
||
//// [x.ts] | ||
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"; | ||
exports.__esModule = true; | ||
exports["default"] = 0; | ||
//// [y.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = 0; | ||
//// [user.js] | ||
"use strict"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// @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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I expected
find
to have the type thattryFind
has right now. Why doesfind
takecallback
and notpredicate
?I forgot to mention it when I ran into it last week while trying to use
find
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we rename
tryFind
tofind
, what should we callfind
?mustFind
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findMap
? Let me go see if this function is in hoogle...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the equivalent of bind (
>>=
) or something likeData.Foldable.concatMap
except that it takesArray<T>
toException<T>
. So, yeah,findMap
is not bad, ormapFirst
ormapSingle
.