-
Notifications
You must be signed in to change notification settings - Fork 1
/
typescript.ts
executable file
·92 lines (81 loc) · 2.5 KB
/
typescript.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as ts from "typescript";
import { CheckResult } from ".";
import { GithubCheckAnnotation } from "./octokit-types";
/**
* Get all Typescript compiler diagnostics for the given TS project.
*/
export async function typescriptCheck(
configFileName: string
): Promise<CheckResult> {
const diagnosticHost = {
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
getCanonicalFileName: (s: string) => s
};
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(
configFileName,
{},
{
...ts.sys,
onUnRecoverableConfigFileDiagnostic: (diagnostic: ts.Diagnostic) => {
ts.formatDiagnostic(diagnostic, diagnosticHost);
}
}
);
const program = ts.createProgram(parsedCommandLine?.fileNames || [], {
...parsedCommandLine?.options,
noEmit: true
});
const emitResult = program.emit();
const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
const annotations: GithubCheckAnnotation[] = [];
const globalErrors = [];
let errorCount = 0;
let warningCount = 0;
for (const diagnostic of allDiagnostics) {
if (diagnostic.category === ts.DiagnosticCategory.Error) {
errorCount++;
} else if (diagnostic.category === ts.DiagnosticCategory.Warning) {
warningCount++;
}
if (diagnostic.file) {
const start = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start ?? 0
);
const end = diagnostic.file.getLineAndCharacterOfPosition(
(diagnostic.start ?? 0) + (diagnostic.length ?? 0)
);
annotations.push({
annotation_level:
diagnostic.category === ts.DiagnosticCategory.Error
? "failure"
: diagnostic.category === ts.DiagnosticCategory.Warning
? "warning"
: "notice",
start_line: start.line + 1,
end_line: end.line + 1,
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
path: diagnostic.file.fileName
});
} else if (diagnostic.category === ts.DiagnosticCategory.Error) {
globalErrors.push(
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
);
}
}
let consoleOutput = ts.formatDiagnosticsWithColorAndContext(
allDiagnostics,
diagnosticHost
);
if (globalErrors.length > 0) {
consoleOutput = `${globalErrors.join("\n")}\n${consoleOutput}`;
}
return {
errorCount,
warningCount,
annotations,
consoleOutput
};
}