Skip to content

Commit 5129d7c

Browse files
committed
Merge pull request microsoft#3032 from Microsoft/supportedExtensions
unify file extension resolution logic
2 parents cf8f582 + ce9f7b4 commit 5129d7c

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,11 @@ module ts {
857857
return symbol;
858858
}
859859
}
860+
let fileName: string;
860861
let sourceFile: SourceFile;
861862
while (true) {
862-
let fileName = normalizePath(combinePaths(searchPath, moduleName));
863-
sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts");
863+
fileName = normalizePath(combinePaths(searchPath, moduleName));
864+
sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension));
864865
if (sourceFile || isRelative) {
865866
break;
866867
}

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

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ module ts {
498498
Corrupted_locale_file_0: { code: 6051, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
499499
Raise_error_on_expressions_and_declarations_with_an_implied_any_type: { code: 6052, category: DiagnosticCategory.Message, key: "Raise error on expressions and declarations with an implied 'any' type." },
500500
File_0_not_found: { code: 6053, category: DiagnosticCategory.Error, key: "File '{0}' not found." },
501-
File_0_must_have_extension_ts_or_d_ts: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' must have extension '.ts' or '.d.ts'." },
501+
File_0_has_unsupported_extension_The_only_supported_extensions_are_1: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' has unsupported extension. The only supported extensions are {1}." },
502502
Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures: { code: 6055, category: DiagnosticCategory.Message, key: "Suppress noImplicitAny errors for indexing objects lacking index signatures." },
503503
Do_not_emit_declarations_for_code_that_has_an_internal_annotation: { code: 6056, category: DiagnosticCategory.Message, key: "Do not emit declarations for code that has an '@internal' annotation." },
504504
Preserve_new_lines_when_emitting_code: { code: 6057, category: DiagnosticCategory.Message, key: "Preserve new-lines when emitting code." },

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@
19821982
"category": "Error",
19831983
"code": 6053
19841984
},
1985-
"File '{0}' must have extension '.ts' or '.d.ts'.": {
1985+
"File '{0}' has unsupported extension. The only supported extensions are {1}.": {
19861986
"category": "Error",
19871987
"code": 6054
19881988
},

src/compiler/program.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,38 +307,45 @@ module ts {
307307
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) {
308308
let start: number;
309309
let length: number;
310+
let extensions: string;
311+
let diagnosticArgument: string[];
310312
if (refEnd !== undefined && refPos !== undefined) {
311313
start = refPos;
312314
length = refEnd - refPos;
313315
}
314316
let diagnostic: DiagnosticMessage;
315317
if (hasExtension(fileName)) {
316-
if (!options.allowNonTsExtensions && !fileExtensionIs(host.getCanonicalFileName(fileName), ".ts")) {
317-
diagnostic = Diagnostics.File_0_must_have_extension_ts_or_d_ts;
318+
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
319+
diagnostic = Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1;
320+
diagnosticArgument = [fileName, "'" + supportedExtensions.join("', '") + "'"];
318321
}
319322
else if (!findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
320323
diagnostic = Diagnostics.File_0_not_found;
324+
diagnosticArgument = [fileName];
321325
}
322326
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
323327
diagnostic = Diagnostics.A_file_cannot_have_a_reference_to_itself;
328+
diagnosticArgument = [fileName];
324329
}
325330
}
326331
else {
327332
if (options.allowNonTsExtensions && !findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
328333
diagnostic = Diagnostics.File_0_not_found;
334+
diagnosticArgument = [fileName];
329335
}
330-
else if (!findSourceFile(fileName + ".ts", isDefaultLib, refFile, refPos, refEnd) && !findSourceFile(fileName + ".d.ts", isDefaultLib, refFile, refPos, refEnd)) {
336+
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) {
331337
diagnostic = Diagnostics.File_0_not_found;
332338
fileName += ".ts";
339+
diagnosticArgument = [fileName];
333340
}
334341
}
335342

336343
if (diagnostic) {
337344
if (refFile) {
338-
diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, fileName));
345+
diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, ...diagnosticArgument));
339346
}
340347
else {
341-
diagnostics.add(createCompilerDiagnostic(diagnostic, fileName));
348+
diagnostics.add(createCompilerDiagnostic(diagnostic, ...diagnosticArgument));
342349
}
343350
}
344351
}
@@ -417,9 +424,10 @@ module ts {
417424
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
418425
if (moduleNameText) {
419426
let searchPath = basePath;
427+
let searchName: string;
420428
while (true) {
421-
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
422-
if (findModuleSourceFile(searchName + ".ts", moduleNameExpr) || findModuleSourceFile(searchName + ".d.ts", moduleNameExpr)) {
429+
searchName = normalizePath(combinePaths(searchPath, moduleNameText));
430+
if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) {
423431
break;
424432
}
425433
let parentPath = getDirectoryPath(searchPath);
@@ -448,10 +456,7 @@ module ts {
448456
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
449457
// only through top - level external module names. Relative external module names are not permitted.
450458
let searchName = normalizePath(combinePaths(basePath, moduleName));
451-
let tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral);
452-
if (!tsFile) {
453-
findModuleSourceFile(searchName + ".d.ts", nameLiteral);
454-
}
459+
forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, nameLiteral));
455460
}
456461
}
457462
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
1+
error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
22
error TS6053: File 'a.ts' not found.
33

44

5-
!!! error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
5+
!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
66
!!! error TS6053: File 'a.ts' not found.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
1+
error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
22
error TS6053: File 'a.ts' not found.
33

44

5-
!!! error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
5+
!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
66
!!! error TS6053: File 'a.ts' not found.

0 commit comments

Comments
 (0)