Skip to content

Commit 767b853

Browse files
committed
Unify tsc runner even more
1 parent 7e374a2 commit 767b853

File tree

63 files changed

+437
-1166
lines changed

Some content is hidden

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

63 files changed

+437
-1166
lines changed

cmd/tsgo/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ func runMain() int {
2020
return runAPI(args[1:])
2121
}
2222
}
23-
status, _, _, _ := execute.CommandLine(newSystem(), args, false)
23+
status, _, _ := execute.CommandLine(newSystem(), args, false)
2424
return int(status)
2525
}

internal/execute/testsys_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (s *testSys) baselineProgram(baseline *strings.Builder, program *incrementa
176176
return
177177
}
178178

179-
baseline.WriteString("\nSemanticDiagnostics::\n")
179+
baseline.WriteString("SemanticDiagnostics::\n")
180180
semanticDiagnostics, diagnosticsFromOldProgram, updatedSignatureKinds := program.GetTestingData(program.GetProgram())
181181
for _, file := range program.GetProgram().GetSourceFiles() {
182182
if diagnostics, ok := semanticDiagnostics[file.Path()]; ok {
@@ -189,7 +189,7 @@ func (s *testSys) baselineProgram(baseline *strings.Builder, program *incrementa
189189
}
190190

191191
// Write signature updates
192-
baseline.WriteString("\nSignatures::\n")
192+
baseline.WriteString("Signatures::\n")
193193
for _, file := range program.GetProgram().GetSourceFiles() {
194194
if kind, loaded := updatedSignatureKinds.Load(file.Path()); loaded {
195195
switch kind {

internal/execute/tsc.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,20 @@ func applyBulkEdits(text string, edits []core.TextChange) string {
4141
return b.String()
4242
}
4343

44-
func CommandLine(sys System, commandLineArgs []string, testing bool) (ExitStatus, *tsoptions.ParsedCommandLine, *incremental.Program, *Watcher) {
44+
func CommandLine(sys System, commandLineArgs []string, testing bool) (ExitStatus, *incremental.Program, *Watcher) {
4545
if len(commandLineArgs) > 0 {
4646
// !!! build mode
4747
switch strings.ToLower(commandLineArgs[0]) {
4848
case "-b", "--b", "-build", "--build":
4949
fmt.Fprint(sys.Writer(), "Build mode is currently unsupported."+sys.NewLine())
5050
sys.EndWrite()
51-
return ExitStatusNotImplemented, nil, nil, nil
51+
return ExitStatusNotImplemented, nil, nil
5252
// case "-f":
5353
// return fmtMain(sys, commandLineArgs[1], commandLineArgs[1])
5454
}
5555
}
5656

57-
parsedCommandLine := tsoptions.ParseCommandLine(commandLineArgs, sys)
58-
status, incrementalProgram, watcher := tscCompilation(sys, parsedCommandLine, testing)
59-
if watcher != nil {
60-
watcher.start()
61-
}
62-
return status, parsedCommandLine, incrementalProgram, watcher
57+
return tscCompilation(sys, tsoptions.ParseCommandLine(commandLineArgs, sys), testing)
6358
}
6459

6560
func fmtMain(sys System, input, output string) ExitStatus {
@@ -186,7 +181,9 @@ func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine, testin
186181
return ExitStatusSuccess, nil, nil
187182
}
188183
if configForCompilation.CompilerOptions().Watch.IsTrue() {
189-
return ExitStatusSuccess, nil, createWatcher(sys, configForCompilation, reportDiagnostic, testing)
184+
watcher := createWatcher(sys, configForCompilation, reportDiagnostic, testing)
185+
watcher.start()
186+
return ExitStatusSuccess, nil, watcher
190187
} else if configForCompilation.CompilerOptions().IsIncremental() {
191188
exitStatus, program := performIncrementalCompilation(
192189
sys,

internal/execute/tsc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestTscCommandline(t *testing.T) {
142142
}
143143

144144
for _, testCase := range testCases {
145-
testCase.verifyCommandLineParsing(t, "commandLine")
145+
testCase.run(t, "commandLine")
146146
}
147147
}
148148

Lines changed: 41 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package execute_test
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"path/filepath"
76
"slices"
@@ -35,48 +34,71 @@ type tscInput struct {
3534
edits []*testTscEdit
3635
}
3736

37+
func (test *tscInput) executeCommand(baselineBuilder *strings.Builder, commandLineArgs []string) (*incremental.Program, *execute.Watcher) {
38+
fmt.Fprint(baselineBuilder, "tsgo ", strings.Join(commandLineArgs, " "), "\n")
39+
exit, incrementalProgram, watcher := execute.CommandLine(test.sys, commandLineArgs, true)
40+
switch exit {
41+
case execute.ExitStatusSuccess:
42+
baselineBuilder.WriteString("ExitStatus:: Success")
43+
case execute.ExitStatusDiagnosticsPresent_OutputsSkipped:
44+
baselineBuilder.WriteString("ExitStatus:: DiagnosticsPresent_OutputsSkipped")
45+
case execute.ExitStatusDiagnosticsPresent_OutputsGenerated:
46+
baselineBuilder.WriteString("ExitStatus:: DiagnosticsPresent_OutputsGenerated")
47+
case execute.ExitStatusInvalidProject_OutputsSkipped:
48+
baselineBuilder.WriteString("ExitStatus:: InvalidProject_OutputsSkipped")
49+
case execute.ExitStatusProjectReferenceCycle_OutputsSkipped:
50+
baselineBuilder.WriteString("ExitStatus:: ProjectReferenceCycle_OutputsSkipped")
51+
case execute.ExitStatusNotImplemented:
52+
baselineBuilder.WriteString("ExitStatus:: NotImplemented")
53+
default:
54+
panic(fmt.Sprintf("UnknownExitStatus %d", exit))
55+
}
56+
return incrementalProgram, watcher
57+
}
58+
3859
func (test *tscInput) run(t *testing.T, scenario string) {
3960
t.Helper()
61+
// !!! sheetal TODO :: add incremental correctness
4062
t.Run(test.subScenario+" tsc baseline", func(t *testing.T) {
4163
t.Parallel()
4264
// initial test tsc compile
43-
baselineBuilder := test.startBaseline()
44-
45-
exit, parsedCommandLine, incrementalProgram, watcher := execute.CommandLine(test.sys, test.commandLineArgs, true)
46-
baselineBuilder.WriteString("ExitStatus:: " + fmt.Sprint(exit))
47-
48-
compilerOptionsString, _ := json.MarshalIndent(parsedCommandLine.CompilerOptions(), "", " ")
49-
baselineBuilder.WriteString("\n\nCompilerOptions::")
50-
baselineBuilder.Write(compilerOptionsString)
51-
65+
baselineBuilder := &strings.Builder{}
66+
fmt.Fprint(
67+
baselineBuilder,
68+
"currentDirectory::",
69+
test.sys.GetCurrentDirectory(),
70+
"\nuseCaseSensitiveFileNames::",
71+
test.sys.FS().UseCaseSensitiveFileNames(),
72+
"\nInput::\n",
73+
)
74+
test.sys.baselineFSwithDiff(baselineBuilder)
75+
incrementalProgram, watcher := test.executeCommand(baselineBuilder, test.commandLineArgs)
5276
test.sys.serializeState(baselineBuilder)
5377
test.sys.baselineProgram(baselineBuilder, incrementalProgram, watcher)
54-
for _, do := range test.edits {
55-
baselineBuilder.WriteString("\n\nEdit:: " + do.caption + "\n")
78+
79+
for index, do := range test.edits {
80+
baselineBuilder.WriteString(fmt.Sprintf("\n\nEdit [%d]:: %s\n", index, do.caption))
5681
if do.edit != nil {
5782
do.edit(test.sys)
5883
}
5984
test.sys.baselineFSwithDiff(baselineBuilder)
6085

6186
var incrementalProgram *incremental.Program
6287
if watcher == nil {
63-
exit, parsedCommandLine, incrementalProgram, watcher = execute.CommandLine(test.sys, core.IfElse(do.commandLineArgs == nil, test.commandLineArgs, do.commandLineArgs), true)
64-
baselineBuilder.WriteString("ExitStatus:: " + fmt.Sprint(exit))
88+
commandLineArgs := core.IfElse(do.commandLineArgs == nil, test.commandLineArgs, do.commandLineArgs)
89+
incrementalProgram, watcher = test.executeCommand(baselineBuilder, commandLineArgs)
6590
} else {
6691
watcher.DoCycle()
6792
}
6893
test.sys.serializeState(baselineBuilder)
6994
test.sys.baselineProgram(baselineBuilder, incrementalProgram, watcher)
7095
}
7196

72-
options, name := test.getBaselineName(scenario, "")
73-
baseline.Run(t, name, baselineBuilder.String(), options)
97+
baseline.Run(t, strings.ReplaceAll(test.subScenario, " ", "-")+".js", baselineBuilder.String(), baseline.Options{Subfolder: filepath.Join(test.getBaselineSubFolder(), scenario)})
7498
})
75-
76-
// !!! sheetal TODO :: add incremental correctness
7799
}
78100

79-
func (test *tscInput) getTestNamePrefix() string {
101+
func (test *tscInput) getBaselineSubFolder() string {
80102
commandName := "tsc"
81103
if slices.ContainsFunc(test.commandLineArgs, func(arg string) bool {
82104
return arg == "--build" || arg == "-b"
@@ -91,46 +113,3 @@ func (test *tscInput) getTestNamePrefix() string {
91113
}
92114
return commandName + w
93115
}
94-
95-
func (test *tscInput) getBaselineName(scenario string, suffix string) (baseline.Options, string) {
96-
return baseline.Options{Subfolder: filepath.Join(test.getTestNamePrefix(), scenario)},
97-
strings.ReplaceAll(test.subScenario, " ", "-") + suffix + ".js"
98-
}
99-
100-
func (test *tscInput) startBaseline() *strings.Builder {
101-
s := &strings.Builder{}
102-
fmt.Fprint(
103-
s,
104-
"\ncurrentDirectory::",
105-
test.sys.GetCurrentDirectory(),
106-
"\nuseCaseSensitiveFileNames::",
107-
test.sys.FS().UseCaseSensitiveFileNames(),
108-
"\nInput::",
109-
)
110-
fmt.Fprint(s, strings.Join(test.commandLineArgs, " "), "\n")
111-
test.sys.baselineFSwithDiff(s)
112-
return s
113-
}
114-
115-
func (test *tscInput) verifyCommandLineParsing(t *testing.T, scenario string) {
116-
t.Helper()
117-
t.Run(test.subScenario, func(t *testing.T) {
118-
t.Parallel()
119-
t.Run("baseline for the tsc compiles", func(t *testing.T) {
120-
t.Parallel()
121-
// initial test tsc compile
122-
baselineBuilder := test.startBaseline()
123-
124-
exit, parsedCommandLine, _, _ := execute.CommandLine(test.sys, test.commandLineArgs, true)
125-
baselineBuilder.WriteString("ExitStatus:: " + fmt.Sprint(exit))
126-
//nolint:musttag
127-
parsedCommandLineString, _ := json.MarshalIndent(parsedCommandLine, "", " ")
128-
baselineBuilder.WriteString("\n\nParsedCommandLine::")
129-
baselineBuilder.Write(parsedCommandLineString)
130-
131-
test.sys.serializeState(baselineBuilder)
132-
options, name := test.getBaselineName(scenario, "")
133-
baseline.Run(t, name, baselineBuilder.String(), options)
134-
})
135-
})
136-
}
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
21
currentDirectory::/home/src/workspaces/project
32
useCaseSensitiveFileNames::true
4-
Input::--verbose --build
5-
6-
ExitStatus:: 1
3+
Input::
74

8-
ParsedCommandLine::{
9-
"parsedConfig": {
10-
"compilerOptions": {},
11-
"watchOptions": {
12-
"watchInterval": null,
13-
"watchFile": 0,
14-
"watchDirectory": 0,
15-
"fallbackPolling": 0,
16-
"synchronousWatchDirectory": null,
17-
"excludeDirectories": null,
18-
"excludeFiles": null
19-
},
20-
"typeAcquisition": null,
21-
"fileNames": [],
22-
"projectReferences": null
23-
},
24-
"configFile": null,
25-
"errors": [
26-
{},
27-
{}
28-
],
29-
"raw": {},
30-
"compileOnSave": null
31-
}
5+
tsgo --verbose --build
6+
ExitStatus:: DiagnosticsPresent_OutputsSkipped
327
Output::
338
error TS5093: Compiler option '--verbose' may only be used with '--build'.
349
error TS6369: Option '--build' must be the first command line argument.

testdata/baselines/reference/tsc/commandLine/Parse---lib-option-with-file-name.js

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,11 @@
1-
21
currentDirectory::/home/src/workspaces/project
32
useCaseSensitiveFileNames::true
4-
Input::--lib es6 first.ts
3+
Input::
54
//// [/home/src/workspaces/project/first.ts] *new*
65
export const Key = Symbol()
76

8-
ExitStatus:: 0
9-
10-
ParsedCommandLine::{
11-
"parsedConfig": {
12-
"compilerOptions": {
13-
"lib": [
14-
"lib.es2015.d.ts"
15-
]
16-
},
17-
"watchOptions": {
18-
"watchInterval": null,
19-
"watchFile": 0,
20-
"watchDirectory": 0,
21-
"fallbackPolling": 0,
22-
"synchronousWatchDirectory": null,
23-
"excludeDirectories": null,
24-
"excludeFiles": null
25-
},
26-
"typeAcquisition": null,
27-
"fileNames": [
28-
"first.ts"
29-
],
30-
"projectReferences": null
31-
},
32-
"configFile": null,
33-
"errors": [],
34-
"raw": {
35-
"lib": [
36-
"lib.es2015.d.ts"
37-
]
38-
},
39-
"compileOnSave": null
40-
}
7+
tsgo --lib es6 first.ts
8+
ExitStatus:: Success
419
Output::
4210
//// [/home/src/tslibs/TS/Lib/lib.es2015.d.ts] *Lib*
4311
/// <reference no-default-lib="true"/>

testdata/baselines/reference/tsc/commandLine/Parse--p-with-path-to-tsconfig-file.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
21
currentDirectory::/home/src/workspaces/project
32
useCaseSensitiveFileNames::true
4-
Input::-p /home/src/workspaces/project/tsconfig.json
3+
Input::
54
//// [/home/src/workspaces/project/first.ts] *new*
65
export const a = 1
76
//// [/home/src/workspaces/project/tsconfig.json] *new*
@@ -12,33 +11,8 @@ export const a = 1
1211
}
1312
}
1413

15-
ExitStatus:: 0
16-
17-
ParsedCommandLine::{
18-
"parsedConfig": {
19-
"compilerOptions": {
20-
"project": "/home/src/workspaces/project/tsconfig.json"
21-
},
22-
"watchOptions": {
23-
"watchInterval": null,
24-
"watchFile": 0,
25-
"watchDirectory": 0,
26-
"fallbackPolling": 0,
27-
"synchronousWatchDirectory": null,
28-
"excludeDirectories": null,
29-
"excludeFiles": null
30-
},
31-
"typeAcquisition": null,
32-
"fileNames": [],
33-
"projectReferences": null
34-
},
35-
"configFile": null,
36-
"errors": [],
37-
"raw": {
38-
"project": "/home/src/workspaces/project/tsconfig.json"
39-
},
40-
"compileOnSave": null
41-
}
14+
tsgo -p /home/src/workspaces/project/tsconfig.json
15+
ExitStatus:: Success
4216
Output::
4317
//// [/home/src/tslibs/TS/Lib/lib.d.ts] *Lib*
4418
/// <reference no-default-lib="true"/>

testdata/baselines/reference/tsc/commandLine/Parse--p-with-path-to-tsconfig-folder.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
21
currentDirectory::/home/src/workspaces/project
32
useCaseSensitiveFileNames::true
4-
Input::-p /home/src/workspaces/project
3+
Input::
54
//// [/home/src/workspaces/project/first.ts] *new*
65
export const a = 1
76
//// [/home/src/workspaces/project/tsconfig.json] *new*
@@ -12,33 +11,8 @@ export const a = 1
1211
}
1312
}
1413

15-
ExitStatus:: 0
16-
17-
ParsedCommandLine::{
18-
"parsedConfig": {
19-
"compilerOptions": {
20-
"project": "/home/src/workspaces/project"
21-
},
22-
"watchOptions": {
23-
"watchInterval": null,
24-
"watchFile": 0,
25-
"watchDirectory": 0,
26-
"fallbackPolling": 0,
27-
"synchronousWatchDirectory": null,
28-
"excludeDirectories": null,
29-
"excludeFiles": null
30-
},
31-
"typeAcquisition": null,
32-
"fileNames": [],
33-
"projectReferences": null
34-
},
35-
"configFile": null,
36-
"errors": [],
37-
"raw": {
38-
"project": "/home/src/workspaces/project"
39-
},
40-
"compileOnSave": null
41-
}
14+
tsgo -p /home/src/workspaces/project
15+
ExitStatus:: Success
4216
Output::
4317
//// [/home/src/tslibs/TS/Lib/lib.d.ts] *Lib*
4418
/// <reference no-default-lib="true"/>

0 commit comments

Comments
 (0)