Skip to content

Commit b79ef45

Browse files
committed
Add baseline for semantic diagnostics refresh to check for correctness
1 parent 8430b07 commit b79ef45

23 files changed

+325
-127
lines changed

cmd/tsgo/main.go

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

internal/execute/export_test.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

internal/execute/testsys_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
"github.com/microsoft/typescript-go/internal/collections"
1414
"github.com/microsoft/typescript-go/internal/core"
15+
"github.com/microsoft/typescript-go/internal/execute"
16+
"github.com/microsoft/typescript-go/internal/incremental"
1517
"github.com/microsoft/typescript-go/internal/testutil/incrementaltestutil"
1618
"github.com/microsoft/typescript-go/internal/tsoptions"
1719
"github.com/microsoft/typescript-go/internal/vfs"
@@ -95,6 +97,8 @@ type testSys struct {
9597
start time.Time
9698
}
9799

100+
var _ execute.System = (*testSys)(nil)
101+
98102
func (s *testSys) Now() time.Time {
99103
// todo: make a "test time" structure
100104
return time.Now()
@@ -162,6 +166,29 @@ func (s *testSys) EndWrite() {
162166
s.output = append(s.output, output)
163167
}
164168

169+
func (s *testSys) baselineProgram(baseline *strings.Builder, program *incremental.Program, watcher *execute.Watcher) {
170+
if watcher != nil {
171+
program = watcher.GetProgram()
172+
}
173+
if program == nil {
174+
return
175+
}
176+
177+
baseline.WriteString("\nSemanticDiagnostics::\n")
178+
semanticDiagnostics, diagnosticsFromOldProgram := program.GetTestingData(program.GetProgram())
179+
for _, file := range program.GetProgram().GetSourceFiles() {
180+
if diagnostics, ok := semanticDiagnostics[file.Path()]; ok {
181+
if oldDiagnostics, ok := diagnosticsFromOldProgram[file.Path()]; !ok || oldDiagnostics != diagnostics {
182+
baseline.WriteString("*refresh* " + file.FileName() + "\n")
183+
}
184+
} else {
185+
baseline.WriteString("*not cached* " + file.FileName() + "\n")
186+
}
187+
}
188+
189+
// Write signature updates
190+
}
191+
165192
func (s *testSys) serializeState(baseline *strings.Builder) {
166193
s.baselineOutput(baseline)
167194
s.baselineFSwithDiff(baseline)

internal/execute/tsc.go

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

44-
func CommandLine(sys System, commandLineArgs []string) ExitStatus {
45-
status, _, watcher := commandLineWorker(sys, commandLineArgs)
46-
if watcher == nil {
47-
return status
48-
}
49-
return start(watcher)
50-
}
51-
52-
func commandLineWorker(sys System, commandLineArgs []string) (ExitStatus, *tsoptions.ParsedCommandLine, *watcher) {
44+
func CommandLine(sys System, commandLineArgs []string, testing bool) (ExitStatus, *tsoptions.ParsedCommandLine, *incremental.Program, *Watcher) {
5345
if len(commandLineArgs) > 0 {
5446
// !!! build mode
5547
switch strings.ToLower(commandLineArgs[0]) {
5648
case "-b", "--b", "-build", "--build":
5749
fmt.Fprint(sys.Writer(), "Build mode is currently unsupported."+sys.NewLine())
5850
sys.EndWrite()
59-
return ExitStatusNotImplemented, nil, nil
51+
return ExitStatusNotImplemented, nil, nil, nil
6052
// case "-f":
6153
// return fmtMain(sys, commandLineArgs[1], commandLineArgs[1])
6254
}
6355
}
6456

6557
parsedCommandLine := tsoptions.ParseCommandLine(commandLineArgs, sys)
66-
status, watcher := tscCompilation(sys, parsedCommandLine)
67-
return status, parsedCommandLine, watcher
58+
status, incrementalProgram, watcher := tscCompilation(sys, parsedCommandLine, testing)
59+
if watcher == nil {
60+
watcher.start()
61+
}
62+
return status, parsedCommandLine, incrementalProgram, watcher
6863
}
6964

7065
func fmtMain(sys System, input, output string) ExitStatus {
@@ -93,7 +88,7 @@ func fmtMain(sys System, input, output string) ExitStatus {
9388
return ExitStatusSuccess
9489
}
9590

96-
func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine) (ExitStatus, *watcher) {
91+
func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine, testing bool) (ExitStatus, *incremental.Program, *Watcher) {
9792
configFileName := ""
9893
reportDiagnostic := createDiagnosticReporter(sys, commandLine.CompilerOptions())
9994
// if commandLine.Options().Locale != nil
@@ -102,7 +97,7 @@ func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine) (ExitS
10297
for _, e := range commandLine.Errors {
10398
reportDiagnostic(e)
10499
}
105-
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil
100+
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil, nil
106101
}
107102

108103
if pprofDir := commandLine.CompilerOptions().PprofDir; pprofDir != "" {
@@ -112,42 +107,42 @@ func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine) (ExitS
112107
}
113108

114109
if commandLine.CompilerOptions().Init.IsTrue() {
115-
return ExitStatusNotImplemented, nil
110+
return ExitStatusNotImplemented, nil, nil
116111
}
117112

118113
if commandLine.CompilerOptions().Version.IsTrue() {
119114
printVersion(sys)
120-
return ExitStatusSuccess, nil
115+
return ExitStatusSuccess, nil, nil
121116
}
122117

123118
if commandLine.CompilerOptions().Help.IsTrue() || commandLine.CompilerOptions().All.IsTrue() {
124119
printHelp(sys, commandLine)
125-
return ExitStatusSuccess, nil
120+
return ExitStatusSuccess, nil, nil
126121
}
127122

128123
if commandLine.CompilerOptions().Watch.IsTrue() && commandLine.CompilerOptions().ListFilesOnly.IsTrue() {
129124
reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.Options_0_and_1_cannot_be_combined, "watch", "listFilesOnly"))
130-
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil
125+
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil, nil
131126
}
132127

133128
if commandLine.CompilerOptions().Project != "" {
134129
if len(commandLine.FileNames()) != 0 {
135130
reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line))
136-
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil
131+
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil, nil
137132
}
138133

