Skip to content

Commit 368a8e5

Browse files
authored
Fix parsing of enum options (#593)
1 parent eeaaeb6 commit 368a8e5

6 files changed

+87
-18
lines changed

internal/tsoptions/tsconfigparsing.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,7 @@ func parseOwnConfigOfJsonSourceFile(
139139
}
140140
if parentOption != nil && parentOption.Name != "undefined" && value != nil {
141141
if option != nil && option.Name != "" {
142-
commandLineOptionEnumMapVal := option.EnumMap()
143-
if commandLineOptionEnumMapVal != nil {
144-
val, ok := commandLineOptionEnumMapVal.Get(strings.ToLower(value.(string)))
145-
if ok {
146-
propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, val, options)...)
147-
}
148-
} else {
149-
propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, value, options)...)
150-
}
142+
propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, value, options)...)
151143
} else if keyText != "" {
152144
if parentOption.ElementOptions != nil {
153145
// !!! TODO: support suggestion
@@ -276,8 +268,7 @@ func isCompilerOptionsValue(option *CommandLineOption, value any) bool {
276268
return reflect.TypeOf(value) == orderedMapType
277269
}
278270
if option.Kind == "enum" && reflect.TypeOf(value).Kind() == reflect.String {
279-
_, ok := option.EnumMap().Get(strings.ToLower(value.(string)))
280-
return ok || (option.DeprecatedKeys() != nil && option.DeprecatedKeys().Has(strings.ToLower(value.(string))))
271+
return true
281272
}
282273
}
283274
return false
@@ -376,18 +367,19 @@ func convertJsonOption(
376367
}
377368
}
378369
if isCompilerOptionsValue(opt, value) {
379-
optType := opt.Kind
380-
if optType == "list" {
370+
switch opt.Kind {
371+
case CommandLineOptionTypeList:
381372
return convertJsonOptionOfListType(opt, value, basePath, propertyAssignment, valueExpression, sourceFile) // as ArrayLiteralExpression | undefined
382-
} else if optType == "listOrElement" {
373+
case CommandLineOptionTypeListOrElement:
383374
if reflect.TypeOf(value).Kind() == reflect.Slice {
384375
return convertJsonOptionOfListType(opt, value, basePath, propertyAssignment, valueExpression, sourceFile)
385376
} else {
386377
return convertJsonOption(opt.Elements(), value, basePath, propertyAssignment, valueExpression, sourceFile)
387378
}
388-
} else if !(reflect.TypeOf(optType).Kind() == reflect.String) {
379+
case CommandLineOptionTypeEnum:
389380
return convertJsonOptionOfEnumType(opt, value.(string), valueExpression, sourceFile)
390381
}
382+
391383
validatedValue, errors := validateJsonOptionValue(opt, value, valueExpression, sourceFile)
392384
if len(errors) > 0 || validatedValue == nil {
393385
return validatedValue, errors

internal/tsoptions/tsconfigparsing_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,21 @@ var parseJsonConfigFileTests = []struct {
480480
allFileList: map[string]string{"/app.ts": ""},
481481
}},
482482
},
483+
{
484+
title: "reports errors for wrong type option and invalid enum value",
485+
input: []testConfig{{
486+
jsonText: `{
487+
"compilerOptions": {
488+
"target": "invalid value",
489+
"removeComments": "should be a boolean",
490+
"moduleResolution": "invalid value"
491+
}
492+
}`,
493+
configFileName: "tsconfig.json",
494+
basePath: "/",
495+
allFileList: map[string]string{"/app.ts": ""},
496+
}},
497+
},
483498
}
484499

485500
var tsconfigWithExtends = `{
@@ -702,7 +717,7 @@ func TestParseSrcCompiler(t *testing.T) {
702717

703718
opts := parseConfigFileContent.CompilerOptions()
704719
assert.DeepEqual(t, opts, &core.CompilerOptions{
705-
Lib: []string{"es2020"},
720+
Lib: []string{"lib.es2020.d.ts"},
706721
ModuleKind: core.ModuleKindNodeNext,
707722
ModuleResolution: core.ModuleResolutionKindNodeNext,
708723
NewLine: core.NewLineKindLF,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Fs::
2+
//// [/app.ts]
3+
4+
5+
//// [/tsconfig.json]
6+
{
7+
"compilerOptions": {
8+
"target": "invalid value",
9+
"removeComments": "should be a boolean",
10+
"moduleResolution": "invalid value"
11+
}
12+
}
13+
14+
15+
configFileName:: tsconfig.json
16+
CompilerOptions::
17+
{
18+
"configFilePath": "/tsconfig.json"
19+
}
20+
21+
FileNames::
22+
/app.ts
23+
Errors::
24+
error TS5024: Compiler option 'removeComments' requires a value of type boolean.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Fs::
2+
//// [/app.ts]
3+
4+
5+
//// [/tsconfig.json]
6+
{
7+
"compilerOptions": {
8+
"target": "invalid value",
9+
"removeComments": "should be a boolean",
10+
"moduleResolution": "invalid value"
11+
}
12+
}
13+
14+
15+
configFileName:: tsconfig.json
16+
CompilerOptions::
17+
{
18+
"configFilePath": "/tsconfig.json"
19+
}
20+
21+
FileNames::
22+
/app.ts
23+
Errors::
24+
tsconfig.json:3:15 - error TS6046: Argument for '--target' option must be: 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'es2022', 'es2023', 'esnext'.
25+
26+
3 "target": "invalid value",
27+
   ~~~~~~~~~~~~~~~
28+
29+
tsconfig.json:4:23 - error TS5024: Compiler option 'removeComments' requires a value of type boolean.
30+
31+
4 "removeComments": "should be a boolean",
32+
   ~~~~~~~~~~~~~~~~~~~~~
33+
34+
tsconfig.json:5:25 - error TS6046: Argument for '--moduleResolution' option must be: 'node16', 'nodenext', 'bundler'.
35+
36+
5 "moduleResolution": "invalid value"
37+
   ~~~~~~~~~~~~~~~
38+

testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with json api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ configFileName:: tsconfig.json
2020
CompilerOptions::
2121
{
2222
"lib": [
23-
"es5"
23+
"lib.es5.d.ts"
2424
],
2525
"configFilePath": "/apath/tsconfig.json"
2626
}

testdata/baselines/reference/config/tsconfigParsing/returns error when tsconfig have excludes with jsonSourceFile api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ configFileName:: tsconfig.json
2020
CompilerOptions::
2121
{
2222
"lib": [
23-
"es5"
23+
"lib.es5.d.ts"
2424
],
2525
"configFilePath": "/apath/tsconfig.json"
2626
}

0 commit comments

Comments
 (0)