Skip to content

Commit a29db5d

Browse files
committed
Fix new line with error reporting
1 parent f4a79e2 commit a29db5d

23 files changed

+77
-36
lines changed

internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,41 @@ func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnos
4444
if i > 0 {
4545
fmt.Fprint(output, formatOpts.NewLine)
4646
}
47+
FormatDiagnosticWithColorAndContext(output, diagnostic, formatOpts)
48+
}
49+
}
4750

48-
if diagnostic.File() != nil {
49-
file := diagnostic.File()
50-
pos := diagnostic.Loc().Pos()
51-
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
52-
fmt.Fprint(output, " - ")
53-
}
51+
func FormatDiagnosticWithColorAndContext(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *FormattingOptions) {
52+
if diagnostic.File() != nil {
53+
file := diagnostic.File()
54+
pos := diagnostic.Loc().Pos()
55+
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
56+
fmt.Fprint(output, " - ")
57+
}
5458

55-
writeWithStyleAndReset(output, diagnostic.Category().Name(), getCategoryFormat(diagnostic.Category()))
56-
fmt.Fprintf(output, "%s TS%d: %s", foregroundColorEscapeGrey, diagnostic.Code(), resetEscapeSequence)
57-
WriteFlattenedDiagnosticMessage(output, diagnostic, formatOpts.NewLine)
59+
writeWithStyleAndReset(output, diagnostic.Category().Name(), getCategoryFormat(diagnostic.Category()))
60+
fmt.Fprintf(output, "%s TS%d: %s", foregroundColorEscapeGrey, diagnostic.Code(), resetEscapeSequence)
61+
WriteFlattenedDiagnosticMessage(output, diagnostic, formatOpts.NewLine)
5862

59-
if diagnostic.File() != nil && diagnostic.Code() != diagnostics.File_appears_to_be_binary.Code() {
60-
fmt.Fprint(output, formatOpts.NewLine)
61-
writeCodeSnippet(output, diagnostic.File(), diagnostic.Pos(), diagnostic.Len(), getCategoryFormat(diagnostic.Category()), "", formatOpts)
62-
fmt.Fprint(output, formatOpts.NewLine)
63-
}
63+
if diagnostic.File() != nil && diagnostic.Code() != diagnostics.File_appears_to_be_binary.Code() {
64+
fmt.Fprint(output, formatOpts.NewLine)
65+
writeCodeSnippet(output, diagnostic.File(), diagnostic.Pos(), diagnostic.Len(), getCategoryFormat(diagnostic.Category()), "", formatOpts)
66+
fmt.Fprint(output, formatOpts.NewLine)
67+
}
6468

65-
if (diagnostic.RelatedInformation() != nil) && (len(diagnostic.RelatedInformation()) > 0) {
66-
for _, relatedInformation := range diagnostic.RelatedInformation() {
67-
file := relatedInformation.File()
68-
if file != nil {
69-
fmt.Fprint(output, formatOpts.NewLine)
70-
fmt.Fprint(output, " ")
71-
pos := relatedInformation.Pos()
72-
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
73-
fmt.Fprint(output, " - ")
74-
WriteFlattenedDiagnosticMessage(output, relatedInformation, formatOpts.NewLine)
75-
writeCodeSnippet(output, file, pos, relatedInformation.Len(), foregroundColorEscapeCyan, " ", formatOpts)
76-
}
69+
if (diagnostic.RelatedInformation() != nil) && (len(diagnostic.RelatedInformation()) > 0) {
70+
for _, relatedInformation := range diagnostic.RelatedInformation() {
71+
file := relatedInformation.File()
72+
if file != nil {
7773
fmt.Fprint(output, formatOpts.NewLine)
74+
fmt.Fprint(output, " ")
75+
pos := relatedInformation.Pos()
76+
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
77+
fmt.Fprint(output, " - ")
78+
WriteFlattenedDiagnosticMessage(output, relatedInformation, formatOpts.NewLine)
79+
writeCodeSnippet(output, file, pos, relatedInformation.Len(), foregroundColorEscapeCyan, " ", formatOpts)
7880
}
81+
fmt.Fprint(output, formatOpts.NewLine)
7982
}
8083
}
8184
}

internal/execute/outputs.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ func getFormatOptsOfSys(sys System) *diagnosticwriter.FormattingOptions {
3131

3232
type diagnosticReporter = func(*ast.Diagnostic)
3333

34+
func quietDiagnosticReporter(diagnostic *ast.Diagnostic) {}
3435
func createDiagnosticReporter(sys System, options *core.CompilerOptions) diagnosticReporter {
3536
if options.Quiet.IsTrue() {
36-
return func(diagnostic *ast.Diagnostic) {}
37+
return quietDiagnosticReporter
3738
}
3839

3940
formatOpts := getFormatOptsOfSys(sys)
40-
if !shouldBePretty(sys, options) {
41-
return func(diagnostic *ast.Diagnostic) {
42-
diagnosticwriter.WriteFormatDiagnostic(sys.Writer(), diagnostic, formatOpts)
43-
}
44-
}
41+
writeDiagnostic := core.IfElse(shouldBePretty(sys, options), diagnosticwriter.FormatDiagnosticWithColorAndContext, diagnosticwriter.WriteFormatDiagnostic)
42+
4543
return func(diagnostic *ast.Diagnostic) {
46-
diagnosticwriter.FormatDiagnosticsWithColorAndContext(sys.Writer(), []*ast.Diagnostic{diagnostic}, formatOpts)
44+
writeDiagnostic(sys.Writer(), diagnostic, formatOpts)
45+
fmt.Fprint(sys.Writer(), formatOpts.NewLine)
4746
}
4847
}
4948

testdata/baselines/reference/tsbuild/commandLine/when-build-not-first-argument.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ Input::
55
tsgo --verbose --build
66
ExitStatus:: DiagnosticsPresent_OutputsSkipped
77
Output::
8-
error TS5093: Compiler option '--verbose' may only be used with '--build'.error TS6369: Option '--build' must be the first command line argument.
8+
error TS5093: Compiler option '--verbose' may only be used with '--build'.
9+
error TS6369: Option '--build' must be the first command line argument.
10+

testdata/baselines/reference/tsc/extends/configDir-template-with-commandline.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ Directory '/node_modules' does not exist, skipping all lookups in it.
9999

100100
3 "compilerOptions": {
101101
   ~~~~~~~~~~~~~~~~~
102+
102103
tsconfig.json:3:5 - error TS5102: Option 'baseUrl' has been removed. Please remove it from your configuration.
103104
Use '"paths": {"*": ["./*"]}' instead.
104105

105106
3 "compilerOptions": {
106107
   ~~~~~~~~~~~~~~~~~
108+
107109
../../tslibs/TS/Lib/lib.d.ts
108110
Default library for target 'ES5'
109111
types/sometype.ts

testdata/baselines/reference/tsc/extends/configDir-template.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ Directory '/node_modules' does not exist, skipping all lookups in it.
9999

100100
3 "compilerOptions": {
101101
   ~~~~~~~~~~~~~~~~~
102+
102103
tsconfig.json:3:5 - error TS5102: Option 'baseUrl' has been removed. Please remove it from your configuration.
103104
Use '"paths": {"*": ["./*"]}' instead.
104105

105106
3 "compilerOptions": {
106107
   ~~~~~~~~~~~~~~~~~
108+
107109
../../tslibs/TS/Lib/lib.d.ts
108110
Default library for target 'ES5'
109111
types/sometype.ts

testdata/baselines/reference/tsc/incremental/change-to-modifier-of-class-expression-field-with-declaration-emit-enabled.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,13 @@ Output::
184184
MessageablePerson.ts:6:7 - Add a type annotation to the variable wrapper.
185185
6 const wrapper = () => Messageable();
186186
   ~~~~~~~
187+
187188
main.ts:3:25 - error TS2445: Property 'message' is protected and only accessible within class 'MessageableClass' and its subclasses.
188189

189190
3 console.log( person.message );
190191
   ~~~~~~~
191192

193+
192194
Found 2 errors in 2 files.
193195

194196
Errors Files
@@ -321,11 +323,13 @@ Output::
321323
MessageablePerson.ts:6:7 - Add a type annotation to the variable wrapper.
322324
6 const wrapper = () => Messageable();
323325
   ~~~~~~~
326+
324327
main.ts:3:25 - error TS2445: Property 'message' is protected and only accessible within class 'MessageableClass' and its subclasses.
325328

326329
3 console.log( person.message );
327330
   ~~~~~~~
328331

332+
329333
Found 2 errors in 2 files.
330334

331335
Errors Files

testdata/baselines/reference/tsc/incremental/change-to-modifier-of-class-expression-field.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Output::
155155
3 console.log( person.message );
156156
   ~~~~~~~
157157

158+
158159
Found 1 error in main.ts:3
159160

160161
//// [/home/src/workspaces/project/MessageablePerson.js] *rewrite with same content*
@@ -254,6 +255,7 @@ Output::
254255
3 console.log( person.message );
255256
   ~~~~~~~
256257

258+
257259
Found 1 error in main.ts:3
258260

259261

testdata/baselines/reference/tsc/incremental/change-to-type-that-gets-used-as-global-through-export-in-another-file-through-indirect-import.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,12 @@ Diff:: Currently there is issue with d.ts emit for export default = 1 to widen i
301301
+declare const a = 1;
302302
--- nonIncremental.output.txt
303303
+++ incremental.output.txt
304-
@@ -1,7 +0,0 @@
304+
@@ -1,8 +0,0 @@
305305
-class1.ts:1:7 - error TS2322: Type '1' is not assignable to type '2'.
306306
-
307307
-1 const a: MagicNumber = 1;
308308
-   ~
309309
-
310+
-
310311
-Found 1 error in class1.ts:1
311312
-

testdata/baselines/reference/tsc/incremental/change-to-type-that-gets-used-as-global-through-export-in-another-file.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,12 @@ Diff:: Currently there is issue with d.ts emit for export default = 1 to widen i
251251
+declare const a = 1;
252252
--- nonIncremental.output.txt
253253
+++ incremental.output.txt
254-
@@ -1,7 +0,0 @@
254+
@@ -1,8 +0,0 @@
255255
-class1.ts:1:7 - error TS2322: Type '1' is not assignable to type '2'.
256256
-
257257
-1 const a: MagicNumber = 1;
258258
-   ~
259259
-
260+
-
260261
-Found 1 error in class1.ts:1
261262
-

testdata/baselines/reference/tsc/incremental/react-jsx-emit-mode-with-no-backing-types-found-doesnt-crash-under---strict.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Output::
3535
1 export const App = () => <div propA={true}></div>;
3636
   ~~~~~~~~~~~~~~~~~~~~~~~~
3737

38+
3839
Found 1 error in src/index.tsx:1
3940

4041
//// [/home/src/tslibs/TS/Lib/lib.d.ts] *Lib*

0 commit comments

Comments
 (0)