-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.ts
38 lines (35 loc) · 986 Bytes
/
debug.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
import {
createSourceFile,
NodeArray,
ScriptTarget,
Node,
SyntaxKind,
} from "typescript";
import * as fs from "fs";
const { statements } = createSourceFile(
process.argv[2],
fs.readFileSync(process.argv[2]).toString(),
ScriptTarget.ES2017
);
const debug = (statements: NodeArray<Node>) => {
return statements.map(
({ kind, modifiers, flags, parent, decorators, pos, end, ...rest }) => {
return {
kind: SyntaxKind[kind],
modifiers: modifiers?.map((mod) => SyntaxKind[mod.kind]),
...Object.keys(rest).reduce((prev, curr) => {
const val = (rest as any)[curr];
if (val instanceof Array) {
prev[curr] = debug(val as any);
} else if (typeof val === "object") {
prev[curr] = debug([val] as any);
} else {
prev[curr] = val;
}
return prev;
}, {} as any),
};
}
);
};
console.log(JSON.stringify(debug(statements), null, 1));