From 5a4499a93997b6b2e11f144f6cdc1cecc4b38f96 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 5 Dec 2023 14:51:58 -0800 Subject: [PATCH] Fix tsconfig module parsing --- src/compiler/commandLineParser.ts | 21 +++++--------- .../config/convertCompilerOptionsFromJson.ts | 13 +++++++++ .../unittests/config/tsconfigParsing.ts | 9 ++++++ ...odule as object with jsonSourceFile api.js | 29 +++++++++++++++++++ .../module as object jsonParse.js | 20 +++++++++++++ 5 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 tests/baselines/reference/config/convertCompilerOptionsFromJson/module as object with jsonSourceFile api.js create mode 100644 tests/baselines/reference/config/tsconfigParsing/module as object jsonParse.js diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 943b458e176..4d7bceb5360 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -3372,7 +3372,12 @@ function parseOwnConfigOfJsonSourceFile( else if (parentOption === typeAcquisitionDeclaration) currentOption = typeAcquisition ??= getDefaultTypeAcquisition(configFileName); else if (parentOption === moduleOptionDeclaration) currentOption = moduleOption ??= {}; else Debug.fail("Unknown option"); - currentOption[option.name] = value; + if (currentOption === options && option.name === "module" && moduleOption) { + options.module = moduleOption; + } + else { + currentOption[option.name] = value; + } } else if (keyText && parentOption?.extraKeyDiagnostics) { if (parentOption.elementOptions) { @@ -3592,18 +3597,8 @@ export function convertJsonOption( return isNullOrUndefined(validatedValue) ? validatedValue : normalizeNonListOptionValue(opt, basePath, validatedValue); } else { - // const result: NestedCompilerOption = {}; - // for (const id in value as NestedCompilerOption) { - // const property = (value as NestedCompilerOption)[id]; - // const subOption = opt.elementOptions.get(id); - // if (subOption) { - // result[opt.name] = convertJsonOption(opt, property, basePath, errors, propertyAssignment, valueExpression, sourceFile); - // } - // else { - // errors.push(createUnknownOptionError(id, opt.extraKeyDiagnostics, /*unknownOptionErrorText*/ undefined, propertyAssignment?.name, sourceFile)); - // } - // } - return {}; + // Property keys have already been converted/validated + return value; } } else if (!isString(opt.type)) { diff --git a/src/testRunner/unittests/config/convertCompilerOptionsFromJson.ts b/src/testRunner/unittests/config/convertCompilerOptionsFromJson.ts index 31d9ff664d3..deed2a6a206 100644 --- a/src/testRunner/unittests/config/convertCompilerOptionsFromJson.ts +++ b/src/testRunner/unittests/config/convertCompilerOptionsFromJson.ts @@ -307,4 +307,17 @@ describe("unittests:: config:: convertCompilerOptionsFromJson", () => { baselineCompilerOptionsJsonText("Don't crash when root expression is not object at all", `42`, "tsconfig.json"); baselineCompilerOptionsJsonText("Allow trailing comments", `{} // no options`, "tsconfig.json"); + + baselineCompilerOptionsJsonText( + "module as object", + `{ + "compilerOptions": { + "module": { + "preset": "nodenext", + "formatDetection": "defaultmodule" + } + } + }`, + "tsconfig.json", + ); }); diff --git a/src/testRunner/unittests/config/tsconfigParsing.ts b/src/testRunner/unittests/config/tsconfigParsing.ts index 1b050c6a20c..d3c334d6ff5 100644 --- a/src/testRunner/unittests/config/tsconfigParsing.ts +++ b/src/testRunner/unittests/config/tsconfigParsing.ts @@ -144,6 +144,15 @@ describe("unittests:: config:: tsconfigParsing:: parseConfigFileTextToJson", () }`, ]); + baselineParseResult("module as object", () => [`{ + "compilerOptions": { + "module": { + "preset": "nodenext", + "formatDetection": "defaultmodule", + }, + }, + }`]); + baselinedParsed("returns error when tsconfig have excludes", () => [{ jsonText: `{ "compilerOptions": { diff --git a/tests/baselines/reference/config/convertCompilerOptionsFromJson/module as object with jsonSourceFile api.js b/tests/baselines/reference/config/convertCompilerOptionsFromJson/module as object with jsonSourceFile api.js new file mode 100644 index 00000000000..236ad29b470 --- /dev/null +++ b/tests/baselines/reference/config/convertCompilerOptionsFromJson/module as object with jsonSourceFile api.js @@ -0,0 +1,29 @@ +Fs:: +//// [/apath/a.ts] + + +//// [/apath/b.js] + + +//// [/apath/tsconfig.json] +{ + "compilerOptions": { + "module": { + "preset": "nodenext", + "formatDetection": "defaultmodule" + } + } + } + + +configFileName:: tsconfig.json +CompilerOptions:: +{ + "module": { + "preset": 199, + "formatDetection": 2 + }, + "configFilePath": "tsconfig.json" +} +Errors:: + diff --git a/tests/baselines/reference/config/tsconfigParsing/module as object jsonParse.js b/tests/baselines/reference/config/tsconfigParsing/module as object jsonParse.js new file mode 100644 index 00000000000..45559f1f39f --- /dev/null +++ b/tests/baselines/reference/config/tsconfigParsing/module as object jsonParse.js @@ -0,0 +1,20 @@ +Input:: +{ + "compilerOptions": { + "module": { + "preset": "nodenext", + "formatDetection": "defaultmodule", + }, + }, + } +Config:: +{ + "compilerOptions": { + "module": { + "preset": "nodenext", + "formatDetection": "defaultmodule" + } + } +} +Errors:: +