Skip to content

add unknown option error for TypeAcquisition #859

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 5 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 33 additions & 3 deletions internal/tsoptions/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,26 @@ func (parser *commandLineParser) createUnknownOptionError(
unknownOption string,
unknownOptionErrorText string,
node *ast.Node,
sourceFile *ast.SourceFile, // todo: TsConfigSourceFile,
sourceFile *ast.SourceFile,
) *ast.Diagnostic {
return createUnknownOptionError(
unknownOption,
parser.UnknownOptionDiagnostic(),
unknownOptionErrorText,
node,
sourceFile,
parser.AlternateMode(),
)
}

func createUnknownOptionError(
unknownOption string,
unknownOptionDiagnostic *diagnostics.Message,
unknownOptionErrorText string, // optional
node *ast.Node, // optional
sourceFile *ast.SourceFile, // optional
alternateMode *AlternateModeDiagnostics, // optional
) *ast.Diagnostic {
alternateMode := parser.AlternateMode()
if alternateMode != nil && alternateMode.optionsNameMap != nil {
otherOption := alternateMode.optionsNameMap.Get(strings.ToLower(unknownOption))
if otherOption != nil {
Expand All @@ -58,7 +75,7 @@ func (parser *commandLineParser) createUnknownOptionError(
unknownOptionErrorText = unknownOption
}
// TODO: possibleOption := spelling suggestion
return createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, node, parser.UnknownOptionDiagnostic(), unknownOptionErrorText)
return createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, node, unknownOptionDiagnostic, unknownOptionErrorText)
}

func createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile *ast.SourceFile, node *ast.Node, message *diagnostics.Message, args ...any) *ast.Diagnostic {
Expand All @@ -67,3 +84,16 @@ func createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile *ast.Sou
}
return ast.NewCompilerDiagnostic(message, args...)
}

func extraKeyDiagnostics(s string) *diagnostics.Message {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i meant here shouldnt this be from parser.unknownDiagnostics ? i dont understand why we have two ways of doing same thing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are implemented in two different ways because of how it was implemented in strada, and then the two different code paths that use the messages.
When we are parsing from a map to a struct, we only have an optionParser, we don't know what type of optionParser it is, so we can't use extraKeyDiagnostics directly.
When we are parsing the tsconfig, we don't use optionParser, so we are not able call optionsParser.UnknownOptionDiagnostic().

I could refactor each of the optionsParser.UnknownOptionDiagnostic() to be like

func (o *typeAcquisitionParser) UnknownOptionDiagnostic() *diagnostics.Message {
	return extraKeyDiagnostics("typeAcquisition")
}

but I'm not sure if it's worth the indirection

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it some more, and ended up doing that refactor

switch s {
case "compilerOptions":
return diagnostics.Unknown_compiler_option_0
case "watchOptions":
return diagnostics.Unknown_watch_option_0
case "typeAcquisition":
return diagnostics.Unknown_type_acquisition_option_0
default:
return nil
}
}
14 changes: 14 additions & 0 deletions internal/tsoptions/parsinghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/tspath"
)

Expand Down Expand Up @@ -118,6 +119,7 @@ func parseJsonToStringKey(json any) *collections.OrderedMap[string, any] {

type optionParser interface {
ParseOption(key string, value any) []*ast.Diagnostic
UnknownOptionDiagnostic() *diagnostics.Message
}

type compilerOptionsParser struct {
Expand All @@ -128,6 +130,10 @@ func (o *compilerOptionsParser) ParseOption(key string, value any) []*ast.Diagno
return ParseCompilerOptions(key, value, o.CompilerOptions)
}

func (o *compilerOptionsParser) UnknownOptionDiagnostic() *diagnostics.Message {
return diagnostics.Unknown_compiler_option_0
}

type watchOptionsParser struct {
*core.WatchOptions
}
Expand All @@ -136,6 +142,10 @@ func (o *watchOptionsParser) ParseOption(key string, value any) []*ast.Diagnosti
return ParseWatchOptions(key, value, o.WatchOptions)
}

