Skip to content

Commit

Permalink
Merge pull request #3032 from Microsoft/supportedExtensions
Browse files Browse the repository at this point in the history
unify file extension resolution logic
  • Loading branch information
mhegazy committed May 7, 2015
2 parents cf8f582 + ce9f7b4 commit 5129d7c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,11 @@ module ts {
return symbol;
}
}
let fileName: string;
let sourceFile: SourceFile;
while (true) {
let fileName = normalizePath(combinePaths(searchPath, moduleName));
sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts");
fileName = normalizePath(combinePaths(searchPath, moduleName));
sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension));
if (sourceFile || isRelative) {
break;
}
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,16 +640,18 @@ module ts {
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
}

let supportedExtensions = [".d.ts", ".ts", ".js"];
/**
* List of supported extensions in order of file resolution precedence.
*/
export const supportedExtensions = [".ts", ".d.ts"];

const extensionsToRemove = [".d.ts", ".ts", ".js"];
export function removeFileExtension(path: string): string {
for (let ext of supportedExtensions) {

for (let ext of extensionsToRemove) {
if (fileExtensionIs(path, ext)) {
return path.substr(0, path.length - ext.length);
}
}

return path;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ module ts {
Corrupted_locale_file_0: { code: 6051, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
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." },
File_0_not_found: { code: 6053, category: DiagnosticCategory.Error, key: "File '{0}' not found." },
File_0_must_have_extension_ts_or_d_ts: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' must have extension '.ts' or '.d.ts'." },
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}." },
Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures: { code: 6055, category: DiagnosticCategory.Message, key: "Suppress noImplicitAny errors for indexing objects lacking index signatures." },
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." },
Preserve_new_lines_when_emitting_code: { code: 6057, category: DiagnosticCategory.Message, key: "Preserve new-lines when emitting code." },
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1982,7 +1982,7 @@
"category": "Error",
"code": 6053
},
"File '{0}' must have extension '.ts' or '.d.ts'.": {
"File '{0}' has unsupported extension. The only supported extensions are {1}.": {
"category": "Error",
"code": 6054
},
Expand Down
27 changes: 16 additions & 11 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,38 +307,45 @@ module ts {
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) {
let start: number;
let length: number;
let extensions: string;
let diagnosticArgument: string[];
if (refEnd !== undefined && refPos !== undefined) {
start = refPos;
length = refEnd - refPos;
}
let diagnostic: DiagnosticMessage;
if (hasExtension(fileName)) {
if (!options.allowNonTsExtensions && !fileExtensionIs(host.getCanonicalFileName(fileName), ".ts")) {
diagnostic = Diagnostics.File_0_must_have_extension_ts_or_d_ts;
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
diagnostic = Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1;
diagnosticArgument = [fileName, "'" + supportedExtensions.join("', '") + "'"];
}
else if (!findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
diagnostic = Diagnostics.File_0_not_found;
diagnosticArgument = [fileName];
}
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
diagnostic = Diagnostics.A_file_cannot_have_a_reference_to_itself;
diagnosticArgument = [fileName];
}
}
else {
if (options.allowNonTsExtensions && !findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
diagnostic = Diagnostics.File_0_not_found;
diagnosticArgument = [fileName];
}
else if (!findSourceFile(fileName + ".ts", isDefaultLib, refFile, refPos, refEnd) && !findSourceFile(fileName + ".d.ts", isDefaultLib, refFile, refPos, refEnd)) {
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) {
diagnostic = Diagnostics.File_0_not_found;
fileName += ".ts";
diagnosticArgument = [fileName];
}
}

if (diagnostic) {
if (refFile) {
diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, fileName));
diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, ...diagnosticArgument));
}
else {
diagnostics.add(createCompilerDiagnostic(diagnostic, fileName));
diagnostics.add(createCompilerDiagnostic(diagnostic, ...diagnosticArgument));
}
}
}
Expand Down Expand Up @@ -417,9 +424,10 @@ module ts {
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
if (moduleNameText) {
let searchPath = basePath;
let searchName: string;
while (true) {
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (findModuleSourceFile(searchName + ".ts", moduleNameExpr) || findModuleSourceFile(searchName + ".d.ts", moduleNameExpr)) {
searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) {
break;
}
let parentPath = getDirectoryPath(searchPath);
Expand Down Expand Up @@ -448,10 +456,7 @@ module ts {
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
// only through top - level external module names. Relative external module names are not permitted.
let searchName = normalizePath(combinePaths(basePath, moduleName));
let tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral);
if (!tsFile) {
findModuleSourceFile(searchName + ".d.ts", nameLiteral);
}
forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, nameLiteral));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
error TS6053: File 'a.ts' not found.


!!! error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
!!! error TS6053: File 'a.ts' not found.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'.
error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'.
error TS6053: File 'a.ts' not found.


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

0 comments on commit 5129d7c

Please sign in to comment.