-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathfunctionOutlining.ts
225 lines (185 loc) · 6.06 KB
/
functionOutlining.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
213
214
215
216
217
218
219
220
221
222
223
224
225
import { NodePath } from "@babel/traverse";
import { PluginArg, PluginObject } from "./plugin";
import { Order } from "../order";
import { ensureComputedExpression, prepend } from "../utils/ast-utils";
import * as t from "@babel/types";
import { NameGen } from "../utils/NameGen";
import { chance, getRandomInteger } from "../utils/random-utils";
import { Binding, Visitor } from "@babel/traverse";
import { computeProbabilityMap } from "../probability";
function isSafeForOutlining(path: NodePath): {
isSafe: boolean;
bindings?: Binding[];
} {
if (path.isIdentifier() || path.isLiteral()) return { isSafe: false };
// Skip direct invocations ('this' will be different)
if (path.key === "callee" && path.parentPath.isCallExpression()) {
return { isSafe: false };
}
// Skip typeof and delete expressions (identifier behavior is different)
if (path.key === "argument" && path.parentPath.isUnaryExpression()) {
return { isSafe: false };
}
if (
path.isReturnStatement() ||
path.isYieldExpression() ||
path.isAwaitExpression() ||
path.isContinueStatement() ||
path.isBreakStatement() ||
path.isThrowStatement() ||
path.isDebuggerStatement() ||
path.isImportDeclaration() ||
path.isExportDeclaration()
) {
return { isSafe: false };
}
var isSafe = true;
var bindings: Binding[] = [];
var fnPath = path.getFunctionParent();
var visitor: Visitor = {
ThisExpression(path) {
isSafe = false;
path.stop();
},
Identifier(path) {
if (["arguments", "eval"].includes(path.node.name)) {
isSafe = false;
path.stop();
}
},
BindingIdentifier(path) {
var binding = path.scope.getBinding(path.node.name);
if (binding) {
bindings.push(binding);
}
},
// Function flow guard
"ReturnStatement|YieldExpression|AwaitExpression"(path) {
if (path.getFunctionParent() === fnPath) {
isSafe = false;
path.stop();
}
},
};
// Exclude 'ThisExpression' and semantic 'Identifier' nodes
if (visitor[path.type]) return { isSafe: false };
path.traverse(visitor);
return { isSafe, bindings };
}
export default ({ Plugin }: PluginArg): PluginObject => {
const me = Plugin(Order.FunctionOutlining, {
changeData: {
functionsMade: 0,
},
});
var changesMade = 0;
function checkProbability() {
if (!computeProbabilityMap(me.options.functionOutlining)) return false;
if (changesMade > 100 && chance(changesMade - 100)) return false;
return true;
}
return {
visitor: {
Block: {
exit(blockPath) {
if (blockPath.isProgram()) {
blockPath.scope.crawl();
}
if (blockPath.find((p) => me.isSkipped(p))) return;
if (!checkProbability()) return;
// Extract a random number of statements
var statements = blockPath.get("body");
// var startIndex = getRandomInteger(0, statements.length);
// var endIndex = getRandomInteger(startIndex, statements.length);
var startIndex = 0;
var endIndex = statements.length;
var extractedStatements = statements.slice(startIndex, endIndex);
if (!extractedStatements.length) return;
var bindings: Binding[] = [];
for (var statement of extractedStatements) {
// Don't override the control node
if (me.isSkipped(statement)) return;
var result = isSafeForOutlining(statement);
if (!result.isSafe) {
return;
}
bindings.push(...result.bindings);
}
const extractedStatementSet = new Set<NodePath>(extractedStatements);
for (var binding of bindings) {
for (var referencePath of binding.referencePaths) {
var found = referencePath.find((p) =>
extractedStatementSet.has(p)
);
if (!found) {
return;
}
}
for (var constantViolation of binding.constantViolations) {
var found = constantViolation.find((p) =>
extractedStatementSet.has(p)
);
if (!found) {
return;
}
}
}
changesMade++;
var isFirst = true;
for (var statement of extractedStatements) {
if (isFirst) {
isFirst = false;
var memberExpression = me
.getControlObject(blockPath)
.addProperty(
t.functionExpression(
null,
[],
t.blockStatement(extractedStatements.map((x) => x.node))
)
);
me.changeData.functionsMade++;
var callExpression = t.callExpression(memberExpression, []);
statement.replaceWith(callExpression);
continue;
}
statement.remove();
}
},
},
Expression: {
exit(path) {
// Skip assignment left
if (
path.find(
(p) =>
p.key === "left" &&
p.parentPath?.type === "AssignmentExpression"
)
) {
return;
}
if (!checkProbability()) return;
if (path.find((p) => me.isSkipped(p))) return;
if (!isSafeForOutlining(path).isSafe) return;
changesMade++;
var blockPath = path.find((p) => p.isBlock()) as NodePath<t.Block>;
var memberExpression = me
.getControlObject(blockPath)
.addProperty(
t.functionExpression(
null,
[],
t.blockStatement([t.returnStatement(t.cloneNode(path.node))])
)
);
me.changeData.functionsMade++;
var callExpression = t.callExpression(memberExpression, []);
ensureComputedExpression(path);
path.replaceWith(callExpression);
me.skip(path);
},
},
},
};
};