Skip to content

Commit f4d6b67

Browse files
author
Andy Hanson
committed
Respond to PR comments
1 parent a0546a9 commit f4d6b67

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

src/compiler/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,13 @@ namespace ts {
861861
return fileExtensionIs(path, extension) ? path.substring(0, path.length - extension.length) : undefined;
862862
}
863863

864+
export function getFileExtension(path: string): string {
865+
const dot = path.lastIndexOf(".");
866+
if (dot !== -1) {
867+
return path.substring(dot);
868+
}
869+
}
870+
864871
export interface ObjectAllocator {
865872
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
866873
getSourceFileConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => SourceFile;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,10 @@
27682768
"category": "Error",
27692769
"code": 6131
27702770
},
2771+
"File name '{0}' has a '{1}' extension - stripping it": {
2772+
"category": "Message",
2773+
"code": 6132
2774+
},
27712775

27722776
"Variable '{0}' implicitly has an '{1}' type.": {
27732777
"category": "Error",

src/compiler/program.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -615,11 +615,26 @@ namespace ts {
615615
}
616616

617617
/**
618-
* @param extensions - Either supportedTypeScriptExtensions or if --allowJs, allSupportedExtensions
619618
* @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
620619
* 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.
621620
*/
622621
function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string {
622+
// First try to keep/add an extension: importing "./foo.ts" can be matched by a file "./foo.ts", and "./foo" by "./foo.d.ts"
623+
const keepOrAddExtension = loadModuleFromFileWorker(candidate, extensions, failedLookupLocation, onlyRecordFailures, state);
624+
if (keepOrAddExtension) {
625+
return keepOrAddExtension;
626+
}
627+
// 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"
628+
if (hasJavaScriptFileExtension(candidate)) {
629+
const extensionless = removeFileExtension(candidate);
630+
if (state.traceEnabled) {
631+
const extension = candidate.substring(extensionless.length);
632+
trace(state.host, Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension);
633+
}
634+
return loadModuleFromFileWorker(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state);
635+
}
636+
}
637+
function loadModuleFromFileWorker(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string {
623638
if (!onlyRecordFailures) {
624639
// check if containig folder exists - if it doesn't then just record failures for all supported extensions without disk probing
625640
const directory = getDirectoryPath(candidate);
@@ -628,27 +643,12 @@ namespace ts {
628643
}
629644
}
630645

631-
// First try to keep/add an extension: importing "./foo.ts" can be matched by a file "./foo.ts", and "./foo" by "./foo.d.ts"
632-
const keepOrAddExtension = forEach(extensions, ext => {
646+
return forEach(extensions, ext => {
633647
if (state.skipTsx && (ext === ".jsx" || ext === ".tsx")) {
634648
return;
635649
}
636650
return tryLoad(fileExtensionIs(candidate, ext) ? candidate : candidate + ext);
637651
});
638-
if (keepOrAddExtension) {
639-
return keepOrAddExtension;
640-
}
641-
642-
// 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"
643-
return forEach(supportedJavascriptExtensions, jsExt => {
644-
if (state.skipTsx && jsExt === ".jsx") {
645-
return;
646-
}
647-
const extensionless = tryRemoveExtension(candidate, jsExt);
648-
if (extensionless !== undefined) {
649-
return forEach(supportedTypeScriptExtensions, tsExt => tryLoad(extensionless + tsExt));
650-
}
651-
});
652652

653653
function tryLoad(fileName: string): string {
654654
if (!onlyRecordFailures && state.host.fileExists(fileName)) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
"======== Resolving module './a' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/b.ts'. ========",
3+
"Module resolution kind is not specified, using 'NodeJs'.",
4+
"Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/a'.",
5+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
6+
"======== Module name './a' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'. ========",
7+
"======== Resolving module './a.ts' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/c.ts'. ========",
8+
"Module resolution kind is not specified, using 'NodeJs'.",
9+
"Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'.",
10+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
11+
"======== Module name './a.ts' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'. ========",
12+
"======== Resolving module './a.js' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/d.ts'. ========",
13+
"Module resolution kind is not specified, using 'NodeJs'.",
14+
"Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js'.",
15+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js.ts' does not exist.",
16+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js.tsx' does not exist.",
17+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js.d.ts' does not exist.",
18+
"File name 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.js' has a '.js' extension - stripping it",
19+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
20+
"======== Module name './a.js' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/a.ts'. ========",
21+
"======== Resolving module './jquery.js' from 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery_user_1.ts'. ========",
22+
"Module resolution kind is not specified, using 'NodeJs'.",
23+
"Loading module as file / folder, candidate module location 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js'.",
24+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js.ts' does not exist.",
25+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js.tsx' does not exist.",
26+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js.d.ts' does not exist.",
27+
"File name 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.js' has a '.js' extension - stripping it",
28+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.ts' does not exist.",
29+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.tsx' does not exist.",
30+
"File 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.d.ts' exist - use it as a name resolution result.",
31+
"======== Module name './jquery.js' was successfully resolved to 'C:/Users/anhans/TypeScript/tests/cases/compiler/jquery.d.ts'. ========"
32+
]

tests/cases/compiler/moduleResolution.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @traceResolution: true
12
// @Filename: a.ts
23
export default 0;
34

0 commit comments

Comments
 (0)