From 0b215404d107e64d5eda4628ef1f5f210cebf683 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 12 Nov 2015 13:23:53 -0800 Subject: [PATCH] Simplified logic of getting files if files werent suppplied in tscconfig.json Since we dont support arbitary list of extensions to treat as .js, it becomes easier to simplify code based on the assumptions --- src/compiler/commandLineParser.ts | 49 +++++++------------ src/compiler/core.ts | 4 +- ...eNameDtsNotSpecifiedWithAllowJs.errors.txt | 6 ++- ...ionSameNameDtsNotSpecifiedWithAllowJs.json | 3 +- ...eNameDtsNotSpecifiedWithAllowJs.errors.txt | 6 ++- ...ionSameNameDtsNotSpecifiedWithAllowJs.json | 3 +- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 3c3bb14174d..5336b01bb14 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -502,41 +502,30 @@ namespace ts { else { const filesSeen: Map = {}; const exclude = json["exclude"] instanceof Array ? map(json["exclude"], normalizeSlashes) : undefined; - const extensionsByPriority = getSupportedExtensions(options); - for (let extensionsIndex = 0; extensionsIndex < extensionsByPriority.length; extensionsIndex++) { - const currentExtension = extensionsByPriority[extensionsIndex]; - const filesInDirWithExtension = host.readDirectory(basePath, currentExtension, exclude); - // Get list of conflicting extensions, conflicting extension is - // - extension that is lower priority than current extension and - // - extension also is current extension (ends with "." + currentExtension) - const conflictingExtensions: string[] = []; - for (let i = extensionsIndex + 1; i < extensionsByPriority.length; i++) { - const extension = extensionsByPriority[i]; // lower priority extension - if (fileExtensionIs(extension, currentExtension)) { // also has current extension - conflictingExtensions.push(extension); - } - } + const supportedExtensions = getSupportedExtensions(options); + Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick"); - // Add the files to fileNames list if the file is not any of conflicting extension + // Get files of supported extensions in their order of resolution + for (const extension of supportedExtensions) { + const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude); for (const fileName of filesInDirWithExtension) { - let hasConflictingExtension = false; - for (const conflictingExtension of conflictingExtensions) { - // eg. 'f.d.ts' will match '.ts' extension but really should be process later with '.d.ts' files - if (fileExtensionIs(fileName, conflictingExtension)) { - hasConflictingExtension = true; - break; + // .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension, + // lets pick them when its turn comes up + if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) { + continue; + } + + // If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files) + // do not include this file if we included .ts or .tsx file with same base name as it could be output of the earlier compilation + if (extension === ".d.ts" || (options.allowJs && extension === ".js")) { + const baseName = fileName.substr(0, fileName.length - extension.length); + if (hasProperty(filesSeen, baseName + ".ts") || hasProperty(filesSeen, baseName + ".tsx")) { + continue; } } - if (!hasConflictingExtension) { - // Add the file only if there is no higher priority extension file already included - // eg. when a.d.ts and a.js are present in the folder, include only a.d.ts not a.js - const baseName = fileName.substr(0, fileName.length - currentExtension.length); - if (!hasProperty(filesSeen, baseName)) { - filesSeen[baseName] = true; - fileNames.push(fileName); - } - } + filesSeen[fileName] = true; + fileNames.push(fileName); } } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 5b0eedfc1f9..b773cdfaf45 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -297,8 +297,8 @@ namespace ts { return result; } - export function extend(first: Map, second: Map): Map { - const result: Map = {}; + export function extend, T2 extends Map<{}>>(first: T1 , second: T2): T1 & T2 { + const result: T1 & T2 = {}; for (const id in first) { (result as any)[id] = first[id]; } diff --git a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt index 8da5b629ca8..ac25edc1bd5 100644 --- a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt +++ b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt @@ -1,6 +1,10 @@ error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. +error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file. !!! error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. +!!! error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file. ==== SameNameDTsNotSpecifiedWithAllowJs/a.d.ts (0 errors) ==== - declare var a: number; \ No newline at end of file + declare var a: number; +==== SameNameDTsNotSpecifiedWithAllowJs/a.js (0 errors) ==== + var test1 = 10; // Shouldnt get compiled \ No newline at end of file diff --git a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json index 77e1f227030..b4c3fb8f51b 100644 --- a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json +++ b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json @@ -6,7 +6,8 @@ "project": "SameNameDTsNotSpecifiedWithAllowJs", "resolvedInputFiles": [ "lib.d.ts", - "SameNameDTsNotSpecifiedWithAllowJs/a.d.ts" + "SameNameDTsNotSpecifiedWithAllowJs/a.d.ts", + "SameNameDTsNotSpecifiedWithAllowJs/a.js" ], "emittedFiles": [] } \ No newline at end of file diff --git a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt index 8da5b629ca8..ac25edc1bd5 100644 --- a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt +++ b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.errors.txt @@ -1,6 +1,10 @@ error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. +error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file. !!! error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. +!!! error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file. ==== SameNameDTsNotSpecifiedWithAllowJs/a.d.ts (0 errors) ==== - declare var a: number; \ No newline at end of file + declare var a: number; +==== SameNameDTsNotSpecifiedWithAllowJs/a.js (0 errors) ==== + var test1 = 10; // Shouldnt get compiled \ No newline at end of file diff --git a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json index 77e1f227030..b4c3fb8f51b 100644 --- a/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json +++ b/tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json @@ -6,7 +6,8 @@ "project": "SameNameDTsNotSpecifiedWithAllowJs", "resolvedInputFiles": [ "lib.d.ts", - "SameNameDTsNotSpecifiedWithAllowJs/a.d.ts" + "SameNameDTsNotSpecifiedWithAllowJs/a.d.ts", + "SameNameDTsNotSpecifiedWithAllowJs/a.js" ], "emittedFiles": [] } \ No newline at end of file