1ac7a6ce695e4a8dbe3a107ba37249c712795b48.svn-base
9.51 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = memberExpressionToFunctions;
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
class AssignmentMemoiser {
constructor() {
this._map = new WeakMap();
}
has(key) {
return this._map.has(key);
}
get(key) {
if (!this.has(key)) return;
const record = this._map.get(key);
const {
value
} = record;
record.count--;
if (record.count === 0) {
return t.assignmentExpression("=", value, key);
}
return value;
}
set(key, value, count) {
return this._map.set(key, {
count,
value
});
}
}
function toNonOptional(path, base) {
const {
node
} = path;
if (path.isOptionalMemberExpression()) {
return t.memberExpression(base, node.property, node.computed);
}
if (path.isOptionalCallExpression()) {
const callee = path.get("callee");
if (path.node.optional && callee.isOptionalMemberExpression()) {
const {
object
} = callee.node;
const context = path.scope.maybeGenerateMemoised(object) || object;
callee.get("object").replaceWith(t.assignmentExpression("=", context, object));
return t.callExpression(t.memberExpression(base, t.identifier("call")), [context, ...node.arguments]);
}
return t.callExpression(base, node.arguments);
}
return path.node;
}
function isInDetachedTree(path) {
while (path) {
if (path.isProgram()) break;
const {
parentPath,
container,
listKey
} = path;
const parentNode = parentPath.node;
if (listKey) {
if (container !== parentNode[listKey]) return true;
} else {
if (container !== parentNode) return true;
}
path = parentPath;
}
return false;
}
const handle = {
memoise() {},
handle(member) {
const {
node,
parent,
parentPath
} = member;
if (member.isOptionalMemberExpression()) {
if (isInDetachedTree(member)) return;
const endPath = member.find(({
node,
parent,
parentPath
}) => {
if (parentPath.isOptionalMemberExpression()) {
return parent.optional || parent.object !== node;
}
if (parentPath.isOptionalCallExpression()) {
return node !== member.node && parent.optional || parent.callee !== node;
}
return true;
});
const rootParentPath = endPath.parentPath;
if (rootParentPath.isUpdateExpression({
argument: node
}) || rootParentPath.isAssignmentExpression({
left: node
})) {
throw member.buildCodeFrameError(`can't handle assignment`);
}
const isDeleteOperation = rootParentPath.isUnaryExpression({
operator: "delete"
});
if (isDeleteOperation && endPath.isOptionalMemberExpression() && endPath.get("property").isPrivateName()) {
throw member.buildCodeFrameError(`can't delete a private class element`);
}
let startingOptional = member;
for (;;) {
if (startingOptional.isOptionalMemberExpression()) {
if (startingOptional.node.optional) break;
startingOptional = startingOptional.get("object");
continue;
} else if (startingOptional.isOptionalCallExpression()) {
if (startingOptional.node.optional) break;
startingOptional = startingOptional.get("callee");
continue;
}
throw new Error(`Internal error: unexpected ${startingOptional.node.type}`);
}
const {
scope
} = member;
const startingProp = startingOptional.isOptionalMemberExpression() ? "object" : "callee";
const startingNode = startingOptional.node[startingProp];
const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode;
const parentIsOptionalCall = parentPath.isOptionalCallExpression({
callee: node
});
const parentIsCall = parentPath.isCallExpression({
callee: node
});
startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
if (parentIsOptionalCall) {
if (parent.optional) {
parentPath.replaceWith(this.optionalCall(member, parent.arguments));
} else {
parentPath.replaceWith(this.call(member, parent.arguments));
}
} else if (parentIsCall) {
member.replaceWith(this.boundGet(member));
} else {
member.replaceWith(this.get(member));
}
let regular = member.node;
for (let current = member; current !== endPath;) {
const {
parentPath
} = current;
if (parentPath === endPath && parentIsOptionalCall && parent.optional) {
regular = parentPath.node;
break;
}
regular = toNonOptional(parentPath, regular);
current = parentPath;
}
let context;
const endParentPath = endPath.parentPath;
if (t.isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
callee: endPath.node,
optional: true
})) {
const {
object
} = regular;
context = member.scope.maybeGenerateMemoised(object);
if (context) {
regular.object = t.assignmentExpression("=", context, object);
}
}
let replacementPath = endPath;
if (isDeleteOperation) {
replacementPath = endParentPath;
regular = endParentPath.node;
}
replacementPath.replaceWith(t.conditionalExpression(t.logicalExpression("||", t.binaryExpression("===", baseNeedsMemoised ? t.assignmentExpression("=", t.cloneNode(baseRef), t.cloneNode(startingNode)) : t.cloneNode(baseRef), t.nullLiteral()), t.binaryExpression("===", t.cloneNode(baseRef), scope.buildUndefinedNode())), isDeleteOperation ? t.booleanLiteral(true) : scope.buildUndefinedNode(), regular));
if (context) {
const endParent = endParentPath.node;
endParentPath.replaceWith(t.optionalCallExpression(t.optionalMemberExpression(endParent.callee, t.identifier("call"), false, true), [t.cloneNode(context), ...endParent.arguments], false));
}
return;
}
if (parentPath.isUpdateExpression({
argument: node
})) {
if (this.simpleSet) {
member.replaceWith(this.simpleSet(member));
return;
}
const {
operator,
prefix
} = parent;
this.memoise(member, 2);
const value = t.binaryExpression(operator[0], t.unaryExpression("+", this.get(member)), t.numericLiteral(1));
if (prefix) {
parentPath.replaceWith(this.set(member, value));
} else {
const {
scope
} = member;
const ref = scope.generateUidIdentifierBasedOnNode(node);
scope.push({
id: ref
});
value.left = t.assignmentExpression("=", t.cloneNode(ref), value.left);
parentPath.replaceWith(t.sequenceExpression([this.set(member, value), t.cloneNode(ref)]));
}
return;
}
if (parentPath.isAssignmentExpression({
left: node
})) {
if (this.simpleSet) {
member.replaceWith(this.simpleSet(member));
return;
}
const {
operator,
right
} = parent;
let value = right;
if (operator !== "=") {
this.memoise(member, 2);
value = t.binaryExpression(operator.slice(0, -1), this.get(member), value);
}
parentPath.replaceWith(this.set(member, value));
return;
}
if (parentPath.isCallExpression({
callee: node
})) {
parentPath.replaceWith(this.call(member, parent.arguments));
return;
}
if (parentPath.isOptionalCallExpression({
callee: node
})) {
parentPath.replaceWith(this.optionalCall(member, parent.arguments));
return;
}
if (parentPath.isForXStatement({
left: node
}) || parentPath.isObjectProperty({
value: node
}) && parentPath.parentPath.isObjectPattern() || parentPath.isAssignmentPattern({
left: node
}) && parentPath.parentPath.isObjectProperty({
value: parent
}) && parentPath.parentPath.parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isAssignmentPattern({
left: node
}) && parentPath.parentPath.isArrayPattern() || parentPath.isRestElement()) {
member.replaceWith(this.destructureSet(member));
return;
}
member.replaceWith(this.get(member));
}
};
function memberExpressionToFunctions(path, visitor, state) {
path.traverse(visitor, Object.assign({}, handle, state, {
memoiser: new AssignmentMemoiser()
}));
}