139134
fileOrDirectory := tspath.NormalizePath(commandLine.CompilerOptions().Project)
140135
if sys.FS().DirectoryExists(fileOrDirectory) {
141136
configFileName = tspath.CombinePaths(fileOrDirectory, "tsconfig.json")
142137
if !sys.FS().FileExists(configFileName) {
143138
reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.Cannot_find_a_tsconfig_json_file_at_the_current_directory_Colon_0, configFileName))
144-
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil
139+
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil, nil
145140
}
146141
} else {
147142
configFileName = fileOrDirectory
148143
if !sys.FS().FileExists(configFileName) {
149144
reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.The_specified_path_does_not_exist_Colon_0, fileOrDirectory))
150-
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil
145+
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil, nil
151146
}
152147
}
153148
} else if len(commandLine.FileNames()) == 0 {
@@ -162,7 +157,7 @@ func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine) (ExitS
162157
printVersion(sys)
163158
printHelp(sys, commandLine)
164159
}
165-
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil
160+
return ExitStatusDiagnosticsPresent_OutputsSkipped, nil, nil
166161
}
167162

168163
// !!! convert to options with absolute paths is usually done here, but for ease of implementation, it's done in `tsoptions.ParseCommandLine()`
@@ -179,7 +174,7 @@ func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine) (ExitS
179174
for _, e := range errors {
180175
reportDiagnostic(e)
181176
}
182-
return ExitStatusDiagnosticsPresent_OutputsGenerated, nil
177+
return ExitStatusDiagnosticsPresent_OutputsGenerated, nil, nil
183178
}
184179
configForCompilation = configParseResult
185180
// Updater to reflect pretty
@@ -188,26 +183,28 @@ func tscCompilation(sys System, commandLine *tsoptions.ParsedCommandLine) (ExitS
188183

189184
if compilerOptionsFromCommandLine.ShowConfig.IsTrue() {
190185
showConfig(sys, configForCompilation.CompilerOptions())
191-
return ExitStatusSuccess, nil
186+
return ExitStatusSuccess, nil, nil
192187
}
193188
if configForCompilation.CompilerOptions().Watch.IsTrue() {
194-
return ExitStatusSuccess, createWatcher(sys, configForCompilation, reportDiagnostic)
189+
return ExitStatusSuccess, nil, createWatcher(sys, configForCompilation, reportDiagnostic, testing)
195190
} else if configForCompilation.CompilerOptions().IsIncremental() {
196-
return performIncrementalCompilation(
191+
exitStatus, program := performIncrementalCompilation(
197192
sys,
198193
configForCompilation,
199194
reportDiagnostic,
200195
&extendedConfigCache,
201196
configTime,
202-
), nil
197+
testing,
198+
)
199+
return exitStatus, program, nil
203200
}
204201
return performCompilation(
205202
sys,
206203
configForCompilation,
207204
reportDiagnostic,
208205
&extendedConfigCache,
209206
configTime,
210-
), nil
207+
), nil, nil
211208
}
212209

