-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
compiler.ts
52 lines (41 loc) · 1.42 KB
/
compiler.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
import * as path from 'path';
import {createProgram, getPreEmitDiagnostics, ScriptTarget, ModuleResolutionKind, flattenDiagnosticMessageText} from 'typescript';
import {Diagnostic, Context} from './interfaces';
// List of diagnostic codes that should be ignored
const ignoredDiagnostics = new Set<number>([
1308 // Support top-level `await`
]);
const loadConfig = () => {
return {
moduleResolution: ModuleResolutionKind.NodeJs,
target: ScriptTarget.ES2015
};
};
/**
* Get a list of TypeScript diagnostics within the current context.
*
* @param context - The context object.
* @returns List of diagnostics
*/
export const getDiagnostics = (context: Context): Diagnostic[] => {
const compilerOptions = loadConfig();
const fileName = path.join(context.cwd, context.testFile);
const program = createProgram([fileName], compilerOptions);
// Retrieve the TypeScript compiler diagnostics
const diagnostics = getPreEmitDiagnostics(program);
const result: Diagnostic[] = [];
for (const diagnostic of diagnostics) {
if (!diagnostic.file || ignoredDiagnostics.has(diagnostic.code)) {
continue;
}
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start as number);
result.push({
fileName: diagnostic.file.fileName,
message: flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
severity: 'error',
line: position.line + 1,
column: position.character
});
}
return result;
};