Skip to content

Commit 9b77dd6

Browse files
Merge pull request #16595 from alexeagle/pretty
--pretty diagnostics: move context after the file/error
2 parents d407f14 + 9753d39 commit 9b77dd6

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/compiler/program.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ namespace ts {
271271
export function formatDiagnosticsWithColorAndContext(diagnostics: ReadonlyArray<Diagnostic>, host: FormatDiagnosticsHost): string {
272272
let output = "";
273273
for (const diagnostic of diagnostics) {
274+
let context = "";
274275
if (diagnostic.file) {
275276
const { start, length, file } = diagnostic;
276277
const { line: firstLine, character: firstLineChar } = getLineAndCharacterOfPosition(file, start);
@@ -284,12 +285,12 @@ namespace ts {
284285
gutterWidth = Math.max(ellipsis.length, gutterWidth);
285286
}
286287

287-
output += host.getNewLine();
288+
context += host.getNewLine();
288289
for (let i = firstLine; i <= lastLine; i++) {
289290
// If the error spans over 5 lines, we'll only show the first 2 and last 2 lines,
290291
// so we'll skip ahead to the second-to-last line.
291292
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();
293294
i = lastLine - 1;
294295
}
295296

@@ -300,30 +301,28 @@ namespace ts {
300301
lineContent = lineContent.replace("\t", " "); // convert tabs to single spaces
301302

302303
// 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();
305306

306307
// 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;
309310
if (i === firstLine) {
310311
// If we're on the last line, then limit it to the last character of the last line.
311312
// Otherwise, we'll just squiggle the rest of the line, giving 'slice' no end position.
312313
const lastCharForLine = i === lastLine ? lastLineChar : undefined;
313314

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, "~");
316317
}
317318
else if (i === lastLine) {
318-
output += lineContent.slice(0, lastLineChar).replace(/./g, "~");
319+
context += lineContent.slice(0, lastLineChar).replace(/./g, "~");
319320
}
320321
else {
321322
// Squiggle the entire line.
322-
output += lineContent.replace(/./g, "~");
323+
context += lineContent.replace(/./g, "~");
323324
}
324-
output += resetEscapeSequence;
325-
326-
output += host.getNewLine();
325+
context += resetEscapeSequence;
327326
}
328327

329328
output += host.getNewLine();
@@ -333,6 +332,12 @@ namespace ts {
333332
const categoryColor = getCategoryFormat(diagnostic.category);
334333
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
335334
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+
336341
output += host.getNewLine();
337342
}
338343
return output;

tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2+
tests/cases/compiler/index.ts(2,1): error TS1005: '}' expected.
3+
24
2
35
  
46

5-
tests/cases/compiler/index.ts(2,1): error TS1005: '}' expected.
6-
77

88
==== tests/cases/compiler/index.ts (1 errors) ====
99
if (true) {

0 commit comments

Comments
 (0)