Skip to content

Improve the missing CompilerOptions check #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/tsoptions/parsinghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func ParseCompilerOptions(key string, value any, allOptions *core.CompilerOption
if allOptions == nil {
return nil
}
parseCompilerOptions(key, value, allOptions)
return nil
}

func parseCompilerOptions(key string, value any, allOptions *core.CompilerOptions) (foundKey bool) {
switch key {
case "allowJs":
allOptions.AllowJs = parseTristate(value)
Expand Down Expand Up @@ -255,6 +260,8 @@ func ParseCompilerOptions(key string, value any, allOptions *core.CompilerOption
allOptions.MapRoot = parseString(value)
case "module":
allOptions.ModuleKind = value.(core.ModuleKind)
case "moduleDetectionKind":
allOptions.ModuleDetection = value.(core.ModuleDetectionKind)
case "moduleResolution":
allOptions.ModuleResolution = value.(core.ModuleResolutionKind)
case "moduleSuffixes":
Expand Down Expand Up @@ -393,8 +400,11 @@ func ParseCompilerOptions(key string, value any, allOptions *core.CompilerOption
allOptions.NewLine = value.(core.NewLineKind)
case "watch":
allOptions.Watch = parseTristate(value)
default:
// different than any key above
return false
}
return nil
return true
}

func ParseWatchOptions(key string, value any, allOptions *core.WatchOptions) []*ast.Diagnostic {
Expand Down
23 changes: 7 additions & 16 deletions internal/tsoptions/parsinghelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"github.com/microsoft/typescript-go/internal/core"
)

func TestParseCompilerOptionNoMissingTristates(t *testing.T) {
func TestParseCompilerOptionNoMissingFields(t *testing.T) {
t.Parallel()

var missingKeys []string
for _, field := range reflect.VisibleFields(reflect.TypeFor[core.CompilerOptions]()) {
keyName := field.Name
Expand All @@ -19,23 +18,15 @@ func TestParseCompilerOptionNoMissingTristates(t *testing.T) {
if jsonTag, ok := field.Tag.Lookup("json"); ok {
keyName = strings.SplitN(jsonTag, ",", 2)[0]
}

isTristate := field.Type == reflect.TypeFor[core.Tristate]()
if isTristate {
// Set the field on a CompilerOptions to something other than the
// default (i.e. not TSUnknown), then check whether
// ParseCompilerOptions does actually update the value for that key.
testValue := core.TSTrue
co := core.CompilerOptions{}
ParseCompilerOptions(keyName, testValue, &co)
newSetValue := reflect.ValueOf(co).FieldByName(field.Name)
if !newSetValue.Equal(reflect.ValueOf(testValue)) {
missingKeys = append(missingKeys, keyName)
}
val := reflect.Zero(field.Type).Interface()
co := core.CompilerOptions{}
found := parseCompilerOptions(keyName, val, &co)
if !found {
missingKeys = append(missingKeys, keyName)
}
}
if len(missingKeys) > 0 {
t.Errorf("The following Tristate keys are missing entries in the ParseCompilerOptions"+
t.Errorf("The following keys are missing entries in the ParseCompilerOptions"+
" switch statement:\n%v", missingKeys)
}
}