Skip to content

Commit 1841664

Browse files
authored
Separate error reporting, string baselining from compiler baselining (#217)
1 parent bb7812b commit 1841664

File tree

7 files changed

+69
-45
lines changed

7 files changed

+69
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ go.work.sum
175175

176176
# Local baselines
177177
testdata/baselines/local
178+
testdata/baselines/tmp
178179

179180
custom-gcl
180181
custom-gcl.exe

cmd/tsgo/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/microsoft/typescript-go/internal/bundled"
1616
ts "github.com/microsoft/typescript-go/internal/compiler"
1717
"github.com/microsoft/typescript-go/internal/core"
18+
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
1819
"github.com/microsoft/typescript-go/internal/scanner"
1920
"github.com/microsoft/typescript-go/internal/tspath"
2021
"github.com/microsoft/typescript-go/internal/vfs"
@@ -141,13 +142,13 @@ func main() {
141142
UseCaseSensitiveFileNames: useCaseSensitiveFileNames,
142143
}
143144
if pretty {
144-
formatOpts := ts.DiagnosticsFormattingOptions{
145+
formatOpts := diagnosticwriter.FormattingOptions{
145146
NewLine: "\n",
146147
ComparePathsOptions: comparePathOptions,
147148
}
148-
ts.FormatDiagnosticsWithColorAndContext(os.Stdout, diagnostics, &formatOpts)
149+
diagnosticwriter.FormatDiagnosticsWithColorAndContext(os.Stdout, diagnostics, &formatOpts)
149150
fmt.Fprintln(os.Stdout)
150-
ts.WriteErrorSummaryText(os.Stdout, diagnostics, &formatOpts)
151+
diagnosticwriter.WriteErrorSummaryText(os.Stdout, diagnostics, &formatOpts)
151152
} else {
152153
for _, diagnostic := range diagnostics {
153154
printDiagnostic(diagnostic, 0, comparePathOptions)

internal/compiler/error_reporting.go renamed to internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package compiler
1+
package diagnosticwriter
22

33
import (
44
"fmt"
@@ -15,7 +15,7 @@ import (
1515
"github.com/microsoft/typescript-go/internal/tspath"
1616
)
1717

18-
type DiagnosticsFormattingOptions struct {
18+
type FormattingOptions struct {
1919
tspath.ComparePathsOptions
2020
NewLine string
2121
}
@@ -35,7 +35,7 @@ const (
3535
ellipsis = "..."
3636
)
3737

38-
func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
38+
func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnostic, formatOpts *FormattingOptions) {
3939
if len(diags) == 0 {
4040
return
4141
}
@@ -80,7 +80,7 @@ func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnos
8080
}
8181
}
8282

83-
func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, length int, squiggleColor string, formatOpts *DiagnosticsFormattingOptions) {
83+
func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, length int, squiggleColor string, formatOpts *FormattingOptions) {
8484
firstLine, firstLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start)
8585
lastLine, lastLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start+length)
8686

@@ -196,7 +196,7 @@ func writeWithStyleAndReset(output io.Writer, text string, formatStyle string) {
196196
fmt.Fprint(output, resetEscapeSequence)
197197
}
198198

199-
func WriteLocation(output io.Writer, file *ast.SourceFile, pos int, formatOpts *DiagnosticsFormattingOptions, writeWithStyleAndReset FormattedWriter) {
199+
func WriteLocation(output io.Writer, file *ast.SourceFile, pos int, formatOpts *FormattingOptions, writeWithStyleAndReset FormattedWriter) {
200200
firstLine, firstChar := scanner.GetLineAndCharacterOfPosition(file, pos)
201201
var relativeFileName string
202202
if formatOpts != nil {
@@ -221,7 +221,7 @@ type ErrorSummary struct {
221221
SortedFileList []*ast.SourceFile
222222
}
223223

224-
func WriteErrorSummaryText(output io.Writer, allDiagnostics []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
224+
func WriteErrorSummaryText(output io.Writer, allDiagnostics []*ast.Diagnostic, formatOpts *FormattingOptions) {
225225
// Roughly corresponds to 'getErrorSummaryText' from watch.ts
226226

227227
errorSummary := getErrorSummary(allDiagnostics)
@@ -299,7 +299,7 @@ func getErrorSummary(diags []*ast.Diagnostic) *ErrorSummary {
299299
}
300300
}
301301

302-
func writeTabularErrorsDisplay(output io.Writer, errorSummary *ErrorSummary, formatOpts *DiagnosticsFormattingOptions) {
302+
func writeTabularErrorsDisplay(output io.Writer, errorSummary *ErrorSummary, formatOpts *FormattingOptions) {
303303
sortedFiles := errorSummary.SortedFileList
304304

305305
maxErrors := 0
@@ -330,7 +330,7 @@ func writeTabularErrorsDisplay(output io.Writer, errorSummary *ErrorSummary, for
330330
}
331331
}
332332

333-
func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) string {
333+
func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic, formatOpts *FormattingOptions) string {
334334
line, _ := scanner.GetLineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
335335
fileName := file.FileName()
336336
if tspath.PathIsAbsolute(fileName) && tspath.PathIsAbsolute(formatOpts.CurrentDirectory) {
@@ -344,13 +344,13 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
344344
)
345345
}
346346

347-
func WriteFormatDiagnostics(output io.Writer, diagnostics []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
347+
func WriteFormatDiagnostics(output io.Writer, diagnostics []*ast.Diagnostic, formatOpts *FormattingOptions) {
348348
for _, diagnostic := range diagnostics {
349349
WriteFormatDiagnostic(output, diagnostic, formatOpts)
350350
}
351351
}
352352

353-
func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
353+
func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *FormattingOptions) {
354354
if diagnostic.File() != nil {
355355
line, character := scanner.GetLineAndCharacterOfPosition(diagnostic.File(), diagnostic.Loc().Pos())
356356
fileName := diagnostic.File().FileName()

internal/testutil/baseline/baseline.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,35 @@ type Options struct {
1616
const NoContent = "<no content>"
1717

1818
func Run(t *testing.T, fileName string, actual string, opts Options) {
19-
writeComparison(t, actual, fileName, opts)
19+
writeComparison(t, actual, fileName, false /*useSubmodule*/, opts)
2020
}
2121

22-
func writeComparison(t *testing.T, actual string, relativeFileName string, opts Options) {
22+
func RunAgainstSubmodule(t *testing.T, fileName string, actual string, opts Options) {
23+
writeComparison(t, actual, fileName, true /*useSubmodule*/, opts)
24+
}
25+
26+
func writeComparison(t *testing.T, actual string, relativeFileName string, useSubmodule bool, opts Options) {
2327
if actual == "" {
24-
panic("The generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
28+
panic("the generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
2529
}
2630
var (
2731
localFileName string
2832
referenceFileName string
2933
)
3034

31-
localFileName = localPath(relativeFileName, opts.Subfolder)
32-
referenceFileName = referencePath(relativeFileName, opts.Subfolder)
33-
expected := getExpectedContent(relativeFileName, opts)
35+
if useSubmodule {
36+
localFileName = submoduleLocalPath(relativeFileName, opts.Subfolder)
37+
referenceFileName = submoduleReferencePath(relativeFileName, opts.Subfolder)
38+
} else {
39+
localFileName = localPath(relativeFileName, opts.Subfolder)
40+
referenceFileName = referencePath(relativeFileName, opts.Subfolder)
41+
}
42+
43+
expected := NoContent
44+
if content, err := os.ReadFile(referenceFileName); err == nil {
45+
expected = string(content)
46+
}
47+
3448
if _, err := os.Stat(localFileName); err == nil {
3549
if err := os.Remove(localFileName); err != nil {
3650
t.Fatal(fmt.Errorf("failed to remove the local baseline file %s: %w", localFileName, err))
@@ -49,26 +63,31 @@ func writeComparison(t *testing.T, actual string, relativeFileName string, opts
4963
}
5064

5165
if _, err := os.Stat(referenceFileName); err != nil {
52-
t.Errorf("New baseline created at %s.", localFileName)
66+
if useSubmodule {
67+
t.Errorf("the baseline file %s does not exist in the TypeScript submodule", referenceFileName)
68+
} else {
69+
t.Errorf("new baseline created at %s.", localFileName)
70+
}
71+
} else if useSubmodule {
72+
t.Errorf("the baseline file %s does not match the reference in the TypeScript submodule", relativeFileName)
5373
} else {
54-
t.Errorf("The baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relativeFileName)
74+
t.Errorf("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relativeFileName)
5575
}
5676
}
5777
}
5878

59-
func getExpectedContent(relativeFileName string, opts Options) string {
60-
refFileName := referencePath(relativeFileName, opts.Subfolder)
61-
expected := NoContent
62-
if content, err := os.ReadFile(refFileName); err == nil {
63-
expected = string(content)
64-
}
65-
return expected
66-
}
67-
6879
func localPath(fileName string, subfolder string) string {
6980
return filepath.Join(repo.TestDataPath, "baselines", "local", subfolder, fileName)
7081
}
7182

83+
func submoduleLocalPath(fileName string, subfolder string) string {
84+
return filepath.Join(repo.TestDataPath, "baselines", "tmp", subfolder, fileName)
85+
}
86+
7287
func referencePath(fileName string, subfolder string) string {
7388
return filepath.Join(repo.TestDataPath, "baselines", "reference", subfolder, fileName)
7489
}
90+
91+
func submoduleReferencePath(fileName string, subfolder string) string {
92+
return filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference", subfolder, fileName)
93+
}

internal/testutil/baseline/error_baseline.go renamed to internal/testutil/tsbaseline/error_baseline.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package baseline
1+
package tsbaseline
22

33
import (
44
"fmt"
@@ -11,6 +11,8 @@ import (
1111
"github.com/microsoft/typescript-go/internal/ast"
1212
"github.com/microsoft/typescript-go/internal/compiler"
1313
"github.com/microsoft/typescript-go/internal/core"
14+
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
15+
"github.com/microsoft/typescript-go/internal/testutil/baseline"
1416
"github.com/microsoft/typescript-go/internal/tspath"
1517
"gotest.tools/v3/assert"
1618
"gotest.tools/v3/assert/cmp"
@@ -19,7 +21,7 @@ import (
1921
// IO
2022
const harnessNewLine = "\r\n"
2123

22-
var formatOpts = &compiler.DiagnosticsFormattingOptions{
24+
var formatOpts = &diagnosticwriter.FormattingOptions{
2325
NewLine: harnessNewLine,
2426
}
2527

@@ -40,17 +42,17 @@ func DoErrorBaseline(t *testing.T, baselinePath string, inputFiles []*TestFile,
4042
if len(errors) > 0 {
4143
errorBaseline = getErrorBaseline(t, inputFiles, errors, pretty)
4244
} else {
43-
errorBaseline = NoContent
45+
errorBaseline = baseline.NoContent
4446
}
45-
Run(t, baselinePath, errorBaseline, Options{})
47+
baseline.Run(t, baselinePath, errorBaseline, baseline.Options{})
4648
}
4749

4850
func minimalDiagnosticsToString(diagnostics []*ast.Diagnostic, pretty bool) string {
4951
var output strings.Builder
5052
if pretty {
51-
compiler.FormatDiagnosticsWithColorAndContext(&output, diagnostics, formatOpts)
53+
diagnosticwriter.FormatDiagnosticsWithColorAndContext(&output, diagnostics, formatOpts)
5254
} else {
53-
compiler.WriteFormatDiagnostics(&output, diagnostics, formatOpts)
55+
diagnosticwriter.WriteFormatDiagnostics(&output, diagnostics, formatOpts)
5456
}
5557
return output.String()
5658
}
@@ -61,7 +63,7 @@ func getErrorBaseline(t *testing.T, inputFiles []*TestFile, diagnostics []*ast.D
6163

6264
if pretty {
6365
var summaryBuilder strings.Builder
64-
compiler.WriteErrorSummaryText(
66+
diagnosticwriter.WriteErrorSummaryText(
6567
&summaryBuilder,
6668
diagnostics,
6769
formatOpts)
@@ -248,12 +250,12 @@ func iterateErrorBaseline(t *testing.T, inputFiles []*TestFile, inputDiagnostics
248250

249251
func flattenDiagnosticMessage(d *ast.Diagnostic, newLine string) string {
250252
var output strings.Builder
251-
compiler.WriteFlattenedDiagnosticMessage(&output, d, newLine)
253+
diagnosticwriter.WriteFlattenedDiagnosticMessage(&output, d, newLine)
252254
return output.String()
253255
}
254256

255-
func formatLocation(file *ast.SourceFile, pos int, formatOpts *compiler.DiagnosticsFormattingOptions, writeWithStyleAndReset compiler.FormattedWriter) string {
257+
func formatLocation(file *ast.SourceFile, pos int, formatOpts *diagnosticwriter.FormattingOptions, writeWithStyleAndReset diagnosticwriter.FormattedWriter) string {
256258
var output strings.Builder
257-
compiler.WriteLocation(&output, file, pos, formatOpts, writeWithStyleAndReset)
259+
diagnosticwriter.WriteLocation(&output, file, pos, formatOpts, writeWithStyleAndReset)
258260
return output.String()
259261
}

internal/testutil/baseline/symbol_baseline.go renamed to internal/testutil/tsbaseline/symbol_baseline.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package baseline
1+
package tsbaseline
22

33
import (
44
"fmt"
@@ -12,6 +12,7 @@ import (
1212
"github.com/microsoft/typescript-go/internal/compiler"
1313
"github.com/microsoft/typescript-go/internal/core"
1414
"github.com/microsoft/typescript-go/internal/scanner"
15+
"github.com/microsoft/typescript-go/internal/testutil/baseline"
1516
"github.com/microsoft/typescript-go/internal/tspath"
1617
)
1718

@@ -27,7 +28,7 @@ func DoTypeAndSymbolBaseline(
2728
header string,
2829
program *compiler.Program,
2930
allFiles []*TestFile,
30-
opts Options,
31+
opts baseline.Options,
3132
skipTypeBaselines bool,
3233
skipSymbolBaselines bool,
3334
hasErrorBaseline bool,
@@ -63,13 +64,13 @@ func checkBaselines(
6364
allFiles []*TestFile,
6465
fullWalker *typeWriterWalker,
6566
header string,
66-
opts Options,
67+
opts baseline.Options,
6768
isSymbolBaseline bool,
6869
) {
6970
fullExtension := core.IfElse(isSymbolBaseline, ".symbols", ".types")
7071
outputFileName := tspath.RemoveFileExtension(baselinePath)
7172
fullBaseline := generateBaseline(allFiles, fullWalker, header, isSymbolBaseline)
72-
Run(t, outputFileName+fullExtension, fullBaseline, opts)
73+
baseline.Run(t, outputFileName+fullExtension, fullBaseline, opts)
7374
}
7475

7576
func generateBaseline(

internal/testutil/baseline/util.go renamed to internal/testutil/tsbaseline/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package baseline
1+
package tsbaseline
22

33
import (
44
"regexp"

0 commit comments

Comments
 (0)