Skip to content

Commit 1325bba

Browse files
committed
Port part of tsc help from strada
1 parent a61f96a commit 1325bba

File tree

75 files changed

+471
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+471
-96
lines changed

internal/core/compileroptions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ type CompilerOptions struct {
134134
Watch Tristate `json:"watch"`
135135
ShowConfig Tristate `json:"showConfig"`
136136
TscBuild Tristate `json:"tscBuild"`
137+
Help Tristate `json:"help"`
138+
All Tristate `json:"all"`
137139
}
138140

139141
func (options *CompilerOptions) GetEmitScriptTarget() ScriptTarget {

internal/execute/outputs.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package execute
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/microsoft/typescript-go/internal/ast"
89
"github.com/microsoft/typescript-go/internal/compiler"
10+
"github.com/microsoft/typescript-go/internal/compiler/diagnostics"
911
"github.com/microsoft/typescript-go/internal/core"
1012
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
13+
"github.com/microsoft/typescript-go/internal/tsoptions"
1114
"github.com/microsoft/typescript-go/internal/tspath"
1215
)
1316

@@ -79,3 +82,185 @@ type statistic struct {
7982
func newStatistic(name string, count int) statistic {
8083
return statistic{name, count}
8184
}
85+
86+
func printHelp(sys System, commandLine *tsoptions.ParsedCommandLine) {
87+
if commandLine.CompilerOptions().All.IsFalseOrUnknown() {
88+
printEasyHelp(sys, getOptionsForHelp(commandLine))
89+
} else {
90+
// !!! printAllHelp(sys, getOptionsForHelp(commandLine))
91+
}
92+
}
93+
94+
func getOptionsForHelp(commandLine *tsoptions.ParsedCommandLine) []*tsoptions.CommandLineOption {
95+
// Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch")
96+
opts := slices.Clone(tsoptions.OptionsDeclarations)
97+
opts = append(opts, &tsoptions.TscBuildOption)
98+
99+
if commandLine.CompilerOptions().All.IsTrue() {
100+
slices.SortFunc(opts, func(a, b *tsoptions.CommandLineOption) int {
101+
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
102+
})
103+
return opts
104+
} else {
105+
return core.Filter(opts, func(opt *tsoptions.CommandLineOption) bool {
106+
return opt.ShowInSimplifiedHelpView()
107+
})
108+
}
109+
}
110+
111+
func getHeader(sys System, message string) []string {
112+
// !!! const colors = createColors(sys);
113+
var header []string
114+
// !!! terminalWidth := sys.GetWidthOfTerminal?.() ?? 0
115+
const tsIconLength = 5
116+
117+
// const tsIconFirstLine = colors.blueBackground("".padStart(tsIconLength));
118+
// const tsIconSecondLine = colors.blueBackground(colors.brightWhite("TS ".padStart(tsIconLength)));
119+
// // If we have enough space, print TS icon.
120+
// if (terminalWidth >= message.length + tsIconLength) {
121+
// // right align of the icon is 120 at most.
122+
// const rightAlign = terminalWidth > 120 ? 120 : terminalWidth;
123+
// const leftAlign = rightAlign - tsIconLength;
124+
// header.push(message.padEnd(leftAlign) + tsIconFirstLine + sys.newLine);
125+
// header.push("".padStart(leftAlign) + tsIconSecondLine + sys.newLine);
126+
// }
127+
// else {
128+
header = append(header, message+sys.NewLine(), sys.NewLine())
129+
// }
130+
return header
131+
}
132+
133+
func printEasyHelp(sys System, simpleOptions []*tsoptions.CommandLineOption) {
134+
// !!! const colors = createColors(sys);
135+
var output []string
136+
example := func(examples []string, desc *diagnostics.Message) {
137+
for _, example := range examples {
138+
// !!! colors
139+
// output.push(" " + colors.blue(example) + sys.newLine);
140+
output = append(output, " ", example, sys.NewLine())
141+
}
142+
output = append(output, " ", ast.NewCompilerDiagnostic(desc).Message(), sys.NewLine(), sys.NewLine())
143+
}
144+
145+
msg := ast.NewCompilerDiagnostic(diagnostics.X_tsc_Colon_The_TypeScript_Compiler).Message() + " - " + ast.NewCompilerDiagnostic(diagnostics.Version_0, core.Version).Message()
146+
output = append(output, getHeader(sys, msg)...)
147+
148+
output = append(output /*colors.bold(*/, ast.NewCompilerDiagnostic(diagnostics.COMMON_COMMANDS).Message() /*)*/, sys.NewLine(), sys.NewLine())
149+
150+
example([]string{"tsc"}, diagnostics.Compiles_the_current_project_tsconfig_json_in_the_working_directory)
151+
example([]string{"tsc app.ts util.ts"}, diagnostics.Ignoring_tsconfig_json_compiles_the_specified_files_with_default_compiler_options)
152+
example([]string{"tsc -b"}, diagnostics.Build_a_composite_project_in_the_working_directory)
153+
example([]string{"tsc --init"}, diagnostics.Creates_a_tsconfig_json_with_the_recommended_settings_in_the_working_directory)
154+
example([]string{"tsc -p ./path/to/tsconfig.json"}, diagnostics.Compiles_the_TypeScript_project_located_at_the_specified_path)
155+
example([]string{"tsc --help --all"}, diagnostics.An_expanded_version_of_this_information_showing_all_possible_compiler_options)
156+
example([]string{"tsc --noEmit", "tsc --target esnext"}, diagnostics.Compiles_the_current_project_with_additional_settings)
157+
158+
var cliCommands []*tsoptions.CommandLineOption
159+
var configOpts []*tsoptions.CommandLineOption
160+
for _, opt := range simpleOptions {
161+
if opt.IsCommandLineOnly || opt.Category == diagnostics.Command_line_Options {
162+
cliCommands = append(cliCommands, opt)
163+
} else {
164+
configOpts = append(configOpts, opt)
165+
}
166+
}
167+
168+
output = append(output, generateSectionOptionsOutput(sys, diagnostics.COMMAND_LINE_FLAGS.Format(), cliCommands /*subCategory*/, false /*beforeOptionsDescription*/, nil /*afterOptionsDescription*/, nil)...)
169+
170+
after := diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0.Format("https://aka.ms/tsc")
171+
output = append(output, generateSectionOptionsOutput(sys, diagnostics.COMMON_COMPILER_OPTIONS.Format(), configOpts /*subCategory*/, false /*beforeOptionsDescription*/, nil,
172+
// !!! locale formatMessage(Diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0, "https://aka.ms/tsc")),
173+
&after)...)
174+
175+
for _, chunk := range output {
176+
fmt.Fprint(sys.Writer(), chunk)
177+
}
178+
sys.EndWrite()
179+
}
180+
181+
func generateSectionOptionsOutput(
182+
sys System,
183+
sectionName string,
184+
options []*tsoptions.CommandLineOption,
185+
subCategory bool,
186+
beforeOptionsDescription,
187+
afterOptionsDescription *string,
188+
) (output []string) {
189+
// !!! color
190+
output = append(output /*createColors(sys).bold(*/, sectionName /*)*/, sys.NewLine(), sys.NewLine())
191+
192+
if beforeOptionsDescription != nil {
193+
output = append(output, *beforeOptionsDescription, sys.NewLine(), sys.NewLine())
194+
}
195+
if !subCategory {
196+
output = append(output, generateGroupOptionOutput(sys, options)...)
197+
if afterOptionsDescription != nil {
198+
output = append(output, *afterOptionsDescription, sys.NewLine(), sys.NewLine())
199+
}
200+
return output
201+
}
202+
categoryMap := make(map[string][]*tsoptions.CommandLineOption)
203+
for _, option := range options {
204+
if option.Category == nil {
205+
continue
206+
}
207+
curCategory := ast.NewCompilerDiagnostic(option.Category).Message()
208+
categoryMap[curCategory] = append(categoryMap[curCategory], option)
209+
}
210+
for key, value := range categoryMap {
211+
output = append(output, "### ", key, sys.NewLine(), sys.NewLine())
212+
output = append(output, generateGroupOptionOutput(sys, value)...)
213+
}
214+
if afterOptionsDescription != nil {
215+
output = append(output, *afterOptionsDescription, sys.NewLine(), sys.NewLine())
216+
}
217+
218+
return output
219+
}
220+
221+
func generateGroupOptionOutput(sys System, optionsList []*tsoptions.CommandLineOption) []string {
222+
var maxLength int
223+
for _, option := range optionsList {
224+
curLenght := len(getDisplayNameTextOfOption(option))
225+
maxLength = max(curLenght, maxLength)
226+
}
227+
228+
// left part should be right align, right part should be left align
229+
230+
// assume 2 space between left margin and left part.
231+
rightAlignOfLeftPart := maxLength + 2
232+
// assume 2 space between left and right part
233+
leftAlignOfRightPart := rightAlignOfLeftPart + 2
234+
235+
var lines []string
236+
for _, option := range optionsList {
237+
tmp := generateOptionOutput(sys, option, rightAlignOfLeftPart, leftAlignOfRightPart)
238+
lines = append(lines, tmp...)
239+
}
240+
241+
// make sure always a blank line in the end.
242+
// !!! if lines[len(lines)-2] != sys.NewLine() {
243+
// !!! lines = append(lines, sys.NewLine())
244+
// !!! }
245+
246+
return lines
247+
}
248+
func generateOptionOutput(
249+
sys System,
250+
option *tsoptions.CommandLineOption,
251+
rightAlignOfLeftPart, leftAlignOfRightPart int,
252+
) []string {
253+
var text []string
254+
// !!! const colors = createColors(sys);
255+
256+
// name and description
257+
// !!! name := getDisplayNameTextOfOption(option)
258+
259+
// !!!
260+
261+
return text
262+
}
263+
264+
func getDisplayNameTextOfOption(option *tsoptions.CommandLineOption) string {
265+
return "--" + option.Name + core.IfElse(option.ShortName != "", ", -"+option.ShortName, "")
266+
}

internal/execute/tsc.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
3636

3737
if commandLine.CompilerOptions().Init.IsTrue() ||
3838
commandLine.CompilerOptions().Version.IsTrue() ||
39-
// commandLine.CompilerOptions().Help != nil ||
40-
// commandLine.CompilerOptions().All != nil ||
4139
commandLine.CompilerOptions().Watch.IsTrue() && commandLine.CompilerOptions().ListFilesOnly.IsTrue() {
4240
return ExitStatusNotImplemented, nil
4341
}
4442

43+
if commandLine.CompilerOptions().Help.IsTrue() || commandLine.CompilerOptions().All.IsTrue() {
44+
printHelp(sys, commandLine)
45+
return ExitStatusSuccess, nil
46+
}
47+
4548
if commandLine.CompilerOptions().Project != "" {
4649
if len(commandLine.FileNames()) != 0 {
4750
reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line))

internal/tsoptions/commandlineoption.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
)
2020

