-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
68 lines (57 loc) · 1.94 KB
/
index.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
import * as typescript from "typescript";
import { Input, Output, ActionStep } from "@shanzhai/interfaces";
/**
* Parses a TypeScript source file.
*/
export class ParseTypeScriptStep extends ActionStep {
/**
* @param name A description of the operation being performed.
* @param input An {@link Input} which supplies the unparsed
* TypeScript source file.
* @param compilerOptions An {@link Input} which supplies the TypeScript
* compiler options to use.
* @param output An {@link Output} which receives the parsed
* TypeScript source file.
*/
constructor(
public readonly input: Input<string>,
public readonly compilerOptions: Input<typescript.CompilerOptions>,
public readonly fileName: string,
public readonly output: Output<typescript.SourceFile>
) {
super(`Parse ${JSON.stringify(fileName)} as TypeScript`, output.effects);
}
/**
* @inheritdoc
*/
async execute(): Promise<void> {
const input = await this.input.get();
const options = await this.compilerOptions.get();
const result = typescript.createSourceFile(
this.fileName,
input,
options.target || typescript.ScriptTarget.ES3,
false,
typescript.ScriptKind.TS
);
const diagnostics = typescript
.createProgram({ rootNames: [], options })
.getSyntacticDiagnostics(result);
if (diagnostics.length > 0) {
let output = `Failed to parse TypeScript:`;
for (const diagnostic of diagnostics) {
const line =
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start).line +
1;
const message = typescript.flattenDiagnosticMessageText(
diagnostic.messageText,
`\n`,
1
);
output += `\nLine ${line}: ${message}`;
}
throw new Error(output);
}
await this.output.set(result);
}
}