1 | import { formatDiagnostics, sys, } from "typescript";
|
2 | import { getCanonicalFileName } from "./fs/path-utils";
|
3 | export default class DiagnosticsHandlerImpl {
|
4 | constructor(options) {
|
5 | this.write = sys.write;
|
6 | this.throwOnError = options.throwOnError;
|
7 | this.host = createFormatDiagnosticsHost(options.workingPath);
|
8 | }
|
9 | setWrite(write) {
|
10 | this.write = write;
|
11 | }
|
12 | check(diagnostics, throwOnError) {
|
13 | const normalized = normalize(diagnostics);
|
14 | if (normalized === undefined) {
|
15 | return false;
|
16 | }
|
17 | const message = this.format(normalized);
|
18 | if (this.throwOnError || throwOnError === true) {
|
19 | throw new Error(message);
|
20 | }
|
21 | this.write(message);
|
22 | return true;
|
23 | }
|
24 | format(diagnostics) {
|
25 | return formatDiagnostics(diagnostics, this.host);
|
26 | }
|
27 | }
|
28 | function normalize(diagnostics) {
|
29 | if (diagnostics === undefined) {
|
30 | return undefined;
|
31 | }
|
32 | if (Array.isArray(diagnostics)) {
|
33 | return diagnostics.length === 0 ? undefined : diagnostics;
|
34 | }
|
35 | return [diagnostics];
|
36 | }
|
37 | function createFormatDiagnosticsHost(rootPath) {
|
38 | const newLine = sys.newLine;
|
39 | return {
|
40 | getCanonicalFileName,
|
41 | getCurrentDirectory: () => rootPath,
|
42 | getNewLine: () => newLine,
|
43 | };
|
44 | }
|
45 |
|
\ | No newline at end of file |