From 34e81f2805fd57616c65efa607dcdced47a0161e Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 13 Jul 2016 09:13:55 -0700 Subject: [PATCH] Add formatDiagnostics utility --- src/compiler/program.ts | 17 +++++++++++++++++ src/compiler/tsc.ts | 19 ++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 320b628c31..151bac9ed6 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -997,6 +997,23 @@ namespace ts { return sortAndDeduplicateDiagnostics(diagnostics); } + export function formatDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): string { + let output = ""; + + for (const diagnostic of diagnostics) { + if (diagnostic.file) { + const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); + const fileName = diagnostic.file.fileName; + const relativeFileName = convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName)); + output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `; + } + + const category = DiagnosticCategory[diagnostic.category].toLowerCase(); + output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }${ sys.newLine }`; + } + return output; + } + export function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string { if (typeof messageText === "string") { return messageText; diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index e25ae37e21..36f71f063b 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -101,23 +101,8 @@ namespace ts { return diagnostic.messageText; } - function getRelativeFileName(fileName: string, host: CompilerHost): string { - return host ? convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName)) : fileName; - } - function reportDiagnosticSimply(diagnostic: Diagnostic, host: CompilerHost): void { - let output = ""; - - if (diagnostic.file) { - const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); - const relativeFileName = getRelativeFileName(diagnostic.file.fileName, host); - output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `; - } - - const category = DiagnosticCategory[diagnostic.category].toLowerCase(); - output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }${ sys.newLine }`; - - sys.write(output); + sys.write(ts.formatDiagnostics([diagnostic], host)); } const redForegroundEscapeSequence = "\u001b[91m"; @@ -145,7 +130,7 @@ namespace ts { const { line: firstLine, character: firstLineChar } = getLineAndCharacterOfPosition(file, start); const { line: lastLine, character: lastLineChar } = getLineAndCharacterOfPosition(file, start + length); const lastLineInFile = getLineAndCharacterOfPosition(file, file.text.length).line; - const relativeFileName = getRelativeFileName(file.fileName, host); + const relativeFileName = host ? convertToRelativePath(file.fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName)) : file.fileName;; const hasMoreThanFiveLines = (lastLine - firstLine) >= 4; let gutterWidth = (lastLine + 1 + "").length;