-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.ts
212 lines (202 loc) · 6.16 KB
/
transform.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as ts from "typescript";
export default function transformer(): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => {
return (file: ts.SourceFile) => ts.visitEachChild(file, visitor, context);
function visitor(node: ts.Node): ts.VisitResult<ts.Node> {
if (ts.isCallExpression(node)) {
return visitCallExpression(node);
} else if (ts.isImportDeclaration(node)) {
return visitImportDeclaration(node);
} else {
return ts.visitEachChild(node, visitor, context);
}
}
function visitImportDeclaration(node: ts.ImportDeclaration) {
return node.moduleSpecifier.getText().slice(1, -1) ===
"@funkia/go-notation"
? undefined
: node;
}
function visitCallExpression(node: ts.CallExpression) {
if (node.expression.getText() === "go") {
const arg0 = node.arguments[0];
if (ts.isFunctionExpression(arg0) || ts.isArrowFunction(arg0)) {
const bindName = arg0.parameters[0].getText();
const statements = (arg0.body as ts.FunctionBody).statements;
return createImmediatelyInvokedFunction(
ts.createBlock(
visitGoBody(
bindName,
normalizeGoBody(bindName, Array.from(statements))
)
)
);
}
}
return ts.visitEachChild(node, visitor, context);
}
function visitGoBody(
bindName: string,
statements: ts.Statement[]
): ts.Statement[] {
// return normalizeGoBody(bindName, statements);
let cur = statements.shift();
let pre: ts.Statement[] = [];
while (cur !== undefined) {
if (ts.isVariableStatement(cur)) {
const declaration = cur.declarationList.declarations[0];
const identifier = declaration.name as ts.Identifier;
const exp = declaration.initializer!;
const next = visitGoExpression(bindName, identifier, exp, statements);
if (next !== undefined) {
return pre.concat(next);
}
}
pre.push(cur);
cur = statements.shift();
}
return pre;
}
function visitGoExpression(
bindName: string,
identifier: ts.Identifier,
exp: ts.Expression,
rest: ts.Statement[]
): ts.Statement[] | undefined {
if (ts.isCallExpression(exp) && exp.expression.getText() === bindName) {
return [
ts.createReturn(
createFlatMapCall(
exp.arguments[0],
identifier,
ts.createBlock(visitGoBody(bindName, rest), true)
)
)
];
}
}
};
}
function createFlatMapCall(
exp: ts.Expression,
argName: ts.Identifier,
body: ts.Block
) {
return ts.createCall(ts.createPropertyAccess(exp, "flatMap"), undefined, [
ts.createArrowFunction(
undefined,
undefined,
[ts.createParameter(undefined, undefined, undefined, argName)],
undefined,
undefined,
body
)
]);
}
function createImmediatelyInvokedFunction(block: ts.Block) {
return ts.createCall(
ts.createFunctionExpression(
undefined,
undefined,
undefined,
undefined,
[],
undefined,
block
),
undefined,
undefined
);
}
function normalizeGoBody(bindName: string, statements: ts.Statement[]) {
return statements.flatMap(s => {
if (ts.isVariableStatement(s)) {
const declaration = s.declarationList.declarations[0];
const identifier = declaration.name as ts.Identifier;
const init = declaration.initializer!;
// Special case form `const a = bind(...);`
if (ts.isCallExpression(init) && init.expression.getText() === bindName) {
const { pre, exp } = normalizeExpression(bindName, init.arguments[0]);
const updatedExp = ts.updateCall(init, init.expression, undefined, [
exp
]);
const updatedStatement = ts.updateVariableStatement(
s,
undefined,
ts.createVariableDeclarationList([
ts.updateVariableDeclaration(
s.declarationList.declarations[0],
identifier,
undefined,
updatedExp
)
])
);
return pre.concat(updatedStatement);
} else {
const { pre, exp } = normalizeExpression(
bindName,
declaration.initializer!
);
const updatedStatement = ts.updateVariableStatement(
s,
undefined,
ts.createVariableDeclarationList([
ts.updateVariableDeclaration(
s.declarationList.declarations[0],
identifier,
undefined,
exp
)
])
);
return pre.concat(updatedStatement);
}
}
return [s];
});
}
function normalizeExpression(
bindName: string,
exp: ts.Expression
): { pre: ts.Statement[]; exp: ts.Expression } {
if (exp.getText().includes(bindName)) {
if (ts.isCallExpression(exp)) {
const args = exp.arguments.map(a => normalizeExpression(bindName, a));
const pre = args.flatMap(a => a.pre);
const updatedExp = ts.updateCall(
exp,
exp.expression,
undefined,
args.map(a => a.exp)
);
const identifier = ts.createUniqueName("bind");
const dec = ts.createVariableStatement(
undefined,
ts.createVariableDeclarationList([
ts.createVariableDeclaration(identifier, undefined, updatedExp)
])
);
return {
pre: pre.concat(dec),
exp: identifier
};
} else if (ts.isBinaryExpression(exp)) {
const left = normalizeExpression(bindName, exp.left);
const right = normalizeExpression(bindName, exp.right);
const updatedExp = ts.updateBinary(exp, left.exp, right.exp);
return {
pre: left.pre.concat(right.pre),
exp: updatedExp
};
} else if (ts.isParenthesizedExpression(exp)) {
const norm = normalizeExpression(bindName, exp.expression);
const updatedExp = ts.updateParen(exp, norm.exp);
return {
pre: norm.pre,
exp: updatedExp
};
}
}
return { pre: [], exp };
}