From c78bc58f1b97eaeaea4a464915a33fffea0ed298 Mon Sep 17 00:00:00 2001 From: haoqixu Date: Sat, 8 Nov 2025 00:57:16 +0800 Subject: [PATCH 1/4] port tsc --init --- internal/execute/tsc.go | 3 +- internal/execute/tsc/init.go | 238 ++++++++++++++++++ internal/execute/tsctests/tsc_test.go | 22 ++ .../tsc/commandLine/init-with---lib-esnext.js | 59 +++++ .../commandLine/init-with-tsconfig.json.js | 18 ++ .../reference/tsc/commandLine/init.js | 58 +++++ 6 files changed, 397 insertions(+), 1 deletion(-) create mode 100644 internal/execute/tsc/init.go create mode 100644 testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js create mode 100644 testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js create mode 100644 testdata/baselines/reference/tsc/commandLine/init.js diff --git a/internal/execute/tsc.go b/internal/execute/tsc.go index d4e56103bc..d2184891c8 100644 --- a/internal/execute/tsc.go +++ b/internal/execute/tsc.go @@ -113,7 +113,8 @@ func tscCompilation(sys tsc.System, commandLine *tsoptions.ParsedCommandLine, te } if commandLine.CompilerOptions().Init.IsTrue() { - return tsc.CommandLineResult{Status: tsc.ExitStatusNotImplemented} + tsc.WriteConfigFile(sys, reportDiagnostic, commandLine.CompilerOptions()) + return tsc.CommandLineResult{Status: tsc.ExitStatusSuccess} } if commandLine.CompilerOptions().Version.IsTrue() { diff --git a/internal/execute/tsc/init.go b/internal/execute/tsc/init.go new file mode 100644 index 0000000000..84697bfe7c --- /dev/null +++ b/internal/execute/tsc/init.go @@ -0,0 +1,238 @@ +package tsc + +import ( + "fmt" + "reflect" + "slices" + "strings" + + "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/jsonutil" + "github.com/microsoft/typescript-go/internal/tsoptions" + "github.com/microsoft/typescript-go/internal/tspath" +) + +func WriteConfigFile(sys System, reportDiagnostic DiagnosticReporter, options *core.CompilerOptions) { + getCurrentDirectory := sys.GetCurrentDirectory() + file := tspath.NormalizePath(tspath.CombinePaths(getCurrentDirectory, "tsconfig.json")) + if sys.FS().FileExists(file) { + reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.A_tsconfig_json_file_is_already_defined_at_Colon_0, file)) + } else { + sys.FS().WriteFile(file, generateTSConfig(options), false) + output := []string{"\n"} + output = append(output, getHeader(sys, "Created a new tsconfig.json")...) + output = append(output, "You can learn more at https://aka.ms/tsconfig", "\n") + fmt.Fprint(sys.Writer(), strings.Join(output, "")) + } +} + +func convertOptionsToMap(options *core.CompilerOptions) *collections.OrderedMap[string, any] { + val := reflect.ValueOf(options).Elem() + typ := val.Type() + + result := collections.NewOrderedMapWithSizeHint[string, any](val.NumField()) + + for i := 0; i < val.NumField(); i++ { + field := typ.Field(i) + fieldValue := val.Field(i) + + if fieldValue.IsZero() { + continue + } + + // Get the field name, considering 'json' tag if present + fieldName := field.Name + if jsonTag := field.Tag.Get("json"); jsonTag != "" { + fieldName, _, _ = strings.Cut(jsonTag, ",") + } + + if fieldName != "" && fieldName != "init" && fieldName != "help" && fieldName != "watch" { + result.Set(fieldName, fieldValue.Interface()) + } + } + return result +} + +func generateTSConfig(options *core.CompilerOptions) string { + const tab = " " + var result []string + + optionsMap := convertOptionsToMap(options) + allSetOptions := slices.Collect(optionsMap.Keys()) + + // !!! locale getLocaleSpecificMessage + emitHeader := func(header *diagnostics.Message) { + result = append(result, tab+tab+"// "+header.Format()) + } + newline := func() { + result = append(result, "") + } + push := func(args ...string) { + result = append(result, args...) + } + + formatSingleValue := func(value any, enumMap *collections.OrderedMap[string, any]) string { + if enumMap != nil { + var found bool + for k, v := range enumMap.Entries() { + if value == v { + value = k + found = true + break + } + } + if !found { + panic(fmt.Sprintf("No matching value of %v", value)) + } + } + + b, err := jsonutil.MarshalIndent(value, "", "") + if err != nil { + panic(fmt.Sprintf("should not happen: %v", err)) + } + return string(b) + } + + formatValueOrArray := func(settingName string, value any) string { + var option *tsoptions.CommandLineOption + for _, decl := range tsoptions.OptionsDeclarations { + if decl.Name == settingName { + option = decl + } + } + if option == nil { + panic(`No option named ` + settingName) + } + + rval := reflect.ValueOf(value) + if rval.Kind() == reflect.Slice { + var enumMap *collections.OrderedMap[string, any] + if elemOption := option.Elements(); elemOption != nil { + enumMap = elemOption.EnumMap() + } + + var elems []string + for i := 0; i < rval.Len(); i++ { + elems = append(elems, formatSingleValue(rval.Index(i).Interface(), enumMap)) + } + return `[` + strings.Join(elems, ", ") + `]` + } else { + return formatSingleValue(value, option.EnumMap()) + } + } + + // commentedNever': Never comment this out + // commentedAlways': Always comment this out, even if it's on commandline + // commentedOptional': Comment out unless it's on commandline + const ( + commentedNever = 0 + commentedAlways = 1 + commentedOptional = 2 + ) + emitOption := func(setting string, defaultValue any, commented int) { + if commented > 2 { + panic("should not happen: invalid `commented`, must be a bug.") + } + + existingOptionIndex := slices.Index(allSetOptions, setting) + if existingOptionIndex >= 0 { + allSetOptions = slices.Delete(allSetOptions, existingOptionIndex, existingOptionIndex+1) + } + + var comment bool + switch commented { + case commentedAlways: + comment = true + case commentedNever: + comment = false + default: + comment = !optionsMap.Has(setting) + } + + value, ok := optionsMap.Get(setting) + if !ok { + value = defaultValue + } + + if comment { + push(tab + tab + `// "` + setting + `": ` + formatValueOrArray(setting, value) + `,`) + } else { + push(tab + tab + `"` + setting + `": ` + formatValueOrArray(setting, value) + `,`) + } + } + + push("{") + // !!! locale getLocaleSpecificMessage + push(tab + `// ` + diagnostics.Visit_https_Colon_Slash_Slashaka_ms_Slashtsconfig_to_read_more_about_this_file.Format()) + push(tab + `"compilerOptions": {`) + + emitHeader(diagnostics.File_Layout) + emitOption("rootDir", "./src", commentedOptional) + emitOption("outDir", "./dist", commentedOptional) + + newline() + + emitHeader(diagnostics.Environment_Settings) + emitHeader(diagnostics.See_also_https_Colon_Slash_Slashaka_ms_Slashtsconfig_Slashmodule) + emitOption("module", core.ModuleKindNodeNext, commentedNever) + emitOption("target", core.ScriptTargetESNext, commentedNever) + emitOption("types", []any{}, commentedNever) + if len(options.Lib) != 0 { + emitOption("lib", options.Lib, commentedNever) + } + emitHeader(diagnostics.For_nodejs_Colon) + push(tab + tab + `// "lib": ["esnext"],`) + push(tab + tab + `// "types": ["node"],`) + emitHeader(diagnostics.X_and_npm_install_D_types_Slashnode) + + newline() + + emitHeader(diagnostics.Other_Outputs) + emitOption("sourceMap" /*defaultValue*/, true, commentedNever) + emitOption("declaration" /*defaultValue*/, true, commentedNever) + emitOption("declarationMap" /*defaultValue*/, true, commentedNever) + + newline() + + emitHeader(diagnostics.Stricter_Typechecking_Options) + emitOption("noUncheckedIndexedAccess" /*defaultValue*/, true, commentedNever) + emitOption("exactOptionalPropertyTypes" /*defaultValue*/, true, commentedNever) + + newline() + + emitHeader(diagnostics.Style_Options) + emitOption("noImplicitReturns" /*defaultValue*/, true, commentedOptional) + emitOption("noImplicitOverride" /*defaultValue*/, true, commentedOptional) + emitOption("noUnusedLocals" /*defaultValue*/, true, commentedOptional) + emitOption("noUnusedParameters" /*defaultValue*/, true, commentedOptional) + emitOption("noFallthroughCasesInSwitch" /*defaultValue*/, true, commentedOptional) + emitOption("noPropertyAccessFromIndexSignature" /*defaultValue*/, true, commentedOptional) + + newline() + + emitHeader(diagnostics.Recommended_Options) + emitOption("strict" /*defaultValue*/, true, commentedNever) + emitOption("jsx", core.JsxEmitReactJSX, commentedNever) + emitOption("verbatimModuleSyntax" /*defaultValue*/, true, commentedNever) + emitOption("isolatedModules" /*defaultValue*/, true, commentedNever) + emitOption("noUncheckedSideEffectImports" /*defaultValue*/, true, commentedNever) + emitOption("moduleDetection", core.ModuleDetectionKindForce, commentedNever) + emitOption("skipLibCheck" /*defaultValue*/, true, commentedNever) + + // Write any user-provided options we haven't already + if len(allSetOptions) > 0 { + newline() + for len(allSetOptions) > 0 { + emitOption(allSetOptions[0], optionsMap.GetOrZero(allSetOptions[0]), commentedNever) + } + } + + push(tab + "}") + push(`}`) + push(``) + + return strings.Join(result, "\n") +} diff --git a/internal/execute/tsctests/tsc_test.go b/internal/execute/tsctests/tsc_test.go index df7e4e39b1..1a142e6ffe 100644 --- a/internal/execute/tsctests/tsc_test.go +++ b/internal/execute/tsctests/tsc_test.go @@ -36,6 +36,28 @@ func TestTscCommandline(t *testing.T) { subScenario: "when build not first argument", commandLineArgs: []string{"--verbose", "--build"}, }, + { + subScenario: "init", + commandLineArgs: []string{"--init"}, + }, + { + subScenario: "init with --lib esnext", + commandLineArgs: []string{"--init", "--lib", "esnext"}, + }, + { + subScenario: "init with tsconfig.json", + commandLineArgs: []string{"--init"}, + files: FileMap{ + "/home/src/workspaces/project/first.ts": `export const a = 1`, + "/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(` + { + "compilerOptions": { + "strict": true, + "noEmit": true + } + }`), + }, + }, { subScenario: "help", commandLineArgs: []string{"--help"}, diff --git a/testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js b/testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js new file mode 100644 index 0000000000..ab844b0100 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js @@ -0,0 +1,59 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --lib esnext +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + "lib": ["esnext"], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js b/testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js new file mode 100644 index 0000000000..543a4d4988 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js @@ -0,0 +1,18 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/project/first.ts] *new* +export const a = 1 +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + "compilerOptions": { + "strict": true, + "noEmit": true + } +} + +tsgo --init +ExitStatus:: Success +Output:: +error TS5054: A 'tsconfig.json' file is already defined at: '/home/src/workspaces/project/tsconfig.json'. + diff --git a/testdata/baselines/reference/tsc/commandLine/init.js b/testdata/baselines/reference/tsc/commandLine/init.js new file mode 100644 index 0000000000..76ed0bc973 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/init.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + From 5f5dfa1140d3f447ff70f508b86b21e0ca5e5228 Mon Sep 17 00:00:00 2001 From: haoqixu Date: Sat, 8 Nov 2025 01:33:44 +0800 Subject: [PATCH 2/4] fix lint --- internal/execute/tsc/init.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/execute/tsc/init.go b/internal/execute/tsc/init.go index 84697bfe7c..0ec2f2118c 100644 --- a/internal/execute/tsc/init.go +++ b/internal/execute/tsc/init.go @@ -21,7 +21,7 @@ func WriteConfigFile(sys System, reportDiagnostic DiagnosticReporter, options *c if sys.FS().FileExists(file) { reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.A_tsconfig_json_file_is_already_defined_at_Colon_0, file)) } else { - sys.FS().WriteFile(file, generateTSConfig(options), false) + _ = sys.FS().WriteFile(file, generateTSConfig(options), false) output := []string{"\n"} output = append(output, getHeader(sys, "Created a new tsconfig.json")...) output = append(output, "You can learn more at https://aka.ms/tsconfig", "\n") @@ -35,7 +35,7 @@ func convertOptionsToMap(options *core.CompilerOptions) *collections.OrderedMap[ result := collections.NewOrderedMapWithSizeHint[string, any](val.NumField()) - for i := 0; i < val.NumField(); i++ { + for i := range val.NumField() { field := typ.Field(i) fieldValue := val.Field(i) @@ -115,7 +115,7 @@ func generateTSConfig(options *core.CompilerOptions) string { } var elems []string - for i := 0; i < rval.Len(); i++ { + for i := range rval.Len() { elems = append(elems, formatSingleValue(rval.Index(i).Interface(), enumMap)) } return `[` + strings.Join(elems, ", ") + `]` From 671c40f21d2fae874f2e13c85782e7736b1fce81 Mon Sep 17 00:00:00 2001 From: haoqixu Date: Mon, 10 Nov 2025 11:29:52 +0800 Subject: [PATCH 3/4] add testcases from strada --- internal/execute/tsctests/tsc_test.go | 42 +++++++++++-- .../Initialized-TSConfig-with---help.js | 58 ++++++++++++++++++ ...tialized-TSConfig-with-advanced-options.js | 61 +++++++++++++++++++ ...fig-with-boolean-value-compiler-options.js | 58 ++++++++++++++++++ ...Config-with-enum-value-compiler-options.js | 58 ++++++++++++++++++ ...Initialized-TSConfig-with-files-options.js | 58 ++++++++++++++++++ ...ig-with-incorrect-compiler-option-value.js | 9 +++ ...TSConfig-with-incorrect-compiler-option.js | 9 +++ ...h-list-compiler-options-with-enum-value.js | 59 ++++++++++++++++++ ...zed-TSConfig-with-list-compiler-options.js | 58 ++++++++++++++++++ ...Initialized-TSConfig-with-tsconfig.json.js | 18 ++++++ .../Initialized-TSConfig-with---watch.js | 58 ++++++++++++++++++ 12 files changed, 541 insertions(+), 5 deletions(-) create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with---help.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-advanced-options.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-boolean-value-compiler-options.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-enum-value-compiler-options.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-files-options.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option-value.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options-with-enum-value.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options.js create mode 100644 testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-tsconfig.json.js create mode 100644 testdata/baselines/reference/tscWatch/commandLine/Initialized-TSConfig-with---watch.js diff --git a/internal/execute/tsctests/tsc_test.go b/internal/execute/tsctests/tsc_test.go index 1a142e6ffe..b5f1858f6c 100644 --- a/internal/execute/tsctests/tsc_test.go +++ b/internal/execute/tsctests/tsc_test.go @@ -37,15 +37,47 @@ func TestTscCommandline(t *testing.T) { commandLineArgs: []string{"--verbose", "--build"}, }, { - subScenario: "init", - commandLineArgs: []string{"--init"}, + subScenario: "Initialized TSConfig with files options", + commandLineArgs: []string{"--init", "file0.st", "file1.ts", "file2.ts"}, + }, + { + subScenario: "Initialized TSConfig with boolean value compiler options", + commandLineArgs: []string{"--init", "--noUnusedLocals"}, + }, + { + subScenario: "Initialized TSConfig with enum value compiler options", + commandLineArgs: []string{"--init", "--target", "es5", "--jsx", "react"}, + }, + { + subScenario: "Initialized TSConfig with list compiler options", + commandLineArgs: []string{"--init", "--types", "jquery,mocha"}, + }, + { + subScenario: "Initialized TSConfig with list compiler options with enum value", + commandLineArgs: []string{"--init", "--lib", "es5,es2015.core"}, + }, + { + subScenario: "Initialized TSConfig with incorrect compiler option", + commandLineArgs: []string{"--init", "--someNonExistOption"}, + }, + { + subScenario: "Initialized TSConfig with incorrect compiler option value", + commandLineArgs: []string{"--init", "--lib", "nonExistLib,es5,es2015.promise"}, + }, + { + subScenario: "Initialized TSConfig with advanced options", + commandLineArgs: []string{"--init", "--declaration", "--declarationDir", "lib", "--skipLibCheck", "--noErrorTruncation"}, + }, + { + subScenario: "Initialized TSConfig with --help", + commandLineArgs: []string{"--init", "--help"}, }, { - subScenario: "init with --lib esnext", - commandLineArgs: []string{"--init", "--lib", "esnext"}, + subScenario: "Initialized TSConfig with --watch", + commandLineArgs: []string{"--init", "--watch"}, }, { - subScenario: "init with tsconfig.json", + subScenario: "Initialized TSConfig with tsconfig.json", commandLineArgs: []string{"--init"}, files: FileMap{ "/home/src/workspaces/project/first.ts": `export const a = 1`, diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with---help.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with---help.js new file mode 100644 index 0000000000..45e76c1864 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with---help.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --help +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-advanced-options.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-advanced-options.js new file mode 100644 index 0000000000..72477c4be4 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-advanced-options.js @@ -0,0 +1,61 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --declaration --declarationDir lib --skipLibCheck --noErrorTruncation +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + + "declarationDir": "/home/src/workspaces/project/lib", + "noErrorTruncation": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-boolean-value-compiler-options.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-boolean-value-compiler-options.js new file mode 100644 index 0000000000..6991ef17a4 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-boolean-value-compiler-options.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --noUnusedLocals +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-enum-value-compiler-options.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-enum-value-compiler-options.js new file mode 100644 index 0000000000..5f38857fa0 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-enum-value-compiler-options.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --target es5 --jsx react +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "es5", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-files-options.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-files-options.js new file mode 100644 index 0000000000..b48c72562e --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-files-options.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init file0.st file1.ts file2.ts +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option-value.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option-value.js new file mode 100644 index 0000000000..037fb5cc4b --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option-value.js @@ -0,0 +1,9 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --lib nonExistLib,es5,es2015.promise +ExitStatus:: DiagnosticsPresent_OutputsSkipped +Output:: +error TS6046: Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'es2022', 'es2023', 'es2024', 'esnext', 'dom', 'dom.iterable', 'dom.asynciterable', 'webworker', 'webworker.importscripts', 'webworker.iterable', 'webworker.asynciterable', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2016.intl', 'es2017.arraybuffer', 'es2017.date', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'es2018.asyncgenerator', 'es2018.asynciterable', 'es2018.intl', 'es2018.promise', 'es2018.regexp', 'es2019.array', 'es2019.object', 'es2019.string', 'es2019.symbol', 'es2019.intl', 'es2020.bigint', 'es2020.date', 'es2020.promise', 'es2020.sharedmemory', 'es2020.string', 'es2020.symbol.wellknown', 'es2020.intl', 'es2020.number', 'es2021.promise', 'es2021.string', 'es2021.weakref', 'es2021.intl', 'es2022.array', 'es2022.error', 'es2022.intl', 'es2022.object', 'es2022.string', 'es2022.regexp', 'es2023.array', 'es2023.collection', 'es2023.intl', 'es2024.arraybuffer', 'es2024.collection', 'es2024.object', 'es2024.promise', 'es2024.regexp', 'es2024.sharedmemory', 'es2024.string', 'esnext.array', 'esnext.collection', 'esnext.symbol', 'esnext.asynciterable', 'esnext.intl', 'esnext.disposable', 'esnext.bigint', 'esnext.string', 'esnext.promise', 'esnext.weakref', 'esnext.decorators', 'esnext.object', 'esnext.regexp', 'esnext.iterator', 'esnext.float16', 'esnext.error', 'esnext.sharedmemory', 'decorators', 'decorators.legacy'. + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option.js new file mode 100644 index 0000000000..c31779e874 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-incorrect-compiler-option.js @@ -0,0 +1,9 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --someNonExistOption +ExitStatus:: DiagnosticsPresent_OutputsSkipped +Output:: +error TS5023: Unknown compiler option '--someNonExistOption'. + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options-with-enum-value.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options-with-enum-value.js new file mode 100644 index 0000000000..f309647030 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options-with-enum-value.js @@ -0,0 +1,59 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --lib es5,es2015.core +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + "lib": ["es5", "es2015.core"], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options.js new file mode 100644 index 0000000000..7fb35046fa --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-list-compiler-options.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --types jquery,mocha +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": ["jquery", "mocha"], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + diff --git a/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-tsconfig.json.js b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-tsconfig.json.js new file mode 100644 index 0000000000..543a4d4988 --- /dev/null +++ b/testdata/baselines/reference/tsc/commandLine/Initialized-TSConfig-with-tsconfig.json.js @@ -0,0 +1,18 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/project/first.ts] *new* +export const a = 1 +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + "compilerOptions": { + "strict": true, + "noEmit": true + } +} + +tsgo --init +ExitStatus:: Success +Output:: +error TS5054: A 'tsconfig.json' file is already defined at: '/home/src/workspaces/project/tsconfig.json'. + diff --git a/testdata/baselines/reference/tscWatch/commandLine/Initialized-TSConfig-with---watch.js b/testdata/baselines/reference/tscWatch/commandLine/Initialized-TSConfig-with---watch.js new file mode 100644 index 0000000000..24a3371f42 --- /dev/null +++ b/testdata/baselines/reference/tscWatch/commandLine/Initialized-TSConfig-with---watch.js @@ -0,0 +1,58 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: + +tsgo --init --watch +ExitStatus:: Success +Output:: + +Created a new tsconfig.json + +You can learn more at https://aka.ms/tsconfig +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + // File Layout + // "rootDir": "./src", + // "outDir": "./dist", + + // Environment Settings + // See also https://aka.ms/tsconfig/module + "module": "nodenext", + "target": "esnext", + "types": [], + // For nodejs: + // "lib": ["esnext"], + // "types": ["node"], + // and npm install -D @types/node + + // Other Outputs + "sourceMap": true, + "declaration": true, + "declarationMap": true, + + // Stricter Typechecking Options + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + + // Style Options + // "noImplicitReturns": true, + // "noImplicitOverride": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + // "noFallthroughCasesInSwitch": true, + // "noPropertyAccessFromIndexSignature": true, + + // Recommended Options + "strict": true, + "jsx": "react-jsx", + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + "skipLibCheck": true, + } +} + + From 3122bb1d713f0ffa1aecd34722b1059e7b06dc2f Mon Sep 17 00:00:00 2001 From: haoqixu Date: Mon, 10 Nov 2025 11:37:49 +0800 Subject: [PATCH 4/4] clean up --- .../tsc/commandLine/init-with---lib-esnext.js | 59 ------------------- .../commandLine/init-with-tsconfig.json.js | 18 ------ .../reference/tsc/commandLine/init.js | 58 ------------------ 3 files changed, 135 deletions(-) delete mode 100644 testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js delete mode 100644 testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js delete mode 100644 testdata/baselines/reference/tsc/commandLine/init.js diff --git a/testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js b/testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js deleted file mode 100644 index ab844b0100..0000000000 --- a/testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js +++ /dev/null @@ -1,59 +0,0 @@ -currentDirectory::/home/src/workspaces/project -useCaseSensitiveFileNames::true -Input:: - -tsgo --init --lib esnext -ExitStatus:: Success -Output:: - -Created a new tsconfig.json - -You can learn more at https://aka.ms/tsconfig -//// [/home/src/workspaces/project/tsconfig.json] *new* -{ - // Visit https://aka.ms/tsconfig to read more about this file - "compilerOptions": { - // File Layout - // "rootDir": "./src", - // "outDir": "./dist", - - // Environment Settings - // See also https://aka.ms/tsconfig/module - "module": "nodenext", - "target": "esnext", - "types": [], - "lib": ["esnext"], - // For nodejs: - // "lib": ["esnext"], - // "types": ["node"], - // and npm install -D @types/node - - // Other Outputs - "sourceMap": true, - "declaration": true, - "declarationMap": true, - - // Stricter Typechecking Options - "noUncheckedIndexedAccess": true, - "exactOptionalPropertyTypes": true, - - // Style Options - // "noImplicitReturns": true, - // "noImplicitOverride": true, - // "noUnusedLocals": true, - // "noUnusedParameters": true, - // "noFallthroughCasesInSwitch": true, - // "noPropertyAccessFromIndexSignature": true, - - // Recommended Options - "strict": true, - "jsx": "react-jsx", - "verbatimModuleSyntax": true, - "isolatedModules": true, - "noUncheckedSideEffectImports": true, - "moduleDetection": "force", - "skipLibCheck": true, - } -} - - diff --git a/testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js b/testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js deleted file mode 100644 index 543a4d4988..0000000000 --- a/testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js +++ /dev/null @@ -1,18 +0,0 @@ -currentDirectory::/home/src/workspaces/project -useCaseSensitiveFileNames::true -Input:: -//// [/home/src/workspaces/project/first.ts] *new* -export const a = 1 -//// [/home/src/workspaces/project/tsconfig.json] *new* -{ - "compilerOptions": { - "strict": true, - "noEmit": true - } -} - -tsgo --init -ExitStatus:: Success -Output:: -error TS5054: A 'tsconfig.json' file is already defined at: '/home/src/workspaces/project/tsconfig.json'. - diff --git a/testdata/baselines/reference/tsc/commandLine/init.js b/testdata/baselines/reference/tsc/commandLine/init.js deleted file mode 100644 index 76ed0bc973..0000000000 --- a/testdata/baselines/reference/tsc/commandLine/init.js +++ /dev/null @@ -1,58 +0,0 @@ -currentDirectory::/home/src/workspaces/project -useCaseSensitiveFileNames::true -Input:: - -tsgo --init -ExitStatus:: Success -Output:: - -Created a new tsconfig.json - -You can learn more at https://aka.ms/tsconfig -//// [/home/src/workspaces/project/tsconfig.json] *new* -{ - // Visit https://aka.ms/tsconfig to read more about this file - "compilerOptions": { - // File Layout - // "rootDir": "./src", - // "outDir": "./dist", - - // Environment Settings - // See also https://aka.ms/tsconfig/module - "module": "nodenext", - "target": "esnext", - "types": [], - // For nodejs: - // "lib": ["esnext"], - // "types": ["node"], - // and npm install -D @types/node - - // Other Outputs - "sourceMap": true, - "declaration": true, - "declarationMap": true, - - // Stricter Typechecking Options - "noUncheckedIndexedAccess": true, - "exactOptionalPropertyTypes": true, - - // Style Options - // "noImplicitReturns": true, - // "noImplicitOverride": true, - // "noUnusedLocals": true, - // "noUnusedParameters": true, - // "noFallthroughCasesInSwitch": true, - // "noPropertyAccessFromIndexSignature": true, - - // Recommended Options - "strict": true, - "jsx": "react-jsx", - "verbatimModuleSyntax": true, - "isolatedModules": true, - "noUncheckedSideEffectImports": true, - "moduleDetection": "force", - "skipLibCheck": true, - } -} - -