Skip to content
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
22 changes: 7 additions & 15 deletions internal/tsoptions/tsconfigparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Strada uses a Map as type of enum option and use !isString(opt.type) as condition of this branch. However tsgo use CommandLineOptionKind for CommandLineOption.Kind whose underlying type is string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is the bug you referred to in another comment. 🤔 @jakebailey

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
Expand Down
17 changes: 16 additions & 1 deletion internal/tsoptions/tsconfigparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `{
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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"
   ~~~~~~~~~~~~~~~

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ configFileName:: tsconfig.json
CompilerOptions::
{
"lib": [
"es5"
"lib.es5.d.ts"
],
"configFilePath": "/apath/tsconfig.json"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ configFileName:: tsconfig.json
CompilerOptions::
{
"lib": [
"es5"
"lib.es5.d.ts"
],
"configFilePath": "/apath/tsconfig.json"
}
Expand Down