-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform-calls.ts
64 lines (51 loc) · 1.43 KB
/
transform-calls.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
import * as ts from "typescript";
import { tsquery } from "@phenomnomnominal/tsquery";
export function transformCalls(
node: ts.Node,
options: { ctx: ts.TransformationContext; typeChecker: ts.TypeChecker }
) {
function visit(node: ts.Node) {
if (ts.isBlock(node)) {
const extraCode: any = [];
let i = 0;
function searchStatement(node: ts.Node) {
if (ts.isNewExpression(node)) {
extraCode.push({
code: `{
let state = {foo: 1};
}`,
index: i,
});
}
return ts.visitEachChild(node, searchStatement, options.ctx);
}
node.statements.forEach((statement, index) => {
i = index;
ts.visitEachChild(statement, searchStatement, options.ctx);
});
let newStatements = [...node.statements];
let extraStatements = extraCode.map(({ code, index }) => {
const ast = tsquery.ast(code);
const block: any = tsquery(ast, "Block")[0] as ts.Block;
block.pos = -1;
block.end = -1;
newStatements.splice(index + 1, 0, ...block.statements);
});
if (extraCode.length) {
}
node = ts.factory.updateBlock(node, [...newStatements]);
}
return node;
}
let result = ts.visitEachChild(node, visit, options.ctx);
return { result };
}
// function
// function findParent(node: ts.Node): ts.Block {
// if (!node.parent) return null;
// if (!ts.isBlock(node.parent)) {
// return findParent(node.parent);
// } else {
// return node.parent;
// }
// }