-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Allow an import of "foo.js" to be matched by a file "foo.ts" #8895
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
Changes from 1 commit
fa16a99
a0546a9
f4d6b67
d4b8889
9179f4c
9575b3c
a918730
704f987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -861,6 +861,13 @@ namespace ts { | |
| return fileExtensionIs(path, extension) ? path.substring(0, path.length - extension.length) : undefined; | ||
| } | ||
|
|
||
| export function getFileExtension(path: string): string { | ||
| const dot = path.lastIndexOf("."); | ||
|
||
| if (dot !== -1) { | ||
| return path.substring(dot); | ||
| } | ||
| } | ||
|
|
||
| export interface ObjectAllocator { | ||
| getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node; | ||
| getSourceFileConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => SourceFile; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -615,11 +615,26 @@ namespace ts { | |
| } | ||
|
|
||
| /** | ||
| * @param extensions - Either supportedTypeScriptExtensions or if --allowJs, allSupportedExtensions | ||
| * @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 keepOrAddExtension = loadModuleFromFileWorker(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); | ||
| if (keepOrAddExtension) { | ||
|
||
| return keepOrAddExtension; | ||
| } | ||
| // 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 (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); | ||
| } | ||
| } | ||
| function loadModuleFromFileWorker(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string { | ||
|
Member
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. Newline above. |
||
| if (!onlyRecordFailures) { | ||
| // check if containig folder exists - if it doesn't then just record failures for all supported extensions without disk probing | ||
|
||
| const directory = getDirectoryPath(candidate); | ||
|
|
@@ -628,27 +643,12 @@ namespace ts { | |
| } | ||
| } | ||
|
|
||
| // 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 keepOrAddExtension = forEach(extensions, ext => { | ||
| return forEach(extensions, ext => { | ||
| if (state.skipTsx && (ext === ".jsx" || ext === ".tsx")) { | ||
|
||
| return; | ||
| } | ||
| return tryLoad(fileExtensionIs(candidate, ext) ? candidate : candidate + ext); | ||
| }); | ||
| if (keepOrAddExtension) { | ||
| return keepOrAddExtension; | ||
| } | ||
|
|
||
| // 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" | ||
| return forEach(supportedJavascriptExtensions, jsExt => { | ||
| if (state.skipTsx && jsExt === ".jsx") { | ||
| return; | ||
| } | ||
| const extensionless = tryRemoveExtension(candidate, jsExt); | ||
| if (extensionless !== undefined) { | ||
| return forEach(supportedTypeScriptExtensions, tsExt => tryLoad(extensionless + tsExt)); | ||
| } | ||
| }); | ||
|
|
||
| function tryLoad(fileName: string): string { | ||
| if (!onlyRecordFailures && state.host.fileExists(fileName)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [ | ||
| "======== Resolving module './a' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/b.ts'. ========", | ||
| "Module resolution kind is not specified, using 'NodeJs'.", | ||
| "Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/a'.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.", | ||
| "======== Module name './a' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'. ========", | ||
| "======== Resolving module './a.ts' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/c.ts'. ========", | ||
| "Module resolution kind is not specified, using 'NodeJs'.", | ||
| "Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.", | ||
| "======== Module name './a.ts' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'. ========", | ||
| "======== Resolving module './a.js' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/d.ts'. ========", | ||
| "Module resolution kind is not specified, using 'NodeJs'.", | ||
| "Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js'.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js.ts' does not exist.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js.tsx' does not exist.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js.d.ts' does not exist.", | ||
| "File name 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js' has a '.js' extension - stripping it", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.", | ||
| "======== Module name './a.js' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'. ========", | ||
| "======== Resolving module './jquery.js' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery_user_1.ts'. ========", | ||
| "Module resolution kind is not specified, using 'NodeJs'.", | ||
| "Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js'.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js.ts' does not exist.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js.tsx' does not exist.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js.d.ts' does not exist.", | ||
| "File name 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js' has a '.js' extension - stripping it", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.ts' does not exist.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.tsx' does not exist.", | ||
| "File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.d.ts' exist - use it as a name resolution result.", | ||
| "======== Module name './jquery.js' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.d.ts'. ========" | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // @traceResolution: true | ||
| // @Filename: a.ts | ||
| export default 0; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Consider making the return type annotation
string | undefined