Skip to content

Commit 78d2219

Browse files
committed
fix getExtendedConfig in commandLineParser
* remove invalid assertion * fix invalid array spread on possibly undefined value * only add unique files to extendedSourceFiles, preventing the array from growing infinitely
1 parent 394ee31 commit 78d2219

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/compiler/commandLineParser.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,7 @@ namespace ts {
19931993
if (ownConfig.extendedConfigPath) {
19941994
// copy the resolution stack so it is never reused between branches in potential diamond-problem scenarios.
19951995
resolutionStack = resolutionStack.concat([resolvedPath]);
1996-
const extendedConfig = getExtendedConfig(sourceFile!, ownConfig.extendedConfigPath, host, basePath, resolutionStack, errors);
1996+
const extendedConfig = getExtendedConfig(sourceFile, ownConfig.extendedConfigPath, host, basePath, resolutionStack, errors);
19971997
if (extendedConfig && isSuccessfulParsedTsconfig(extendedConfig)) {
19981998
const baseRaw = extendedConfig.raw;
19991999
const raw = ownConfig.raw;
@@ -2134,7 +2134,7 @@ namespace ts {
21342134
}
21352135

21362136
function getExtendedConfig(
2137-
sourceFile: TsConfigSourceFile,
2137+
sourceFile: TsConfigSourceFile | undefined,
21382138
extendedConfigPath: string,
21392139
host: ParseConfigHost,
21402140
basePath: string,
@@ -2143,7 +2143,12 @@ namespace ts {
21432143
): ParsedTsconfig | undefined {
21442144
const extendedResult = readJsonConfigFile(extendedConfigPath, path => host.readFile(path));
21452145
if (sourceFile) {
2146-
(sourceFile.extendedSourceFiles || (sourceFile.extendedSourceFiles = [])).push(extendedResult.fileName);
2146+
if (sourceFile.extendedSourceFiles) {
2147+
pushIfUnique(sourceFile.extendedSourceFiles, extendedResult.fileName);
2148+
}
2149+
else {
2150+
sourceFile.extendedSourceFiles = [extendedResult.fileName];
2151+
}
21472152
}
21482153
if (extendedResult.parseDiagnostics.length) {
21492154
errors.push(...extendedResult.parseDiagnostics);
@@ -2153,8 +2158,10 @@ namespace ts {
21532158
const extendedDirname = getDirectoryPath(extendedConfigPath);
21542159
const extendedConfig = parseConfig(/*json*/ undefined, extendedResult, host, extendedDirname,
21552160
getBaseFileName(extendedConfigPath), resolutionStack, errors);
2156-
if (sourceFile) {
2157-
sourceFile.extendedSourceFiles!.push(...extendedResult.extendedSourceFiles!);
2161+
if (sourceFile && extendedResult.extendedSourceFiles) {
2162+
for (const extended of extendedResult.extendedSourceFiles) {
2163+
pushIfUnique(sourceFile.extendedSourceFiles!, extended);
2164+
}
21582165
}
21592166

21602167
if (isSuccessfulParsedTsconfig(extendedConfig)) {

src/testRunner/unittests/configurationExtension.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ namespace ts {
244244
}, [
245245
combinePaths(basePath, "main.ts")
246246
]);
247+
248+
it("adds extendedSourceFiles only once", () => {
249+
const sourceFile = readJsonConfigFile("configs/fourth.json", (path) => host.readFile(path));
250+
const dir = combinePaths(basePath, "configs");
251+
const expected = [
252+
combinePaths(dir, "third.json"),
253+
combinePaths(dir, "second.json"),
254+
combinePaths(dir, "base.json"),
255+
];
256+
parseJsonSourceFileConfigFileContent(sourceFile, host, dir, {}, "fourth.json");
257+
assert.deepEqual(sourceFile.extendedSourceFiles, expected);
258+
parseJsonSourceFileConfigFileContent(sourceFile, host, dir, {}, "fourth.json");
259+
assert.deepEqual(sourceFile.extendedSourceFiles, expected);
260+
});
247261
});
248262
});
249263
});

0 commit comments

Comments
 (0)