From 78d221993b2c4c011834e094d9581acdc5cd4ef4 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 17 Sep 2018 14:41:48 +0200 Subject: [PATCH 1/2] 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 --- src/compiler/commandLineParser.ts | 17 ++++++++++++----- .../unittests/configurationExtension.ts | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 05113f812dbfc..14925c86354e3 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1993,7 +1993,7 @@ namespace ts { if (ownConfig.extendedConfigPath) { // copy the resolution stack so it is never reused between branches in potential diamond-problem scenarios. resolutionStack = resolutionStack.concat([resolvedPath]); - const extendedConfig = getExtendedConfig(sourceFile!, ownConfig.extendedConfigPath, host, basePath, resolutionStack, errors); + const extendedConfig = getExtendedConfig(sourceFile, ownConfig.extendedConfigPath, host, basePath, resolutionStack, errors); if (extendedConfig && isSuccessfulParsedTsconfig(extendedConfig)) { const baseRaw = extendedConfig.raw; const raw = ownConfig.raw; @@ -2134,7 +2134,7 @@ namespace ts { } function getExtendedConfig( - sourceFile: TsConfigSourceFile, + sourceFile: TsConfigSourceFile | undefined, extendedConfigPath: string, host: ParseConfigHost, basePath: string, @@ -2143,7 +2143,12 @@ namespace ts { ): ParsedTsconfig | undefined { const extendedResult = readJsonConfigFile(extendedConfigPath, path => host.readFile(path)); if (sourceFile) { - (sourceFile.extendedSourceFiles || (sourceFile.extendedSourceFiles = [])).push(extendedResult.fileName); + if (sourceFile.extendedSourceFiles) { + pushIfUnique(sourceFile.extendedSourceFiles, extendedResult.fileName); + } + else { + sourceFile.extendedSourceFiles = [extendedResult.fileName]; + } } if (extendedResult.parseDiagnostics.length) { errors.push(...extendedResult.parseDiagnostics); @@ -2153,8 +2158,10 @@ namespace ts { const extendedDirname = getDirectoryPath(extendedConfigPath); const extendedConfig = parseConfig(/*json*/ undefined, extendedResult, host, extendedDirname, getBaseFileName(extendedConfigPath), resolutionStack, errors); - if (sourceFile) { - sourceFile.extendedSourceFiles!.push(...extendedResult.extendedSourceFiles!); + if (sourceFile && extendedResult.extendedSourceFiles) { + for (const extended of extendedResult.extendedSourceFiles) { + pushIfUnique(sourceFile.extendedSourceFiles!, extended); + } } if (isSuccessfulParsedTsconfig(extendedConfig)) { diff --git a/src/testRunner/unittests/configurationExtension.ts b/src/testRunner/unittests/configurationExtension.ts index 012b8e0d2fae1..b9567b3b54d39 100644 --- a/src/testRunner/unittests/configurationExtension.ts +++ b/src/testRunner/unittests/configurationExtension.ts @@ -244,6 +244,20 @@ namespace ts { }, [ combinePaths(basePath, "main.ts") ]); + + it("adds extendedSourceFiles only once", () => { + const sourceFile = readJsonConfigFile("configs/fourth.json", (path) => host.readFile(path)); + const dir = combinePaths(basePath, "configs"); + const expected = [ + combinePaths(dir, "third.json"), + combinePaths(dir, "second.json"), + combinePaths(dir, "base.json"), + ]; + parseJsonSourceFileConfigFileContent(sourceFile, host, dir, {}, "fourth.json"); + assert.deepEqual(sourceFile.extendedSourceFiles, expected); + parseJsonSourceFileConfigFileContent(sourceFile, host, dir, {}, "fourth.json"); + assert.deepEqual(sourceFile.extendedSourceFiles, expected); + }); }); }); }); From f71030f011b2918033f529a9ff582622665e8cde Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 17 Sep 2018 21:24:26 +0200 Subject: [PATCH 2/2] Simply override extendedSourceFiles array --- src/compiler/commandLineParser.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 14925c86354e3..98f0f586c8b0b 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -2143,12 +2143,7 @@ namespace ts { ): ParsedTsconfig | undefined { const extendedResult = readJsonConfigFile(extendedConfigPath, path => host.readFile(path)); if (sourceFile) { - if (sourceFile.extendedSourceFiles) { - pushIfUnique(sourceFile.extendedSourceFiles, extendedResult.fileName); - } - else { - sourceFile.extendedSourceFiles = [extendedResult.fileName]; - } + sourceFile.extendedSourceFiles = [extendedResult.fileName]; } if (extendedResult.parseDiagnostics.length) { errors.push(...extendedResult.parseDiagnostics); @@ -2159,9 +2154,7 @@ namespace ts { const extendedConfig = parseConfig(/*json*/ undefined, extendedResult, host, extendedDirname, getBaseFileName(extendedConfigPath), resolutionStack, errors); if (sourceFile && extendedResult.extendedSourceFiles) { - for (const extended of extendedResult.extendedSourceFiles) { - pushIfUnique(sourceFile.extendedSourceFiles!, extended); - } + sourceFile.extendedSourceFiles!.push(...extendedResult.extendedSourceFiles); } if (isSuccessfulParsedTsconfig(extendedConfig)) {