From fb6a8755de2ec1dd094a89cf4127dee80498d270 Mon Sep 17 00:00:00 2001 From: Serge Paquet Date: Wed, 4 Sep 2019 17:41:51 -0400 Subject: [PATCH] Pretty-print diagnostics with formatDiagnosticsWithColorAndContext if available --- src/checker/runtime.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/checker/runtime.ts b/src/checker/runtime.ts index 8af6e34..b1ff094 100644 --- a/src/checker/runtime.ts +++ b/src/checker/runtime.ts @@ -1,4 +1,5 @@ import * as ts from 'typescript' +import * as os from 'os' import * as path from 'path' import * as micromatch from 'micromatch' import chalk from 'chalk' @@ -557,6 +558,17 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re const pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start) line = pos.line character = pos.character + } + + if (ts.formatDiagnosticsWithColorAndContext) { + pretty = ts.formatDiagnosticsWithColorAndContext([diagnostic], { + getCurrentDirectory: () => context, + getCanonicalFileName: (fileName) => path.normalize(fileName), + getNewLine: () => os.EOL, + }); + pretty = `[${instanceName}]: ${pretty}`; + } + else if (diagnostic.file) { pretty = `[${instanceName}] ${chalk.red(fileName)}:${line + 1}:${character + 1} \n TS${code}: ${chalk.red(message)}` } else {