-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 3 commits
fa16a99
a0546a9
f4d6b67
d4b8889
9179f4c
9575b3c
a918730
704f987
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 |
---|---|---|
|
@@ -849,13 +849,25 @@ namespace ts { | |
const extensionsToRemove = [".d.ts", ".ts", ".js", ".tsx", ".jsx"]; | ||
export function removeFileExtension(path: string): string { | ||
for (const ext of extensionsToRemove) { | ||
if (fileExtensionIs(path, ext)) { | ||
return path.substr(0, path.length - ext.length); | ||
const extensionless = tryRemoveExtension(path, ext); | ||
if (extensionless !== undefined) { | ||
return extensionless; | ||
} | ||
} | ||
return path; | ||
} | ||
|
||
export function tryRemoveExtension(path: string, extension: string): string { | ||
return fileExtensionIs(path, extension) ? path.substring(0, path.length - extension.length) : undefined; | ||
} | ||
|
||
export function getFileExtension(path: string): string { | ||
const dot = path.lastIndexOf("."); | ||
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. What if the dot is somewhere else in the path, e.g. 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. Turns out we don't need this function, so removing it. |
||
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 |
---|---|---|
|
@@ -619,20 +619,38 @@ 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 keepOrAddExtension = loadModuleFromFileWorker(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); | ||
if (keepOrAddExtension) { | ||
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. Nit: It says |
||
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); | ||
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. consider this pattern, split the function into a loadModuleFromFile and loadModuleFromFileWorker. the worker is never called directly except from loaModulefronFile. and loadModuleFromFile becomes: function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string {
return loadModuleFromFileWorker(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) ||
hasJavaScriptFileExtension(candidate) ? loadModuleFromFileWorker(removeFileExtension(candidate), extensionsm failedLookupLocation, onlyRecordFailures, state) : undefined;
} 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 commentThe reason will be displayed to describe this comment to others. Learn more. Could it be 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. i do not know :). @vladima thoughts? |
||
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 | ||
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. containing |
||
const directory = getDirectoryPath(candidate); | ||
if (directory) { | ||
onlyRecordFailures = !directoryProbablyExists(directory, state.host); | ||
} | ||
} | ||
return forEach(extensions, tryLoad); | ||
|
||
function tryLoad(ext: string): string { | ||
if (ext === ".tsx" && state.skipTsx) { | ||
return undefined; | ||
return forEach(extensions, ext => { | ||
if (state.skipTsx && (ext === ".jsx" || ext === ".tsx")) { | ||
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. If there isn't one, it would be useful to have a function to determine if the file extension is explicitly a JSX 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. Do you mean 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 not think you need the extra lambda. just revert to the old tryLoad implementation. |
||
return; | ||
} | ||
const fileName = fileExtensionIs(candidate, ext) ? candidate : candidate + ext; | ||
return tryLoad(fileExtensionIs(candidate, ext) ? candidate : candidate + ext); | ||
}); | ||
|
||
function tryLoad(fileName: string): string { | ||
if (!onlyRecordFailures && state.host.fileExists(fileName)) { | ||
if (state.traceEnabled) { | ||
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//// [tests/cases/compiler/moduleResolution.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"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
=== tests/cases/compiler/a.ts === | ||
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.=== tests/cases/compiler/b.ts === | ||
import a from './a'; | ||
>a : Symbol(a, Decl(b.ts, 0, 6)) | ||
|
||
// Matching extension | ||
=== tests/cases/compiler/c.ts === | ||
import a from './a.ts'; | ||
>a : Symbol(a, Decl(c.ts, 0, 6)) | ||
|
||
// '.js' extension: stripped and replaced with '.ts' | ||
=== tests/cases/compiler/d.ts === | ||
import a from './a.js'; | ||
>a : Symbol(a, Decl(d.ts, 0, 6)) | ||
|
||
=== tests/cases/compiler/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 | ||
=== tests/cases/compiler/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' | ||
=== tests/cases/compiler/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)) | ||
|
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
=== tests/cases/compiler/a.ts === | ||
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.=== tests/cases/compiler/b.ts === | ||
import a from './a'; | ||
>a : number | ||
|
||
// Matching extension | ||
=== tests/cases/compiler/c.ts === | ||
import a from './a.ts'; | ||
>a : number | ||
|
||
// '.js' extension: stripped and replaced with '.ts' | ||
=== tests/cases/compiler/d.ts === | ||
import a from './a.js'; | ||
>a : number | ||
|
||
=== tests/cases/compiler/jquery.d.ts === | ||
declare var x: number; | ||
>x : number | ||
|
||
export default x; | ||
>x : number | ||
|
||
// No extension: '.d.ts' added | ||
=== tests/cases/compiler/jquery_user_1.ts === | ||
import j from "./jquery"; | ||
>j : number | ||
|
||
// '.js' extension: stripped and replaced with '.d.ts' | ||
=== tests/cases/compiler/jquery_user_1.ts === | ||
import j from "./jquery.js" | ||
>j : number | ||
>j : number | ||
|
This file was deleted.
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)) | ||
|
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 | ||
|
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