/
index.js
311 lines (259 loc) 路 9.23 KB
/
index.js
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
import syntaxObjectRestSpread from "babel-plugin-syntax-object-rest-spread";
export default function ({ types: t }) {
function hasRestElement(path) {
let foundRestElement = false;
path.traverse({
RestElement() {
foundRestElement = true;
path.stop();
},
});
return foundRestElement;
}
function hasSpread(node) {
for (const prop of (node.properties)) {
if (t.isSpreadElement(prop)) {
return true;
}
}
return false;
}
function createObjectSpread(file, props, objRef) {
const restElement = props.pop();
const keys = [];
for (const prop of props) {
let key = prop.key;
if (t.isIdentifier(key) && !prop.computed) {
key = t.stringLiteral(prop.key.name);
}
keys.push(key);
}
return [
restElement.argument,
t.callExpression(
file.addHelper("objectWithoutProperties"), [
objRef,
t.arrayExpression(keys),
]
),
];
}
function replaceRestElement(parentPath, paramPath, i, numParams) {
if (paramPath.isAssignmentPattern()) {
replaceRestElement(parentPath, paramPath.get("left"), i, numParams);
return;
}
if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
const elements = paramPath.get("elements");
for (let i = 0; i < elements.length; i++) {
replaceRestElement(parentPath, elements[i], i, elements.length);
}
}
if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
const uid = parentPath.scope.generateUidIdentifier("ref");
const declar = t.variableDeclaration("let", [
t.variableDeclarator(paramPath.node, uid),
]);
declar._blockHoist = i ? numParams - i : 1;
parentPath.ensureBlock();
parentPath.get("body").unshiftContainer("body", declar);
paramPath.replaceWith(uid);
}
}
return {
inherits: syntaxObjectRestSpread,
visitor: {
// taken from transform-es2015-parameters/src/destructuring.js
// function a({ b, ...c }) {}
Function(path) {
const params = path.get("params");
for (let i = 0; i < params.length; i++) {
replaceRestElement(params[i].parentPath, params[i], i, params.length);
}
},
// adapted from transform-es2015-destructuring/src/index.js#pushObjectRest
// const { a, ...b } = c;
VariableDeclarator(path, file) {
if (!path.get("id").isObjectPattern()) { return; }
let insertionPath = path;
path.get("id").traverse({
RestElement(path) {
if (
// skip single-property case, e.g.
// const { ...x } = foo();
// since the RHS will not be duplicated
this.originalPath.node.id.properties.length > 1 &&
!t.isIdentifier(this.originalPath.node.init)
) {
// const { a, ...b } = foo();
// to avoid calling foo() twice, as a first step convert it to:
// const _foo = foo(),
// { a, ...b } = _foo;
const initRef = path.scope.generateUidIdentifierBasedOnNode(
this.originalPath.node.init, "ref");
// insert _foo = foo()
this.originalPath.insertBefore(t.variableDeclarator(initRef,
this.originalPath.node.init));
// replace foo() with _foo
this.originalPath.replaceWith(t.variableDeclarator(
this.originalPath.node.id, initRef));
return;
}
let ref = this.originalPath.node.init;
let refProperty = null;
path.findParent((path) => {
if (path.isObjectProperty()) {
const id = t.identifier(path.node.key.name);
if (refProperty) {
ref = t.memberExpression(ref, id);
} else {
refProperty = id;
}
} else if (path.isVariableDeclarator()) {
return true;
}
});
if (refProperty) {
ref = t.memberExpression(ref, refProperty);
}
const [ argument, callExpression ] = createObjectSpread(
file,
path.parentPath.node.properties,
ref
);
insertionPath.insertAfter(
t.variableDeclarator(
argument,
callExpression
)
);
insertionPath = insertionPath.getSibling(insertionPath.key + 1);
if (path.parentPath.node.properties.length === 0) {
path.findParent(
(path) => path.isObjectProperty() || path.isVariableDeclarator()
).remove();
}
},
}, {
originalPath: path,
});
},
// taken from transform-es2015-destructuring/src/index.js#visitor
// export var { a, ...b } = c;
ExportNamedDeclaration(path) {
const declaration = path.get("declaration");
if (!declaration.isVariableDeclaration()) return;
if (!hasRestElement(declaration)) return;
const specifiers = [];
for (const name in path.getOuterBindingIdentifiers(path)) {
const id = t.identifier(name);
specifiers.push(t.exportSpecifier(id, id));
}
// Split the declaration and export list into two declarations so that the variable
// declaration can be split up later without needing to worry about not being a
// top-level statement.
path.replaceWith(declaration.node);
path.insertAfter(t.exportNamedDeclaration(null, specifiers));
},
// try {} catch ({a, ...b}) {}
CatchClause(path) {
const paramPath = path.get("param");
replaceRestElement(paramPath.parentPath, paramPath);
},
// ({a, ...b} = c);
AssignmentExpression(path, file) {
const leftPath = path.get("left");
if (leftPath.isObjectPattern() && hasRestElement(leftPath)) {
const nodes = [];
let ref;
if (path.isCompletionRecord() || path.parentPath.isExpressionStatement()) {
ref = path.scope.generateUidIdentifierBasedOnNode(path.node.right, "ref");
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(ref, path.node.right),
]));
}
const [ argument, callExpression ] = createObjectSpread(
file,
path.node.left.properties,
ref
);
const nodeWithoutSpread = t.clone(path.node);
nodeWithoutSpread.right = ref;
nodes.push(t.expressionStatement(nodeWithoutSpread));
nodes.push(t.toStatement(t.assignmentExpression(
"=",
argument,
callExpression
)));
if (ref) {
nodes.push(t.expressionStatement(ref));
}
path.replaceWithMultiple(nodes);
}
},
// taken from transform-es2015-destructuring/src/index.js#visitor
ForXStatement(path) {
const { node, scope } = path;
const leftPath = path.get("left");
const left = node.left;
// for ({a, ...b} of []) {}
if (t.isObjectPattern(left) && hasRestElement(leftPath)) {
const temp = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration("var", [
t.variableDeclarator(temp),
]);
path.ensureBlock();
node.body.body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(left, temp),
]));
return;
}
if (!t.isVariableDeclaration(left)) return;
const pattern = left.declarations[0].id;
if (!t.isObjectPattern(pattern)) return;
const key = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration(left.kind, [
t.variableDeclarator(key, null),
]);
path.ensureBlock();
node.body.body.unshift(
t.variableDeclaration(node.left.kind, [
t.variableDeclarator(pattern, key),
])
);
},
// var a = { ...b, ...c }
ObjectExpression(path, file) {
if (!hasSpread(path.node)) return;
const useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== "boolean") {
throw new Error("transform-object-rest-spread currently only accepts a boolean " +
"option for useBuiltIns (defaults to false)");
}
const args = [];
let props = [];
function push() {
if (!props.length) return;
args.push(t.objectExpression(props));
props = [];
}
for (const prop of (path.node.properties: Array)) {
if (t.isSpreadElement(prop)) {
push();
args.push(prop.argument);
} else {
props.push(prop);
}
}
push();
if (!t.isObjectExpression(args[0])) {
args.unshift(t.objectExpression([]));
}
const helper = useBuiltIns ?
t.memberExpression(t.identifier("Object"), t.identifier("assign")) :
file.addHelper("extends");
path.replaceWith(t.callExpression(helper, args));
},
},
};
}