Skip to content

Commit ccd5608

Browse files
Merge pull request #20416 from JoshuaKGoldberg/tell-me-im-pretty
Prettified timestamps and error reports in --pretty
2 parents 69e091b + b315170 commit ccd5608

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

src/compiler/program.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,28 @@ namespace ts {
241241
return errorMessage;
242242
}
243243

244-
const redForegroundEscapeSequence = "\u001b[91m";
245-
const yellowForegroundEscapeSequence = "\u001b[93m";
246-
const blueForegroundEscapeSequence = "\u001b[93m";
244+
/** @internal */
245+
export enum ForegroundColorEscapeSequences {
246+
Grey = "\u001b[90m",
247+
Red = "\u001b[91m",
248+
Yellow = "\u001b[93m",
249+
Blue = "\u001b[94m",
250+
Cyan = "\u001b[96m"
251+
}
247252
const gutterStyleSequence = "\u001b[30;47m";
248253
const gutterSeparator = " ";
249254
const resetEscapeSequence = "\u001b[0m";
250255
const ellipsis = "...";
251256
function getCategoryFormat(category: DiagnosticCategory): string {
252257
switch (category) {
253-
case DiagnosticCategory.Warning: return yellowForegroundEscapeSequence;
254-
case DiagnosticCategory.Error: return redForegroundEscapeSequence;
255-
case DiagnosticCategory.Message: return blueForegroundEscapeSequence;
258+
case DiagnosticCategory.Warning: return ForegroundColorEscapeSequences.Yellow;
259+
case DiagnosticCategory.Error: return ForegroundColorEscapeSequences.Red;
260+
case DiagnosticCategory.Message: return ForegroundColorEscapeSequences.Blue;
256261
}
257262
}
258263

259-
function formatAndReset(text: string, formatStyle: string) {
264+
/** @internal */
265+
export function formatColorAndReset(text: string, formatStyle: string) {
260266
return formatStyle + text + resetEscapeSequence;
261267
}
262268

@@ -289,7 +295,7 @@ namespace ts {
289295
// If the error spans over 5 lines, we'll only show the first 2 and last 2 lines,
290296
// so we'll skip ahead to the second-to-last line.
291297
if (hasMoreThanFiveLines && firstLine + 1 < i && i < lastLine - 1) {
292-
context += formatAndReset(padLeft(ellipsis, gutterWidth), gutterStyleSequence) + gutterSeparator + host.getNewLine();
298+
context += formatColorAndReset(padLeft(ellipsis, gutterWidth), gutterStyleSequence) + gutterSeparator + host.getNewLine();
293299
i = lastLine - 1;
294300
}
295301

@@ -300,12 +306,12 @@ namespace ts {
300306
lineContent = lineContent.replace("\t", " "); // convert tabs to single spaces
301307

302308
// Output the gutter and the actual contents of the line.
303-
context += formatAndReset(padLeft(i + 1 + "", gutterWidth), gutterStyleSequence) + gutterSeparator;
309+
context += formatColorAndReset(padLeft(i + 1 + "", gutterWidth), gutterStyleSequence) + gutterSeparator;
304310
context += lineContent + host.getNewLine();
305311

306312
// Output the gutter and the error span for the line using tildes.
307-
context += formatAndReset(padLeft("", gutterWidth), gutterStyleSequence) + gutterSeparator;
308-
context += redForegroundEscapeSequence;
313+
context += formatColorAndReset(padLeft("", gutterWidth), gutterStyleSequence) + gutterSeparator;
314+
context += ForegroundColorEscapeSequences.Red;
309315
if (i === firstLine) {
310316
// If we're on the last line, then limit it to the last character of the last line.
311317
// Otherwise, we'll just squiggle the rest of the line, giving 'slice' no end position.
@@ -324,13 +330,19 @@ namespace ts {
324330
context += resetEscapeSequence;
325331
}
326332

327-
output += host.getNewLine();
328-
output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `;
333+
output += formatColorAndReset(relativeFileName, ForegroundColorEscapeSequences.Cyan);
334+
output += "(";
335+
output += formatColorAndReset(`${ firstLine + 1 }`, ForegroundColorEscapeSequences.Yellow);
336+
output += ",";
337+
output += formatColorAndReset(`${ firstLineChar + 1 }`, ForegroundColorEscapeSequences.Yellow);
338+
output += "): ";
329339
}
330340

331341
const categoryColor = getCategoryFormat(diagnostic.category);
332342
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
333-
output += `${ formatAndReset(category, categoryColor) } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine()) }`;
343+
output += formatColorAndReset(category, categoryColor);
344+
output += formatColorAndReset(` TS${ diagnostic.code }: `, ForegroundColorEscapeSequences.Grey);
345+
output += flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine());
334346

335347
if (diagnostic.file) {
336348
output += host.getNewLine();
@@ -339,7 +351,7 @@ namespace ts {
339351

340352
output += host.getNewLine();
341353
}
342-
return output;
354+
return output + host.getNewLine();
343355
}
344356

345357
export function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string {

src/compiler/watch.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ namespace ts {
4848
};
4949
}
5050

51+
/** @internal */
52+
export function createWatchDiagnosticReporterWithColor(system = sys): DiagnosticReporter {
53+
return diagnostic => {
54+
let output = `[${ formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey) }] `;
55+
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${system.newLine + system.newLine + system.newLine}`;
56+
system.write(output);
57+
};
58+
}
59+
5160
export function reportDiagnostics(diagnostics: Diagnostic[], reportDiagnostic: DiagnosticReporter): void {
5261
for (const diagnostic of diagnostics) {
5362
reportDiagnostic(diagnostic);
@@ -131,7 +140,7 @@ namespace ts {
131140
reportWatchDiagnostic?: DiagnosticReporter
132141
): WatchingSystemHost {
133142
reportDiagnostic = reportDiagnostic || createDiagnosticReporter(system, pretty ? reportDiagnosticWithColorAndContext : reportDiagnosticSimply);
134-
reportWatchDiagnostic = reportWatchDiagnostic || createWatchDiagnosticReporter(system);
143+
reportWatchDiagnostic = reportWatchDiagnostic || pretty ? createWatchDiagnosticReporterWithColor(system) : createWatchDiagnosticReporter(system);
135144
parseConfigFile = parseConfigFile || ts.parseConfigFile;
136145
return {
137146
system,

tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
2-
tests/cases/compiler/index.ts(2,1): error TS1005: '}' expected.
1+
tests/cases/compiler/index.ts(2,1): error TS1005: '}' expected.
32

43
2
54
  
65

76

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

0 commit comments

Comments
 (0)