Skip to content

Commit b7f4480

Browse files
committed
unify file extension resolution logic
1 parent 3c9a3c5 commit b7f4480

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,11 @@ module ts {
853853
return symbol;
854854
}
855855
}
856+
let fileName: string;
856857
let sourceFile: SourceFile;
857858
while (true) {
858-
let fileName = normalizePath(combinePaths(searchPath, moduleName));
859-
sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts");
859+
fileName = normalizePath(combinePaths(searchPath, moduleName));
860+
sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension));
860861
if (sourceFile || isRelative) {
861862
break;
862863
}

src/compiler/core.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,18 @@ module ts {
640640
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
641641
}
642642

643-
let supportedExtensions = [".d.ts", ".ts", ".js"];
643+
/**
644+
* List of supported extensions in order of file resolution precedence.
645+
*/
646+
export const supportedExtensions = [".ts", ".d.ts"];
644647

648+
const extensitionsToRemove = [".d.ts", ".ts", ".js"];
645649
export function removeFileExtension(path: string): string {
646-
for (let ext of supportedExtensions) {
647-
650+
for (let ext of extensitionsToRemove) {
648651
if (fileExtensionIs(path, ext)) {
649652
return path.substr(0, path.length - ext.length);
650653
}
651654
}
652-
653655
return path;
654656
}
655657

src/compiler/program.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ module ts {
313313
}
314314
let diagnostic: DiagnosticMessage;
315315
if (hasExtension(fileName)) {
316-
if (!options.allowNonTsExtensions && !fileExtensionIs(host.getCanonicalFileName(fileName), ".ts")) {
316+
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
317317
diagnostic = Diagnostics.File_0_must_have_extension_ts_or_d_ts;
318318
}
319319
else if (!findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
@@ -327,7 +327,7 @@ module ts {
327327
if (options.allowNonTsExtensions && !findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
328328
diagnostic = Diagnostics.File_0_not_found;
329329
}
330-
else if (!findSourceFile(fileName + ".ts", isDefaultLib, refFile, refPos, refEnd) && !findSourceFile(fileName + ".d.ts", isDefaultLib, refFile, refPos, refEnd)) {
330+
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) {
331331
diagnostic = Diagnostics.File_0_not_found;
332332
fileName += ".ts";
333333
}
@@ -417,9 +417,10 @@ module ts {
417417
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
418418
if (moduleNameText) {
419419
let searchPath = basePath;
420+
let searchName: string;
420421
while (true) {
421-
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
422-
if (findModuleSourceFile(searchName + ".ts", moduleNameExpr) || findModuleSourceFile(searchName + ".d.ts", moduleNameExpr)) {
422+
searchName = normalizePath(combinePaths(searchPath, moduleNameText));
423+
if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) {
423424
break;
424425
}
425426
let parentPath = getDirectoryPath(searchPath);
@@ -448,10 +449,7 @@ module ts {
448449
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
449450
// only through top - level external module names. Relative external module names are not permitted.
450451
let searchName = normalizePath(combinePaths(basePath, moduleName));
451-
let tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral);
452-
if (!tsFile) {
453-
findModuleSourceFile(searchName + ".d.ts", nameLiteral);
454-
}
452+
forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, nameLiteral));
455453
}
456454
}
457455
});

0 commit comments

Comments
 (0)