213210
func findConfigFile(searchPath string, fileExists func(string) bool, configName string) string {
@@ -230,7 +227,8 @@ func performIncrementalCompilation(
230227
reportDiagnostic diagnosticReporter,
231228
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry],
232229
configTime time.Duration,
233-
) ExitStatus {
230+
testing bool,
231+
) (ExitStatus, *incremental.Program) {
234232
host := compiler.NewCachedFSCompilerHost(config.CompilerOptions(), sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache)
235233
oldProgram := incremental.ReadBuildInfoProgram(config, incremental.NewBuildInfoReader(host))
236234
// todo: cache, statistics, tracing
@@ -241,7 +239,7 @@ func performIncrementalCompilation(
241239
JSDocParsingMode: ast.JSDocParsingModeParseForTypeErrors,
242240
})
243241
parseTime := sys.Now().Sub(parseStart)
244-
incrementalProgram := incremental.NewProgram(program, oldProgram)
242+
incrementalProgram := incremental.NewProgram(program, oldProgram, testing)
245243
return emitAndReportStatistics(
246244
sys,
247245
incrementalProgram,
@@ -250,7 +248,7 @@ func performIncrementalCompilation(
250248
reportDiagnostic,
251249
configTime,
252250
parseTime,
253-
)
251+
), incrementalProgram
254252
}
255253

256254
func performCompilation(

internal/execute/tsctestrunner_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/microsoft/typescript-go/internal/execute"
12+
"github.com/microsoft/typescript-go/internal/incremental"
1213
"github.com/microsoft/typescript-go/internal/testutil/baseline"
1314
)
1415

@@ -34,30 +35,28 @@ func (test *tscInput) run(t *testing.T, scenario string) {
3435
// initial test tsc compile
3536
baselineBuilder := test.startBaseline()
3637

37-
exit, parsedCommandLine, watcher := execute.CommandLineTest(test.sys, test.commandLineArgs)
38+
exit, parsedCommandLine, incrementalProgram, watcher := execute.CommandLine(test.sys, test.commandLineArgs, true)
3839
baselineBuilder.WriteString("ExitStatus:: " + fmt.Sprint(exit))
3940

4041
compilerOptionsString, _ := json.MarshalIndent(parsedCommandLine.CompilerOptions(), "", " ")
4142
baselineBuilder.WriteString("\n\nCompilerOptions::")
4243
baselineBuilder.Write(compilerOptionsString)
4344

44-
if watcher != nil {
45-
execute.StartForTest(watcher)
46-
}
47-
4845
test.sys.serializeState(baselineBuilder)
49-
46+
test.sys.baselineProgram(baselineBuilder, incrementalProgram, watcher)
5047
for _, do := range test.edits {
5148
do.edit(test.sys)
5249
baselineBuilder.WriteString("\n\nEdit:: " + do.caption + "\n")
5350

51+
var incrementalProgram *incremental.Program
5452
if watcher == nil {
55-
exit, parsedCommandLine, watcher = execute.CommandLineTest(test.sys, test.commandLineArgs)
53+
exit, parsedCommandLine, incrementalProgram, watcher = execute.CommandLine(test.sys, test.commandLineArgs, true)
5654
baselineBuilder.WriteString("ExitStatus:: " + fmt.Sprint(exit))
5755
} else {
58-
execute.RunWatchCycle(watcher)
56+
watcher.DoCycle()
5957
}
6058
test.sys.serializeState(baselineBuilder)
59+
test.sys.baselineProgram(baselineBuilder, incrementalProgram, watcher)
6160
}
6261

6362
options, name := test.getBaselineName(scenario, "")
@@ -117,7 +116,7 @@ func (test *tscInput) verifyCommandLineParsing(t *testing.T, scenario string) {
117116
// initial test tsc compile
118117
baselineBuilder := test.startBaseline()
119118

120-
exit, parsedCommandLine, _ := execute.CommandLineTest(test.sys, test.commandLineArgs)
119+
exit, parsedCommandLine, _, _ := execute.CommandLine(test.sys, test.commandLineArgs, true)
121120
baselineBuilder.WriteString("ExitStatus:: " + fmt.Sprint(exit))
122121
//nolint:musttag
123122
parsedCommandLineString, _ := json.MarshalIndent(parsedCommandLine, "", " ")

internal/execute/watch.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)