2121
type CommandLineOption struct {
22-
Name, shortName string
22+
Name, ShortName string
2323
Kind CommandLineOptionKind
2424

2525
// used in parsing
@@ -65,6 +65,10 @@ type CommandLineOption struct {
6565
ElementOptions map[string]*CommandLineOption
6666
}
6767

68+
func (o *CommandLineOption) ShowInSimplifiedHelpView() bool {
69+
return o.showInSimplifiedHelpView
70+
}
71+
6872
func (o *CommandLineOption) DeprecatedKeys() *core.Set[string] {
6973
if o.Kind != CommandLineOptionTypeEnum {
7074
return nil

internal/tsoptions/declsbuild.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@ import (
88

99
var BuildOpts = slices.Concat(commonOptionsWithBuild, optionsForBuild)
1010

11-
var tscBuildOption = CommandLineOption{
11+
var TscBuildOption = CommandLineOption{
1212
Name: "build",
1313
Kind: "boolean",
14-
shortName: "b",
14+
ShortName: "b",
1515
showInSimplifiedHelpView: true,
1616
Category: diagnostics.Command_line_Options,
1717
Description: diagnostics.Build_one_or_more_projects_and_their_dependencies_if_out_of_date,
1818
DefaultValueDescription: false,
1919
}
2020

2121
var optionsForBuild = []*CommandLineOption{
22-
&tscBuildOption,
22+
&TscBuildOption,
2323
{
2424
Name: "verbose",
25-
shortName: "v",
25+
ShortName: "v",
2626
Category: diagnostics.Command_line_Options,
2727
Description: diagnostics.Enable_verbose_logging,
2828
Kind: "boolean",
2929
DefaultValueDescription: false,
3030
},
3131
{
3232
Name: "dry",
33-
shortName: "d",
33+
ShortName: "d",
3434
Category: diagnostics.Command_line_Options,
3535
Description: diagnostics.Show_what_would_be_built_or_deleted_if_specified_with_clean,
3636
Kind: "boolean",
3737
DefaultValueDescription: false,
3838
},
3939
{
4040
Name: "force",
41-
shortName: "f",
41+
ShortName: "f",
4242
Category: diagnostics.Command_line_Options,
4343
Description: diagnostics.Build_all_projects_including_those_that_appear_to_be_up_to_date,
4444
Kind: "boolean",

internal/tsoptions/declscompiler.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var optionsForCompiler = []*CommandLineOption{
1313
//******* commandOptionsWithoutBuild *******
1414
{
1515
Name: "help",
16-
shortName: "h",
16+
ShortName: "h",
1717
Kind: CommandLineOptionTypeBoolean,
1818
showInSimplifiedHelpView: true,
1919
IsCommandLineOnly: true,
@@ -23,15 +23,15 @@ var optionsForCompiler = []*CommandLineOption{
2323
},
2424
{
2525
Name: "help",
26-
shortName: "?",
26+
ShortName: "?",
2727
Kind: CommandLineOptionTypeBoolean,
2828
IsCommandLineOnly: true,
2929
Category: diagnostics.Command_line_Options,
3030
DefaultValueDescription: false,
3131
},
3232
{
3333
Name: "watch",
34-
shortName: "w",
34+
ShortName: "w",
3535
Kind: CommandLineOptionTypeBoolean,
3636
showInSimplifiedHelpView: true,
3737
IsCommandLineOnly: true,
@@ -114,7 +114,7 @@ var optionsForCompiler = []*CommandLineOption{
114114
},
115115
{
116116
Name: "incremental",
117-
shortName: "i",
117+
ShortName: "i",
118118
Kind: CommandLineOptionTypeBoolean,
119119
Category: diagnostics.Projects,
120120
Description: diagnostics.Save_tsbuildinfo_files_to_allow_for_incremental_compilation_of_projects,
@@ -123,7 +123,7 @@ var optionsForCompiler = []*CommandLineOption{
123123
},
124124
{
125125
Name: "declaration",
126-
shortName: "d",
126+
ShortName: "d",
127127
Kind: CommandLineOptionTypeBoolean,
128128
// Not setting affectsEmit because we calculate this flag might not affect full emit
129129
AffectsBuildInfo: true,
@@ -226,7 +226,7 @@ var commonOptionsWithBuild = []*CommandLineOption{
226226
},
227227
{
228228
Name: "version",
229-
shortName: "v",
229+
ShortName: "v",
230230
Kind: CommandLineOptionTypeBoolean,
231231
showInSimplifiedHelpView: true,
232232
Category: diagnostics.Command_line_Options,
@@ -243,7 +243,7 @@ var commonOptionsWithBuild = []*CommandLineOption{
243243
},
244244
{
245245
Name: "project",
246-
shortName: "p",
246+
ShortName: "p",
247247
Kind: CommandLineOptionTypeString,
248248
isFilePath: true,
249249
showInSimplifiedHelpView: true,
@@ -272,7 +272,7 @@ var commonOptionsWithBuild = []*CommandLineOption{
272272
// targetOptionDeclaration,
273273
{
274274
Name: "target",
275-
shortName: "t",
275+
ShortName: "t",
276276
Kind: CommandLineOptionTypeEnum, // targetOptionMap
277277
AffectsSourceFile: true,
278278
AffectsModuleResolution: true,
@@ -287,7 +287,7 @@ var commonOptionsWithBuild = []*CommandLineOption{
287287
// moduleOptionDeclaration,
288288
{
289289
Name: "module",
290-
shortName: "m",
290+
ShortName: "m",
291291
Kind: CommandLineOptionTypeEnum, // moduleOptionMap
292292
AffectsModuleResolution: true,
293293
AffectsEmit: true,

internal/tsoptions/namemap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func GetNameMapFromList(optDecls []*CommandLineOption) *NameMap {
1717
shortOptionNames := map[string]string{}
1818
for _, option := range optDecls {
1919
optionsNames.Set(strings.ToLower(option.Name), option)
20-
if option.shortName != "" {
21-
shortOptionNames[option.shortName] = option.Name
20+
if option.ShortName != "" {
21+
shortOptionNames[option.ShortName] = option.Name
2222
}
2323
}
2424
return &NameMap{

internal/tsoptions/parsinghelpers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ func ParseCompilerOptions(key string, value any, allOptions *core.CompilerOption
371371
allOptions.VerbatimModuleSyntax = parseTristate(value)
372372
case "version":
373373
allOptions.Version = parseTristate(value)
374+
case "help":
375+
allOptions.Help = parseTristate(value)
376+
case "all":
377+
allOptions.All = parseTristate(value)
374378
case "maxNodeModuleJsDepth":
375379
allOptions.MaxNodeModuleJsDepth = parseNumber(value)
376380
case "skipLibCheck":

0 commit comments

Comments
 (0)