-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdiagnostics.ts
77 lines (67 loc) · 2.04 KB
/
diagnostics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as tsTypes from "typescript";
import { red, white, yellow } from "colors/safe";
import { tsModule } from "./tsproxy";
import { RollupContext } from "./context";
import { formatHost } from "./diagnostics-format-host";
export interface IDiagnostics
{
flatMessage: string;
formatted: string;
fileLine?: string;
category: tsTypes.DiagnosticCategory;
code: number;
type: string;
}
export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[]
{
return data.map((diagnostic) =>
{
const entry: IDiagnostics = {
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()),
formatted: tsModule.formatDiagnosticsWithColorAndContext(data, formatHost),
category: diagnostic.category,
code: diagnostic.code,
type,
};
if (diagnostic.file && diagnostic.start !== undefined)
{
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
entry.fileLine = `${diagnostic.file.fileName}(${line + 1},${character + 1})`;
}
return entry;
});
}
export function printDiagnostics(context: RollupContext, diagnostics: IDiagnostics[], pretty = true): void
{
diagnostics.forEach((diagnostic) =>
{
let print;
let color;
let category;
switch (diagnostic.category)
{
case tsModule.DiagnosticCategory.Message:
print = context.info;
color = white;
category = "";
break;
case tsModule.DiagnosticCategory.Error:
print = context.error;
color = red;
category = "error";
break;
case tsModule.DiagnosticCategory.Warning:
default:
print = context.warn;
color = yellow;
category = "warning";
break;
}
const type = diagnostic.type + " ";
if (pretty)
return print.call(context, `${diagnostic.formatted}`);
if (diagnostic.fileLine !== undefined)
return print.call(context, `${diagnostic.fileLine}: ${type}${category} TS${diagnostic.code}: ${color(diagnostic.flatMessage)}`);
return print.call(context, `${type}${category} TS${diagnostic.code}: ${color(diagnostic.flatMessage)}`);
});
}