Skip to content

Commit 9874733

Browse files
Copilotjakebailey
andauthored
Handle null enum values in tsconfig parsing (#4105)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent bef4582 commit 9874733

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

internal/tsoptions/tsconfigparsing.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ func convertJsonOption(
478478
return convertJsonOption(opt.Elements(), value, basePath, propertyAssignment, valueExpression, sourceFile)
479479
}
480480
case CommandLineOptionTypeEnum:
481+
if value == nil {
482+
return nil, nil
483+
}
481484
return convertJsonOptionOfEnumType(opt, value.(string), valueExpression, sourceFile)
482485
}
483486

@@ -633,9 +636,16 @@ func convertOptionsFromJson[O optionParser](optionsNameMap CommandLineOptionName
633636

634637
commandLineOptionEnumMapVal := opt.EnumMap()
635638
if commandLineOptionEnumMapVal != nil {
636-
val, ok := commandLineOptionEnumMapVal.Get(strings.ToLower(value.(string)))
637-
if ok {
638-
errors = result.ParseOption(key, val)
639+
if value, ok := value.(string); ok {
640+
val, ok := commandLineOptionEnumMapVal.Get(strings.ToLower(value))
641+
if ok {
642+
errors = result.ParseOption(key, val)
643+
}
644+
} else {
645+
convertJson, err := convertJsonOption(opt, value, basePath, nil, nil, nil)
646+
errors = append(errors, err...)
647+
compilerOptionsErr := result.ParseOption(key, convertJson)
648+
errors = append(errors, compilerOptionsErr...)
639649
}
640650
} else {
641651
convertJson, err := convertJsonOption(opt, value, basePath, nil, nil, nil)

internal/tsoptions/tsconfigparsing_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,37 @@ func TestParseJsonSourceFileConfigFileContent(t *testing.T) {
829829
}
830830
}
831831

832+
func TestParseNullEnumCompilerOptions(t *testing.T) {
833+
t.Parallel()
834+
835+
config := testConfig{
836+
jsonText: `{
837+
"compilerOptions": {
838+
"target": null,
839+
"module": null
840+
}
841+
}`,
842+
configFileName: "tsconfig.json",
843+
basePath: "/",
844+
allFileList: map[string]string{"/app.ts": ""},
845+
}
846+
for name, getParsed := range map[string]func(testConfig, tsoptions.ParseConfigHost, string) *tsoptions.ParsedCommandLine{
847+
"json api": getParsedWithJsonApi,
848+
"jsonSourceFile api": getParsedWithJsonSourceFileApi,
849+
} {
850+
t.Run(name, func(t *testing.T) {
851+
t.Parallel()
852+
853+
allFileLists := make(map[string]string, len(config.allFileList)+1)
854+
maps.Copy(allFileLists, config.allFileList)
855+
allFileLists["/tsconfig.json"] = config.jsonText
856+
host := tsoptionstest.NewVFSParseConfigHost(allFileLists, config.basePath, true /*useCaseSensitiveFileNames*/)
857+
parsedConfigFileContent := getParsed(config, host, config.basePath)
858+
assert.Equal(t, len(parsedConfigFileContent.Errors), 0)
859+
})
860+
}
861+
}
862+
832863
func getParsedWithJsonSourceFileApi(config testConfig, host tsoptions.ParseConfigHost, basePath string) *tsoptions.ParsedCommandLine {
833864
configFileName := tspath.GetNormalizedAbsolutePath(config.configFileName, basePath)
834865
path := tspath.ToPath(config.configFileName, basePath, host.FS().UseCaseSensitiveFileNames())

0 commit comments

Comments
 (0)