-
Notifications
You must be signed in to change notification settings - Fork 734
Port tsc --init #2033
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
Open
haoqixu
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
haoqixu:tsc-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+397
−1
Open
Port tsc --init #2033
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 := range val.NumField() { | ||
| 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 := range rval.Len() { | ||
| 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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
testdata/baselines/reference/tsc/commandLine/init-with---lib-esnext.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, | ||
| } | ||
| } | ||
|
|
||
|
|
18 changes: 18 additions & 0 deletions
18
testdata/baselines/reference/tsc/commandLine/init-with-tsconfig.json.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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:: | ||
| [91merror[0m[90m TS5054: [0mA 'tsconfig.json' file is already defined at: '/home/src/workspaces/project/tsconfig.json'. | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do all of the tests listed in
_submodules/TypeScript/src/testRunner/unittests/config/initializeTSConfig.ts?