func (o *watchOptionsParser) UnknownOptionDiagnostic() *diagnostics.Message {
return diagnostics.Unknown_watch_option_0
}

type typeAcquisitionParser struct {
*core.TypeAcquisition
}
Expand All @@ -144,6 +154,10 @@ func (o *typeAcquisitionParser) ParseOption(key string, value any) []*ast.Diagno
return ParseTypeAcquisition(key, value, o.TypeAcquisition)
}

func (o *typeAcquisitionParser) UnknownOptionDiagnostic() *diagnostics.Message {
return diagnostics.Unknown_type_acquisition_option_0
}

func ParseCompilerOptions(key string, value any, allOptions *core.CompilerOptions) []*ast.Diagnostic {
if value == nil {
return nil
Expand Down
14 changes: 11 additions & 3 deletions internal/tsoptions/tsconfigparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ func parseOwnConfigOfJsonSourceFile(
parseDiagnostics = ParseTypeAcquisition(option.Name, value, typeAcquisition)
}
propertySetErrors = append(propertySetErrors, parseDiagnostics...)
} else if keyText != "" {
} else if keyText != "" && extraKeyDiagnostics(parentOption.Name) != nil {
unknownNameDiag := extraKeyDiagnostics(parentOption.Name)
if parentOption.ElementOptions != nil {
// !!! TODO: support suggestion
propertySetErrors = append(propertySetErrors, createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, propertyAssignment.Name(), diagnostics.Unknown_compiler_option_0, keyText))
propertySetErrors = append(propertySetErrors, createUnknownOptionError(
keyText,
unknownNameDiag,
"", /*unknownOptionErrorText*/
propertyAssignment.Name(),
sourceFile,
nil, /*alternateMode*/
))
} else {
// errors = append(errors, ast.NewCompilerDiagnostic(diagnostics.Unknown_compiler_option_0_Did_you_mean_1, keyText, core.FindKey(parentOption.ElementOptions, keyText)))
}
Expand Down Expand Up @@ -520,7 +528,7 @@ func convertOptionsFromJson[O optionParser](optionsNameMap map[string]*CommandLi
opt, ok := optionsNameMap[key]
if !ok {
// !!! TODO?: support suggestion
errors = append(errors, ast.NewCompilerDiagnostic(diagnostics.Unknown_compiler_option_0, key))
errors = append(errors, createUnknownOptionError(key, result.UnknownOptionDiagnostic(), "", nil, nil, nil))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt this just result.UnknownOptionDiagnostic() after adding that method to optionParser

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result.UnknownOptionDiagnostic() only returns the diagnostic message. We need the call to create the full error (and format if needed)

continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ TypeAcquisition::
FileNames::
/apath/a.ts,/apath/b.ts
Errors::
error TS5023: Unknown compiler option 'enableAutoDiscovy'.
error TS17010: Unknown type acquisition option 'enableAutoDiscovy'.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TypeAcquisition::
FileNames::
/apath/a.ts,/apath/b.ts
Errors::
jsconfig.json:3:3 - error TS5023: Unknown compiler option 'enableAutoDiscovy'.
jsconfig.json:3:3 - error TS17010: Unknown type acquisition option 'enableAutoDiscovy'.

3 "enableAutoDiscovy": true,
   ~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ TypeAcquisition::
FileNames::
/apath/a.ts,/apath/b.ts
Errors::
error TS5023: Unknown compiler option 'enableAutoDiscovy'.
error TS17010: Unknown type acquisition option 'enableAutoDiscovy'.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TypeAcquisition::
FileNames::
/apath/a.ts,/apath/b.ts
Errors::
tsconfig.json:3:3 - error TS5023: Unknown compiler option 'enableAutoDiscovy'.
tsconfig.json:3:3 - error TS17010: Unknown type acquisition option 'enableAutoDiscovy'.

3 "enableAutoDiscovy": true,
   ~~~~~~~~~~~~~~~~~~~
Expand Down