-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow an import of "foo.js" to be matched by a file "foo.ts" #8895
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa16a99
Allow an import of "foo.js" to be matched by a file "foo.ts"
a0546a9
Improve loadModuleFromFile code
f4d6b67
Respond to PR comments
d4b8889
Respond to more PR comments
9179f4c
Fix test
9575b3c
Revert to old tryLoad implementation
a918730
factor out isJsxOrTsxExtension
704f987
Move to a conformance test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -619,8 +619,25 @@ 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 { | ||
// 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; | ||
} | ||
// 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 { | ||
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 | ||
// 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); | ||
|
@@ -629,7 +646,7 @@ namespace ts { | |
return forEach(extensions, tryLoad); | ||
|
||
function tryLoad(ext: string): string { | ||
if (ext === ".tsx" && state.skipTsx) { | ||
if (state.skipTsx && isJsxOrTsxExtension(ext)) { | ||
return undefined; | ||
} | ||
const fileName = fileExtensionIs(candidate, ext) ? candidate : candidate + ext; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/moduleResolutionWithExtensions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//// [tests/cases/conformance/externalModules/moduleResolutionWithExtensions.ts] //// | ||
|
||
//// [a.ts] | ||
|
||
export default 0; | ||
|
||
// No extension: '.ts' added | ||
//// [b.ts] | ||
import a from './a'; | ||
|
||
// Matching extension | ||
//// [c.ts] | ||
import a from './a.ts'; | ||
|
||
// '.js' extension: stripped and replaced with '.ts' | ||
//// [d.ts] | ||
import a from './a.js'; | ||
|
||
//// [jquery.d.ts] | ||
declare var x: number; | ||
export default x; | ||
|
||
// No extension: '.d.ts' added | ||
//// [jquery_user_1.ts] | ||
import j from "./jquery"; | ||
|
||
// '.js' extension: stripped and replaced with '.d.ts' | ||
//// [jquery_user_1.ts] | ||
import j from "./jquery.js" | ||
|
||
|
||
//// [a.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = 0; | ||
// No extension: '.ts' added | ||
//// [b.js] | ||
"use strict"; | ||
// Matching extension | ||
//// [c.js] | ||
"use strict"; | ||
// '.js' extension: stripped and replaced with '.ts' | ||
//// [d.js] | ||
"use strict"; | ||
//// [jquery_user_1.js] | ||
"use strict"; |
37 changes: 37 additions & 0 deletions
37
tests/baselines/reference/moduleResolutionWithExtensions.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
=== /src/a.ts === | ||
|
||
No type information for this code.export default 0; | ||
No type information for this code. | ||
No type information for this code.// No extension: '.ts' added | ||
No type information for this code.=== /src/b.ts === | ||
import a from './a'; | ||
>a : Symbol(a, Decl(b.ts, 0, 6)) | ||
|
||
// Matching extension | ||
=== /src/c.ts === | ||
import a from './a.ts'; | ||
>a : Symbol(a, Decl(c.ts, 0, 6)) | ||
|
||
// '.js' extension: stripped and replaced with '.ts' | ||
=== /src/d.ts === | ||
import a from './a.js'; | ||
>a : Symbol(a, Decl(d.ts, 0, 6)) | ||
|
||
=== /src/jquery.d.ts === | ||
declare var x: number; | ||
>x : Symbol(x, Decl(jquery.d.ts, 0, 11)) | ||
|
||
export default x; | ||
>x : Symbol(x, Decl(jquery.d.ts, 0, 11)) | ||
|
||
// No extension: '.d.ts' added | ||
=== /src/jquery_user_1.ts === | ||
import j from "./jquery"; | ||
>j : Symbol(j, Decl(jquery_user_1.ts, 0, 6)) | ||
|
||
// '.js' extension: stripped and replaced with '.d.ts' | ||
=== /src/jquery_user_1.ts === | ||
import j from "./jquery.js" | ||
>j : Symbol(j, Decl(jquery_user_1.ts, 0, 6)) | ||
>j : Symbol(j, Decl(jquery_user_1.ts, 0, 6)) | ||
|
32 changes: 32 additions & 0 deletions
32
tests/baselines/reference/moduleResolutionWithExtensions.trace.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[ | ||
"======== Resolving module './a' from '/src/b.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module as file / folder, candidate module location '/src/a'.", | ||
"File '/src/a.ts' exist - use it as a name resolution result.", | ||
"======== Module name './a' was successfully resolved to '/src/a.ts'. ========", | ||
"======== Resolving module './a.ts' from '/src/c.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module as file / folder, candidate module location '/src/a.ts'.", | ||
"File '/src/a.ts' exist - use it as a name resolution result.", | ||
"======== Module name './a.ts' was successfully resolved to '/src/a.ts'. ========", | ||
"======== Resolving module './a.js' from '/src/d.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module as file / folder, candidate module location '/src/a.js'.", | ||
"File '/src/a.js.ts' does not exist.", | ||
"File '/src/a.js.tsx' does not exist.", | ||
"File '/src/a.js.d.ts' does not exist.", | ||
"File name '/src/a.js' has a '.js' extension - stripping it", | ||
"File '/src/a.ts' exist - use it as a name resolution result.", | ||
"======== Module name './a.js' was successfully resolved to '/src/a.ts'. ========", | ||
"======== Resolving module './jquery.js' from '/src/jquery_user_1.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module as file / folder, candidate module location '/src/jquery.js'.", | ||
"File '/src/jquery.js.ts' does not exist.", | ||
"File '/src/jquery.js.tsx' does not exist.", | ||
"File '/src/jquery.js.d.ts' does not exist.", | ||
"File name '/src/jquery.js' has a '.js' extension - stripping it", | ||
"File '/src/jquery.ts' does not exist.", | ||
"File '/src/jquery.tsx' does not exist.", | ||
"File '/src/jquery.d.ts' exist - use it as a name resolution result.", | ||
"======== Module name './jquery.js' was successfully resolved to '/src/jquery.d.ts'. ========" | ||
] |
37 changes: 37 additions & 0 deletions
37
tests/baselines/reference/moduleResolutionWithExtensions.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
=== /src/a.ts === | ||
|
||
No type information for this code.export default 0; | ||
No type information for this code. | ||
No type information for this code.// No extension: '.ts' added | ||
No type information for this code.=== /src/b.ts === | ||
import a from './a'; | ||
>a : number | ||
|
||
// Matching extension | ||
=== /src/c.ts === | ||
import a from './a.ts'; | ||
>a : number | ||
|
||
// '.js' extension: stripped and replaced with '.ts' | ||
=== /src/d.ts === | ||
import a from './a.js'; | ||
>a : number | ||
|
||
=== /src/jquery.d.ts === | ||
declare var x: number; | ||
>x : number | ||
|
||
export default x; | ||
>x : number | ||
|
||
// No extension: '.d.ts' added | ||
=== /src/jquery_user_1.ts === | ||
import j from "./jquery"; | ||
>j : number | ||
|
||
// '.js' extension: stripped and replaced with '.d.ts' | ||
=== /src/jquery_user_1.ts === | ||
import j from "./jquery.js" | ||
>j : number | ||
>j : number | ||
|
12 changes: 0 additions & 12 deletions
12
tests/baselines/reference/nameWithFileExtension.errors.txt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
=== tests/cases/conformance/externalModules/foo_1.ts === | ||
import foo = require('./foo_0.js'); | ||
>foo : Symbol(foo, Decl(foo_1.ts, 0, 0)) | ||
|
||
var x = foo.foo + 42; | ||
>x : Symbol(x, Decl(foo_1.ts, 1, 3)) | ||
>foo.foo : Symbol(foo.foo, Decl(foo_0.ts, 0, 10)) | ||
>foo : Symbol(foo, Decl(foo_1.ts, 0, 0)) | ||
>foo : Symbol(foo.foo, Decl(foo_0.ts, 0, 10)) | ||
|
||
=== tests/cases/conformance/externalModules/foo_0.ts === | ||
export var foo = 42; | ||
>foo : Symbol(foo, Decl(foo_0.ts, 0, 10)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
=== tests/cases/conformance/externalModules/foo_1.ts === | ||
import foo = require('./foo_0.js'); | ||
>foo : typeof foo | ||
|
||
var x = foo.foo + 42; | ||
>x : number | ||
>foo.foo + 42 : number | ||
>foo.foo : number | ||
>foo : typeof foo | ||
>foo : number | ||
>42 : number | ||
|
||
=== tests/cases/conformance/externalModules/foo_0.ts === | ||
export var foo = 42; | ||
>foo : number | ||
>42 : number | ||
|
6 changes: 3 additions & 3 deletions
6
tests/baselines/reference/project/cantFindTheModule/amd/cantFindTheModule.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
tests/baselines/reference/project/cantFindTheModule/node/cantFindTheModule.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this pattern, split the function into a loadModuleFromFile and loadModuleFromFileWorker. the worker is never called directly except from loaModulefronFile. and loadModuleFromFile becomes:
you would want to make it more explicit off course and add some tracing.
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.
Could it be
function worker(candidate)
in a closure or would that be bad for perf?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.
i do not know :). @vladima thoughts?