Skip to content

Commit 0b21540

Browse files
committed
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
1 parent 1ed67f4 commit 0b21540

File tree

6 files changed

+34
-35
lines changed

6 files changed

+34
-35
lines changed

src/compiler/commandLineParser.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -502,41 +502,30 @@ namespace ts {
502502
else {
503503
const filesSeen: Map<boolean> = {};
504504
const exclude = json["exclude"] instanceof Array ? map(<string[]>json["exclude"], normalizeSlashes) : undefined;
505-
const extensionsByPriority = getSupportedExtensions(options);
506-
for (let extensionsIndex = 0; extensionsIndex < extensionsByPriority.length; extensionsIndex++) {
507-
const currentExtension = extensionsByPriority[extensionsIndex];
508-
const filesInDirWithExtension = host.readDirectory(basePath, currentExtension, exclude);
509-
// Get list of conflicting extensions, conflicting extension is
510-
// - extension that is lower priority than current extension and
511-
// - extension also is current extension (ends with "." + currentExtension)
512-
const conflictingExtensions: string[] = [];
513-
for (let i = extensionsIndex + 1; i < extensionsByPriority.length; i++) {
514-
const extension = extensionsByPriority[i]; // lower priority extension
515-
if (fileExtensionIs(extension, currentExtension)) { // also has current extension
516-
conflictingExtensions.push(extension);
517-
}
518-
}
505+
const supportedExtensions = getSupportedExtensions(options);
506+
Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick");
519507

520-
// Add the files to fileNames list if the file is not any of conflicting extension
508+
// Get files of supported extensions in their order of resolution
509+
for (const extension of supportedExtensions) {
510+
const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude);
521511
for (const fileName of filesInDirWithExtension) {
522-
let hasConflictingExtension = false;
523-
for (const conflictingExtension of conflictingExtensions) {
524-
// eg. 'f.d.ts' will match '.ts' extension but really should be process later with '.d.ts' files
525-
if (fileExtensionIs(fileName, conflictingExtension)) {
526-
hasConflictingExtension = true;
527-
break;
528-
}
512+
// .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension,
513+
// lets pick them when its turn comes up
514+
if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) {
515+
continue;
529516
}
530517

531-
if (!hasConflictingExtension) {
532-
// Add the file only if there is no higher priority extension file already included
533-
// eg. when a.d.ts and a.js are present in the folder, include only a.d.ts not a.js
534-
const baseName = fileName.substr(0, fileName.length - currentExtension.length);
535-
if (!hasProperty(filesSeen, baseName)) {
536-
filesSeen[baseName] = true;
537-
fileNames.push(fileName);
518+
// If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files)
519+
// 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
520+
if (extension === ".d.ts" || (options.allowJs && extension === ".js")) {
521+
const baseName = fileName.substr(0, fileName.length - extension.length);
522+
if (hasProperty(filesSeen, baseName + ".ts") || hasProperty(filesSeen, baseName + ".tsx")) {
523+
continue;
538524
}
539525
}
526+
527+
filesSeen[fileName] = true;
528+
fileNames.push(fileName);
540529
}
541530
}
542531
}

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ namespace ts {
297297
return <T>result;
298298
}
299299

300-
export function extend<T1, T2>(first: Map<T1>, second: Map<T2>): Map<T1 & T2> {
301-
const result: Map<T1 & T2> = {};
300+
export function extend<T1 extends Map<{}>, T2 extends Map<{}>>(first: T1 , second: T2): T1 & T2 {
301+
const result: T1 & T2 = <any>{};
302302
for (const id in first) {
303303
(result as any)[id] = first[id];
304304
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
2+
error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file.
23

34

45
!!! error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
6+
!!! error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file.
57
==== SameNameDTsNotSpecifiedWithAllowJs/a.d.ts (0 errors) ====
6-
declare var a: number;
8+
declare var a: number;
9+
==== SameNameDTsNotSpecifiedWithAllowJs/a.js (0 errors) ====
10+
var test1 = 10; // Shouldnt get compiled

tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/amd/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"project": "SameNameDTsNotSpecifiedWithAllowJs",
77
"resolvedInputFiles": [
88
"lib.d.ts",
9-
"SameNameDTsNotSpecifiedWithAllowJs/a.d.ts"
9+
"SameNameDTsNotSpecifiedWithAllowJs/a.d.ts",
10+
"SameNameDTsNotSpecifiedWithAllowJs/a.js"
1011
],
1112
"emittedFiles": []
1213
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
2+
error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file.
23

34

45
!!! error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
6+
!!! error TS5055: Cannot write file 'SameNameDTsNotSpecifiedWithAllowJs/a.js' because it would overwrite input file.
57
==== SameNameDTsNotSpecifiedWithAllowJs/a.d.ts (0 errors) ====
6-
declare var a: number;
8+
declare var a: number;
9+
==== SameNameDTsNotSpecifiedWithAllowJs/a.js (0 errors) ====
10+
var test1 = 10; // Shouldnt get compiled

tests/baselines/reference/project/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs/node/jsFileCompilationSameNameDtsNotSpecifiedWithAllowJs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"project": "SameNameDTsNotSpecifiedWithAllowJs",
77
"resolvedInputFiles": [
88
"lib.d.ts",
9-
"SameNameDTsNotSpecifiedWithAllowJs/a.d.ts"
9+
"SameNameDTsNotSpecifiedWithAllowJs/a.d.ts",
10+
"SameNameDTsNotSpecifiedWithAllowJs/a.js"
1011
],
1112
"emittedFiles": []
1213
}

0 commit comments

Comments
 (0)