diff --git a/internal/tsoptions/tsconfigparsing.go b/internal/tsoptions/tsconfigparsing.go index faad993acd..fbcb75e507 100644 --- a/internal/tsoptions/tsconfigparsing.go +++ b/internal/tsoptions/tsconfigparsing.go @@ -139,15 +139,7 @@ func parseOwnConfigOfJsonSourceFile( } if parentOption != nil && parentOption.Name != "undefined" && value != nil { if option != nil && option.Name != "" { - commandLineOptionEnumMapVal := option.EnumMap() - if commandLineOptionEnumMapVal != nil { - val, ok := commandLineOptionEnumMapVal.Get(strings.ToLower(value.(string))) - if ok { - propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, val, options)...) - } - } else { - propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, value, options)...) - } + propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, value, options)...) } else if keyText != "" { if parentOption.ElementOptions != nil { // !!! TODO: support suggestion @@ -276,8 +268,7 @@ func isCompilerOptionsValue(option *CommandLineOption, value any) bool { return reflect.TypeOf(value) == orderedMapType } if option.Kind == "enum" && reflect.TypeOf(value).Kind() == reflect.String { - _, ok := option.EnumMap().Get(strings.ToLower(value.(string))) - return ok || (option.DeprecatedKeys() != nil && option.DeprecatedKeys().Has(strings.ToLower(value.(string)))) + return true } } return false @@ -376,18 +367,19 @@ func convertJsonOption( } } if isCompilerOptionsValue(opt, value) { - optType := opt.Kind - if optType == "list" { + switch opt.Kind { + case CommandLineOptionTypeList: return convertJsonOptionOfListType(opt, value, basePath, propertyAssignment, valueExpression, sourceFile) // as ArrayLiteralExpression | undefined - } else if optType == "listOrElement" { + case CommandLineOptionTypeListOrElement: if reflect.TypeOf(value).Kind() == reflect.Slice { return convertJsonOptionOfListType(opt, value, basePath, propertyAssignment, valueExpression, sourceFile) } else { return convertJsonOption(opt.Elements(), value, basePath, propertyAssignment, valueExpression, sourceFile) } - } else if !(reflect.TypeOf(optType).Kind() == reflect.String) { + case CommandLineOptionTypeEnum: return convertJsonOptionOfEnumType(opt, value.(string), valueExpression, sourceFile) } + validatedValue, errors := validateJsonOptionValue(opt, value, valueExpression, sourceFile) if len(errors) > 0 || validatedValue == nil { return validatedValue, errors diff --git a/internal/tsoptions/tsconfigparsing_test.go b/internal/tsoptions/tsconfigparsing_test.go index da38d48d99..49ddbbb667 100644 --- a/internal/tsoptions/tsconfigparsing_test.go +++ b/internal/tsoptions/tsconfigparsing_test.go @@ -480,6 +480,21 @@ var parseJsonConfigFileTests = []struct { allFileList: map[string]string{"/app.ts": ""}, }}, }, + { + title: "reports errors for wrong type option and invalid enum value", + input: []testConfig{{ + jsonText: `{ + "compilerOptions": { + "target": "invalid value", + "removeComments": "should be a boolean", + "moduleResolution": "invalid value" + } + }`, + configFileName: "tsconfig.json", + basePath: "/", + allFileList: map[string]string{"/app.ts": ""}, + }}, + }, } var tsconfigWithExtends = `{ @@ -702,7 +717,7 @@ func TestParseSrcCompiler(t *testing.T) { opts := parseConfigFileContent.CompilerOptions() assert.DeepEqual(t, opts, &core.CompilerOptions{ - Lib: []string{"es2020"}, + Lib: []string{"lib.es2020.d.ts"}, ModuleKind: core.ModuleKindNodeNext, ModuleResolution: core.ModuleResolutionKindNodeNext, NewLine: core.NewLineKindLF, diff --git a/testdata/baselines/reference/config/tsconfigParsing/reports errors for wrong type option and invalid enum value with json api.js b/testdata/baselines/reference/config/tsconfigParsing/reports errors for wrong type option and invalid enum value with json api.js new file mode 100644 index 0000000000..78f5175eef --- /dev/null +++ b/testdata/baselines/reference/config/tsconfigParsing/reports errors for wrong type option and invalid enum value with json api.js @@ -0,0 +1,24 @@ +Fs:: +//// [/app.ts] + + +//// [/tsconfig.json] +{ + "compilerOptions": { + "target": "invalid value", + "removeComments": "should be a boolean", + "moduleResolution": "invalid value" + } + } + + +configFileName:: tsconfig.json +CompilerOptions:: +{ + "configFilePath": "/tsconfig.json" +} + +FileNames:: +/app.ts +Errors:: +error TS5024: Compiler option 'removeComments' requires a value of type boolean. diff --git a/testdata/baselines/reference/config/tsconfigParsing/reports errors for wrong type option and invalid enum value with jsonSourceFile api.js b/testdata/baselines/reference/config/tsconfigParsing/reports errors for wrong type option and invalid enum value with jsonSourceFile api.js new file mode 100644 index 0000000000..10a64ebb70 --- /dev/null +++ b/testdata/baselines/reference/config/tsconfigParsing/reports errors for wrong type option and invalid enum value with jsonSourceFile api.js @@ -0,0 +1,38 @@ +Fs:: +//// [/app.ts] + + +//// [/tsconfig.json] +{ + "compilerOptions": { + "target": "invalid value", + "removeComments": "should be a boolean", + "moduleResolution": "invalid value" + } + } + + +configFileName:: tsconfig.json +CompilerOptions:: +{ + "configFilePath": "/tsconfig.json" +} + +FileNames:: +/app.ts +Errors:: +tsconfig.json:3:15 - error TS6046: Argument for '--target' option must be: 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'es2022', 'es2023', 'esnext'. + +3 "target": "invalid value", +   ~~~~~~~~~~~~~~~ + +tsconfig.json:4:23 - error TS5024: Compiler option 'removeComments' requires a value of type boolean. + +4 "removeComments": "should be a boolean", +   ~~~~~~~~~~~~~~~~~~~~~ + +tsconfig.json:5:25 - error TS6046: Argument for '--moduleResolution' option must be: 'node16', 'nodenext', 'bundler'. + +5 "moduleResolution": "invalid value" +   ~~~~~~~~~~~~~~~ + diff --git a/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with json api.js b/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with json api.js index 334ebabd3a..20d1945437 100644 --- a/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with json api.js +++ b/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with json api.js @@ -20,7 +20,7 @@ configFileName:: tsconfig.json CompilerOptions:: { "lib": [ - "es5" + "lib.es5.d.ts" ], "configFilePath": "/apath/tsconfig.json" } diff --git a/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with jsonSourceFile api.js b/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with jsonSourceFile api.js index af3103c206..bf6a9b12ab 100644 --- a/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with jsonSourceFile api.js +++ b/testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with jsonSourceFile api.js @@ -20,7 +20,7 @@ configFileName:: tsconfig.json CompilerOptions:: { "lib": [ - "es5" + "lib.es5.d.ts" ], "configFilePath": "/apath/tsconfig.json" }