Skip to content

Commit

Permalink
Merge pull request #20416 from JoshuaKGoldberg/tell-me-im-pretty
Browse files Browse the repository at this point in the history
Prettified timestamps and error reports in --pretty
  • Loading branch information
DanielRosenwasser committed Dec 19, 2017
2 parents 69e091b + b315170 commit ccd5608
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
42 changes: 27 additions & 15 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,28 @@ namespace ts {
return errorMessage;
}

const redForegroundEscapeSequence = "\u001b[91m";
const yellowForegroundEscapeSequence = "\u001b[93m";
const blueForegroundEscapeSequence = "\u001b[93m";
/** @internal */
export enum ForegroundColorEscapeSequences {
Grey = "\u001b[90m",
Red = "\u001b[91m",
Yellow = "\u001b[93m",
Blue = "\u001b[94m",
Cyan = "\u001b[96m"
}
const gutterStyleSequence = "\u001b[30;47m";
const gutterSeparator = " ";
const resetEscapeSequence = "\u001b[0m";
const ellipsis = "...";
function getCategoryFormat(category: DiagnosticCategory): string {
switch (category) {
case DiagnosticCategory.Warning: return yellowForegroundEscapeSequence;
case DiagnosticCategory.Error: return redForegroundEscapeSequence;
case DiagnosticCategory.Message: return blueForegroundEscapeSequence;
case DiagnosticCategory.Warning: return ForegroundColorEscapeSequences.Yellow;
case DiagnosticCategory.Error: return ForegroundColorEscapeSequences.Red;
case DiagnosticCategory.Message: return ForegroundColorEscapeSequences.Blue;
}
}

function formatAndReset(text: string, formatStyle: string) {
/** @internal */
export function formatColorAndReset(text: string, formatStyle: string) {
return formatStyle + text + resetEscapeSequence;
}

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

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

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

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

output += host.getNewLine();
output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `;
output += formatColorAndReset(relativeFileName, ForegroundColorEscapeSequences.Cyan);
output += "(";
output += formatColorAndReset(`${ firstLine + 1 }`, ForegroundColorEscapeSequences.Yellow);
output += ",";
output += formatColorAndReset(`${ firstLineChar + 1 }`, ForegroundColorEscapeSequences.Yellow);
output += "): ";
}

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

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

output += host.getNewLine();
}
return output;
return output + host.getNewLine();
}

export function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string {
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ namespace ts {
};
}

/** @internal */
export function createWatchDiagnosticReporterWithColor(system = sys): DiagnosticReporter {
return diagnostic => {
let output = `[${ formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey) }] `;
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${system.newLine + system.newLine + system.newLine}`;
system.write(output);
};
}

export function reportDiagnostics(diagnostics: Diagnostic[], reportDiagnostic: DiagnosticReporter): void {
for (const diagnostic of diagnostics) {
reportDiagnostic(diagnostic);
Expand Down Expand Up @@ -131,7 +140,7 @@ namespace ts {
reportWatchDiagnostic?: DiagnosticReporter
): WatchingSystemHost {
reportDiagnostic = reportDiagnostic || createDiagnosticReporter(system, pretty ? reportDiagnosticWithColorAndContext : reportDiagnosticSimply);
reportWatchDiagnostic = reportWatchDiagnostic || createWatchDiagnosticReporter(system);
reportWatchDiagnostic = reportWatchDiagnostic || pretty ? createWatchDiagnosticReporterWithColor(system) : createWatchDiagnosticReporter(system);
parseConfigFile = parseConfigFile || ts.parseConfigFile;
return {
system,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

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

2
  



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

Expand Down

0 comments on commit ccd5608

Please sign in to comment.