@@ -271,6 +271,7 @@ namespace ts {
271
271
export function formatDiagnosticsWithColorAndContext ( diagnostics : ReadonlyArray < Diagnostic > , host : FormatDiagnosticsHost ) : string {
272
272
let output = "" ;
273
273
for ( const diagnostic of diagnostics ) {
274
+ let context = "" ;
274
275
if ( diagnostic . file ) {
275
276
const { start, length, file } = diagnostic ;
276
277
const { line : firstLine , character : firstLineChar } = getLineAndCharacterOfPosition ( file , start ) ;
@@ -284,12 +285,12 @@ namespace ts {
284
285
gutterWidth = Math . max ( ellipsis . length , gutterWidth ) ;
285
286
}
286
287
287
- output += host . getNewLine ( ) ;
288
+ context += host . getNewLine ( ) ;
288
289
for ( let i = firstLine ; i <= lastLine ; i ++ ) {
289
290
// If the error spans over 5 lines, we'll only show the first 2 and last 2 lines,
290
291
// so we'll skip ahead to the second-to-last line.
291
292
if ( hasMoreThanFiveLines && firstLine + 1 < i && i < lastLine - 1 ) {
292
- output += formatAndReset ( padLeft ( ellipsis , gutterWidth ) , gutterStyleSequence ) + gutterSeparator + host . getNewLine ( ) ;
293
+ context += formatAndReset ( padLeft ( ellipsis , gutterWidth ) , gutterStyleSequence ) + gutterSeparator + host . getNewLine ( ) ;
293
294
i = lastLine - 1 ;
294
295
}
295
296
@@ -300,30 +301,28 @@ namespace ts {
300
301
lineContent = lineContent . replace ( "\t" , " " ) ; // convert tabs to single spaces
301
302
302
303
// Output the gutter and the actual contents of the line.
303
- output += formatAndReset ( padLeft ( i + 1 + "" , gutterWidth ) , gutterStyleSequence ) + gutterSeparator ;
304
- output += lineContent + host . getNewLine ( ) ;
304
+ context += formatAndReset ( padLeft ( i + 1 + "" , gutterWidth ) , gutterStyleSequence ) + gutterSeparator ;
305
+ context += lineContent + host . getNewLine ( ) ;
305
306
306
307
// Output the gutter and the error span for the line using tildes.
307
- output += formatAndReset ( padLeft ( "" , gutterWidth ) , gutterStyleSequence ) + gutterSeparator ;
308
- output += redForegroundEscapeSequence ;
308
+ context += formatAndReset ( padLeft ( "" , gutterWidth ) , gutterStyleSequence ) + gutterSeparator ;
309
+ context += redForegroundEscapeSequence ;
309
310
if ( i === firstLine ) {
310
311
// If we're on the last line, then limit it to the last character of the last line.
311
312
// Otherwise, we'll just squiggle the rest of the line, giving 'slice' no end position.
312
313
const lastCharForLine = i === lastLine ? lastLineChar : undefined ;
313
314
314
- output += lineContent . slice ( 0 , firstLineChar ) . replace ( / \S / g, " " ) ;
315
- output += lineContent . slice ( firstLineChar , lastCharForLine ) . replace ( / ./ g, "~" ) ;
315
+ context += lineContent . slice ( 0 , firstLineChar ) . replace ( / \S / g, " " ) ;
316
+ context += lineContent . slice ( firstLineChar , lastCharForLine ) . replace ( / ./ g, "~" ) ;
316
317
}
317
318
else if ( i === lastLine ) {
318
- output += lineContent . slice ( 0 , lastLineChar ) . replace ( / ./ g, "~" ) ;
319
+ context += lineContent . slice ( 0 , lastLineChar ) . replace ( / ./ g, "~" ) ;
319
320
}
320
321
else {
321
322
// Squiggle the entire line.
322
- output += lineContent . replace ( / ./ g, "~" ) ;
323
+ context += lineContent . replace ( / ./ g, "~" ) ;
323
324
}
324
- output += resetEscapeSequence ;
325
-
326
- output += host . getNewLine ( ) ;
325
+ context += resetEscapeSequence ;
327
326
}
328
327
329
328
output += host . getNewLine ( ) ;
@@ -333,6 +332,12 @@ namespace ts {
333
332
const categoryColor = getCategoryFormat ( diagnostic . category ) ;
334
333
const category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
335
334
output += `${ formatAndReset ( category , categoryColor ) } TS${ diagnostic . code } : ${ flattenDiagnosticMessageText ( diagnostic . messageText , host . getNewLine ( ) ) } ` ;
335
+
336
+ if ( diagnostic . file ) {
337
+ output += host . getNewLine ( ) ;
338
+ output += context ;
339
+ }
340
+
336
341
output += host . getNewLine ( ) ;
337
342
}
338
343
return output ;
0 commit comments