1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
4 |
|
---|
5 | var helperPluginUtils = require('@babel/helper-plugin-utils');
|
---|
6 | var core = require('@babel/core');
|
---|
7 |
|
---|
8 | function isPureVoid(node) {
|
---|
9 | return core.types.isUnaryExpression(node) && node.operator === "void" && core.types.isPureish(node.argument);
|
---|
10 | }
|
---|
11 | function unshiftForXStatementBody(statementPath, newStatements) {
|
---|
12 | statementPath.ensureBlock();
|
---|
13 | const {
|
---|
14 | scope,
|
---|
15 | node
|
---|
16 | } = statementPath;
|
---|
17 | const bodyScopeBindings = statementPath.get("body").scope.bindings;
|
---|
18 | const hasShadowedBlockScopedBindings = Object.keys(bodyScopeBindings).some(name => scope.hasBinding(name));
|
---|
19 | if (hasShadowedBlockScopedBindings) {
|
---|
20 | node.body = core.types.blockStatement([...newStatements, node.body]);
|
---|
21 | } else {
|
---|
22 | node.body.body.unshift(...newStatements);
|
---|
23 | }
|
---|
24 | }
|
---|
25 | function hasArrayRest(pattern) {
|
---|
26 | return pattern.elements.some(elem => core.types.isRestElement(elem));
|
---|
27 | }
|
---|
28 | function hasObjectRest(pattern) {
|
---|
29 | return pattern.properties.some(prop => core.types.isRestElement(prop));
|
---|
30 | }
|
---|
31 | const STOP_TRAVERSAL = {};
|
---|
32 | const arrayUnpackVisitor = (node, ancestors, state) => {
|
---|
33 | if (!ancestors.length) {
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | if (core.types.isIdentifier(node) && core.types.isReferenced(node, ancestors[ancestors.length - 1].node) && state.bindings[node.name]) {
|
---|
37 | state.deopt = true;
|
---|
38 | throw STOP_TRAVERSAL;
|
---|
39 | }
|
---|
40 | };
|
---|
41 | class DestructuringTransformer {
|
---|
42 | constructor(opts) {
|
---|
43 | this.blockHoist = void 0;
|
---|
44 | this.operator = void 0;
|
---|
45 | this.arrayRefSet = void 0;
|
---|
46 | this.nodes = void 0;
|
---|
47 | this.scope = void 0;
|
---|
48 | this.kind = void 0;
|
---|
49 | this.iterableIsArray = void 0;
|
---|
50 | this.arrayLikeIsIterable = void 0;
|
---|
51 | this.objectRestNoSymbols = void 0;
|
---|
52 | this.useBuiltIns = void 0;
|
---|
53 | this.addHelper = void 0;
|
---|
54 | this.blockHoist = opts.blockHoist;
|
---|
55 | this.operator = opts.operator;
|
---|
56 | this.arrayRefSet = new Set();
|
---|
57 | this.nodes = opts.nodes || [];
|
---|
58 | this.scope = opts.scope;
|
---|
59 | this.kind = opts.kind;
|
---|
60 | this.iterableIsArray = opts.iterableIsArray;
|
---|
61 | this.arrayLikeIsIterable = opts.arrayLikeIsIterable;
|
---|
62 | this.objectRestNoSymbols = opts.objectRestNoSymbols;
|
---|
63 | this.useBuiltIns = opts.useBuiltIns;
|
---|
64 | this.addHelper = opts.addHelper;
|
---|
65 | }
|
---|
66 | getExtendsHelper() {
|
---|
67 | return this.useBuiltIns ? core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")) : this.addHelper("extends");
|
---|
68 | }
|
---|
69 | buildVariableAssignment(id, init) {
|
---|
70 | let op = this.operator;
|
---|
71 | if (core.types.isMemberExpression(id) || core.types.isOptionalMemberExpression(id)) op = "=";
|
---|
72 | let node;
|
---|
73 | if (op) {
|
---|
74 | node = core.types.expressionStatement(core.types.assignmentExpression(op, id, core.types.cloneNode(init) || this.scope.buildUndefinedNode()));
|
---|
75 | } else {
|
---|
76 | let nodeInit;
|
---|
77 | if ((this.kind === "const" || this.kind === "using") && init === null) {
|
---|
78 | nodeInit = this.scope.buildUndefinedNode();
|
---|
79 | } else {
|
---|
80 | nodeInit = core.types.cloneNode(init);
|
---|
81 | }
|
---|
82 | node = core.types.variableDeclaration(this.kind, [core.types.variableDeclarator(id, nodeInit)]);
|
---|
83 | }
|
---|
84 | node._blockHoist = this.blockHoist;
|
---|
85 | return node;
|
---|
86 | }
|
---|
87 | buildVariableDeclaration(id, init) {
|
---|
88 | const declar = core.types.variableDeclaration("var", [core.types.variableDeclarator(core.types.cloneNode(id), core.types.cloneNode(init))]);
|
---|
89 | declar._blockHoist = this.blockHoist;
|
---|
90 | return declar;
|
---|
91 | }
|
---|
92 | push(id, _init) {
|
---|
93 | const init = core.types.cloneNode(_init);
|
---|
94 | if (core.types.isObjectPattern(id)) {
|
---|
95 | this.pushObjectPattern(id, init);
|
---|
96 | } else if (core.types.isArrayPattern(id)) {
|
---|
97 | this.pushArrayPattern(id, init);
|
---|
98 | } else if (core.types.isAssignmentPattern(id)) {
|
---|
99 | this.pushAssignmentPattern(id, init);
|
---|
100 | } else {
|
---|
101 | this.nodes.push(this.buildVariableAssignment(id, init));
|
---|
102 | }
|
---|
103 | }
|
---|
104 | toArray(node, count) {
|
---|
105 | if (this.iterableIsArray || core.types.isIdentifier(node) && this.arrayRefSet.has(node.name)) {
|
---|
106 | return node;
|
---|
107 | } else {
|
---|
108 | const {
|
---|
109 | scope,
|
---|
110 | arrayLikeIsIterable
|
---|
111 | } = this;
|
---|
112 | if (core.types.isIdentifier(node)) {
|
---|
113 | const binding = scope.getBinding(node.name);
|
---|
114 | if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
---|
115 | return node;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | if (core.types.isArrayExpression(node)) {
|
---|
119 | return node;
|
---|
120 | }
|
---|
121 | if (core.types.isIdentifier(node, {
|
---|
122 | name: "arguments"
|
---|
123 | })) {
|
---|
124 | return core.template.expression.ast`
|
---|
125 | Array.prototype.slice.call(${node})
|
---|
126 | `;
|
---|
127 | }
|
---|
128 | let helperName;
|
---|
129 | const args = [node];
|
---|
130 | if (typeof count === "number") {
|
---|
131 | args.push(core.types.numericLiteral(count));
|
---|
132 | helperName = "slicedToArray";
|
---|
133 | } else {
|
---|
134 | helperName = "toArray";
|
---|
135 | }
|
---|
136 | if (arrayLikeIsIterable) {
|
---|
137 | args.unshift(scope.path.hub.addHelper(helperName));
|
---|
138 | helperName = "maybeArrayLike";
|
---|
139 | }
|
---|
140 | return core.types.callExpression(scope.path.hub.addHelper(helperName), args);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | pushAssignmentPattern({
|
---|
144 | left,
|
---|
145 | right
|
---|
146 | }, valueRef) {
|
---|
147 | if (isPureVoid(valueRef)) {
|
---|
148 | this.push(left, right);
|
---|
149 | return;
|
---|
150 | }
|
---|
151 | const tempId = this.scope.generateUidIdentifierBasedOnNode(valueRef);
|
---|
152 | this.nodes.push(this.buildVariableDeclaration(tempId, valueRef));
|
---|
153 | const tempConditional = core.types.conditionalExpression(core.types.binaryExpression("===", core.types.cloneNode(tempId), this.scope.buildUndefinedNode()), right, core.types.cloneNode(tempId));
|
---|
154 | if (core.types.isPattern(left)) {
|
---|
155 | let patternId;
|
---|
156 | let node;
|
---|
157 | if (this.kind === "const" || this.kind === "let" || this.kind === "using") {
|
---|
158 | patternId = this.scope.generateUidIdentifier(tempId.name);
|
---|
159 | node = this.buildVariableDeclaration(patternId, tempConditional);
|
---|
160 | } else {
|
---|
161 | patternId = tempId;
|
---|
162 | node = core.types.expressionStatement(core.types.assignmentExpression("=", core.types.cloneNode(tempId), tempConditional));
|
---|
163 | }
|
---|
164 | this.nodes.push(node);
|
---|
165 | this.push(left, patternId);
|
---|
166 | } else {
|
---|
167 | this.nodes.push(this.buildVariableAssignment(left, tempConditional));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) {
|
---|
171 | const value = buildObjectExcludingKeys(pattern.properties.slice(0, spreadPropIndex), objRef, this.scope, name => this.addHelper(name), this.objectRestNoSymbols, this.useBuiltIns);
|
---|
172 | this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
---|
173 | }
|
---|
174 | pushObjectProperty(prop, propRef) {
|
---|
175 | if (core.types.isLiteral(prop.key)) prop.computed = true;
|
---|
176 | const pattern = prop.value;
|
---|
177 | const objRef = core.types.memberExpression(core.types.cloneNode(propRef), prop.key, prop.computed);
|
---|
178 | if (core.types.isPattern(pattern)) {
|
---|
179 | this.push(pattern, objRef);
|
---|
180 | } else {
|
---|
181 | this.nodes.push(this.buildVariableAssignment(pattern, objRef));
|
---|
182 | }
|
---|
183 | }
|
---|
184 | pushObjectPattern(pattern, objRef) {
|
---|
185 | if (!pattern.properties.length) {
|
---|
186 | this.nodes.push(core.types.expressionStatement(core.types.callExpression(this.addHelper("objectDestructuringEmpty"), isPureVoid(objRef) ? [] : [objRef])));
|
---|
187 | return;
|
---|
188 | }
|
---|
189 | if (pattern.properties.length > 1 && !this.scope.isStatic(objRef)) {
|
---|
190 | const temp = this.scope.generateUidIdentifierBasedOnNode(objRef);
|
---|
191 | this.nodes.push(this.buildVariableDeclaration(temp, objRef));
|
---|
192 | objRef = temp;
|
---|
193 | }
|
---|
194 | if (hasObjectRest(pattern)) {
|
---|
195 | let copiedPattern;
|
---|
196 | for (let i = 0; i < pattern.properties.length; i++) {
|
---|
197 | const prop = pattern.properties[i];
|
---|
198 | if (core.types.isRestElement(prop)) {
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | const key = prop.key;
|
---|
202 | if (prop.computed && !this.scope.isPure(key)) {
|
---|
203 | const name = this.scope.generateUidIdentifierBasedOnNode(key);
|
---|
204 | this.nodes.push(this.buildVariableDeclaration(name, key));
|
---|
205 | if (!copiedPattern) {
|
---|
206 | copiedPattern = pattern = Object.assign({}, pattern, {
|
---|
207 | properties: pattern.properties.slice()
|
---|
208 | });
|
---|
209 | }
|
---|
210 | copiedPattern.properties[i] = Object.assign({}, prop, {
|
---|
211 | key: name
|
---|
212 | });
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | for (let i = 0; i < pattern.properties.length; i++) {
|
---|
217 | const prop = pattern.properties[i];
|
---|
218 | if (core.types.isRestElement(prop)) {
|
---|
219 | this.pushObjectRest(pattern, objRef, prop, i);
|
---|
220 | } else {
|
---|
221 | this.pushObjectProperty(prop, objRef);
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | canUnpackArrayPattern(pattern, arr) {
|
---|
226 | if (!core.types.isArrayExpression(arr)) return false;
|
---|
227 | if (pattern.elements.length > arr.elements.length) return;
|
---|
228 | if (pattern.elements.length < arr.elements.length && !hasArrayRest(pattern)) {
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 | for (const elem of pattern.elements) {
|
---|
232 | if (!elem) return false;
|
---|
233 | if (core.types.isMemberExpression(elem)) return false;
|
---|
234 | }
|
---|
235 | for (const elem of arr.elements) {
|
---|
236 | if (core.types.isSpreadElement(elem)) return false;
|
---|
237 | if (core.types.isCallExpression(elem)) return false;
|
---|
238 | if (core.types.isMemberExpression(elem)) return false;
|
---|
239 | }
|
---|
240 | const bindings = core.types.getBindingIdentifiers(pattern);
|
---|
241 | const state = {
|
---|
242 | deopt: false,
|
---|
243 | bindings
|
---|
244 | };
|
---|
245 | try {
|
---|
246 | core.types.traverse(arr, arrayUnpackVisitor, state);
|
---|
247 | } catch (e) {
|
---|
248 | if (e !== STOP_TRAVERSAL) throw e;
|
---|
249 | }
|
---|
250 | return !state.deopt;
|
---|
251 | }
|
---|
252 | pushUnpackedArrayPattern(pattern, arr) {
|
---|
253 | const holeToUndefined = el => el != null ? el : this.scope.buildUndefinedNode();
|
---|
254 | for (let i = 0; i < pattern.elements.length; i++) {
|
---|
255 | const elem = pattern.elements[i];
|
---|
256 | if (core.types.isRestElement(elem)) {
|
---|
257 | this.push(elem.argument, core.types.arrayExpression(arr.elements.slice(i).map(holeToUndefined)));
|
---|
258 | } else {
|
---|
259 | this.push(elem, holeToUndefined(arr.elements[i]));
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | pushArrayPattern(pattern, arrayRef) {
|
---|
264 | if (arrayRef === null) {
|
---|
265 | this.nodes.push(core.types.expressionStatement(core.types.callExpression(this.addHelper("objectDestructuringEmpty"), [])));
|
---|
266 | return;
|
---|
267 | }
|
---|
268 | if (!pattern.elements) return;
|
---|
269 | if (this.canUnpackArrayPattern(pattern, arrayRef)) {
|
---|
270 | this.pushUnpackedArrayPattern(pattern, arrayRef);
|
---|
271 | return;
|
---|
272 | }
|
---|
273 | const count = !hasArrayRest(pattern) && pattern.elements.length;
|
---|
274 | const toArray = this.toArray(arrayRef, count);
|
---|
275 | if (core.types.isIdentifier(toArray)) {
|
---|
276 | arrayRef = toArray;
|
---|
277 | } else {
|
---|
278 | arrayRef = this.scope.generateUidIdentifierBasedOnNode(arrayRef);
|
---|
279 | this.arrayRefSet.add(arrayRef.name);
|
---|
280 | this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray));
|
---|
281 | }
|
---|
282 | for (let i = 0; i < pattern.elements.length; i++) {
|
---|
283 | const elem = pattern.elements[i];
|
---|
284 | if (!elem) continue;
|
---|
285 | let elemRef;
|
---|
286 | if (core.types.isRestElement(elem)) {
|
---|
287 | elemRef = this.toArray(arrayRef);
|
---|
288 | elemRef = core.types.callExpression(core.types.memberExpression(elemRef, core.types.identifier("slice")), [core.types.numericLiteral(i)]);
|
---|
289 | this.push(elem.argument, elemRef);
|
---|
290 | } else {
|
---|
291 | elemRef = core.types.memberExpression(arrayRef, core.types.numericLiteral(i), true);
|
---|
292 | this.push(elem, elemRef);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 | init(pattern, ref) {
|
---|
297 | if (!core.types.isArrayExpression(ref) && !core.types.isMemberExpression(ref)) {
|
---|
298 | const memo = this.scope.maybeGenerateMemoised(ref, true);
|
---|
299 | if (memo) {
|
---|
300 | this.nodes.push(this.buildVariableDeclaration(memo, core.types.cloneNode(ref)));
|
---|
301 | ref = memo;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | this.push(pattern, ref);
|
---|
305 | return this.nodes;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | function buildObjectExcludingKeys(excludedKeys, objRef, scope, addHelper, objectRestNoSymbols, useBuiltIns) {
|
---|
309 | const keys = [];
|
---|
310 | let allLiteral = true;
|
---|
311 | let hasTemplateLiteral = false;
|
---|
312 | for (let i = 0; i < excludedKeys.length; i++) {
|
---|
313 | const prop = excludedKeys[i];
|
---|
314 | const key = prop.key;
|
---|
315 | if (core.types.isIdentifier(key) && !prop.computed) {
|
---|
316 | keys.push(core.types.stringLiteral(key.name));
|
---|
317 | } else if (core.types.isTemplateLiteral(key)) {
|
---|
318 | keys.push(core.types.cloneNode(key));
|
---|
319 | hasTemplateLiteral = true;
|
---|
320 | } else if (core.types.isLiteral(key)) {
|
---|
321 | keys.push(core.types.stringLiteral(String(key.value)));
|
---|
322 | } else if (core.types.isPrivateName(key)) ; else {
|
---|
323 | keys.push(core.types.cloneNode(key));
|
---|
324 | allLiteral = false;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | let value;
|
---|
328 | if (keys.length === 0) {
|
---|
329 | const extendsHelper = useBuiltIns ? core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")) : addHelper("extends");
|
---|
330 | value = core.types.callExpression(extendsHelper, [core.types.objectExpression([]), core.types.sequenceExpression([core.types.callExpression(addHelper("objectDestructuringEmpty"), [core.types.cloneNode(objRef)]), core.types.cloneNode(objRef)])]);
|
---|
331 | } else {
|
---|
332 | let keyExpression = core.types.arrayExpression(keys);
|
---|
333 | if (!allLiteral) {
|
---|
334 | keyExpression = core.types.callExpression(core.types.memberExpression(keyExpression, core.types.identifier("map")), [addHelper("toPropertyKey")]);
|
---|
335 | } else if (!hasTemplateLiteral && !core.types.isProgram(scope.block)) {
|
---|
336 | const programScope = scope.getProgramParent();
|
---|
337 | const id = programScope.generateUidIdentifier("excluded");
|
---|
338 | programScope.push({
|
---|
339 | id,
|
---|
340 | init: keyExpression,
|
---|
341 | kind: "const"
|
---|
342 | });
|
---|
343 | keyExpression = core.types.cloneNode(id);
|
---|
344 | }
|
---|
345 | value = core.types.callExpression(addHelper(`objectWithoutProperties${objectRestNoSymbols ? "Loose" : ""}`), [core.types.cloneNode(objRef), keyExpression]);
|
---|
346 | }
|
---|
347 | return value;
|
---|
348 | }
|
---|
349 | function convertVariableDeclaration(path, addHelper, arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns) {
|
---|
350 | const {
|
---|
351 | node,
|
---|
352 | scope
|
---|
353 | } = path;
|
---|
354 | const nodeKind = node.kind;
|
---|
355 | const nodeLoc = node.loc;
|
---|
356 | const nodes = [];
|
---|
357 | for (let i = 0; i < node.declarations.length; i++) {
|
---|
358 | const declar = node.declarations[i];
|
---|
359 | const patternId = declar.init;
|
---|
360 | const pattern = declar.id;
|
---|
361 | const destructuring = new DestructuringTransformer({
|
---|
362 | blockHoist: node._blockHoist,
|
---|
363 | nodes: nodes,
|
---|
364 | scope: scope,
|
---|
365 | kind: node.kind,
|
---|
366 | iterableIsArray,
|
---|
367 | arrayLikeIsIterable,
|
---|
368 | useBuiltIns,
|
---|
369 | objectRestNoSymbols,
|
---|
370 | addHelper
|
---|
371 | });
|
---|
372 | if (core.types.isPattern(pattern)) {
|
---|
373 | destructuring.init(pattern, patternId);
|
---|
374 | if (+i !== node.declarations.length - 1) {
|
---|
375 | core.types.inherits(nodes[nodes.length - 1], declar);
|
---|
376 | }
|
---|
377 | } else {
|
---|
378 | nodes.push(core.types.inherits(destructuring.buildVariableAssignment(pattern, patternId), declar));
|
---|
379 | }
|
---|
380 | }
|
---|
381 | let tail = null;
|
---|
382 | let nodesOut = [];
|
---|
383 | for (const node of nodes) {
|
---|
384 | if (core.types.isVariableDeclaration(node)) {
|
---|
385 | if (tail !== null) {
|
---|
386 | tail.declarations.push(...node.declarations);
|
---|
387 | continue;
|
---|
388 | } else {
|
---|
389 | node.kind = nodeKind;
|
---|
390 | tail = node;
|
---|
391 | }
|
---|
392 | } else {
|
---|
393 | tail = null;
|
---|
394 | }
|
---|
395 | if (!node.loc) {
|
---|
396 | node.loc = nodeLoc;
|
---|
397 | }
|
---|
398 | nodesOut.push(node);
|
---|
399 | }
|
---|
400 | if (nodesOut.length === 2 && core.types.isVariableDeclaration(nodesOut[0]) && core.types.isExpressionStatement(nodesOut[1]) && core.types.isCallExpression(nodesOut[1].expression) && nodesOut[0].declarations.length === 1) {
|
---|
401 | const expr = nodesOut[1].expression;
|
---|
402 | expr.arguments = [nodesOut[0].declarations[0].init];
|
---|
403 | nodesOut = [expr];
|
---|
404 | } else {
|
---|
405 | if (core.types.isForStatement(path.parent, {
|
---|
406 | init: node
|
---|
407 | }) && !nodesOut.some(v => core.types.isVariableDeclaration(v))) {
|
---|
408 | for (let i = 0; i < nodesOut.length; i++) {
|
---|
409 | const node = nodesOut[i];
|
---|
410 | if (core.types.isExpressionStatement(node)) {
|
---|
411 | nodesOut[i] = node.expression;
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 | if (nodesOut.length === 1) {
|
---|
417 | path.replaceWith(nodesOut[0]);
|
---|
418 | } else {
|
---|
419 | path.replaceWithMultiple(nodesOut);
|
---|
420 | }
|
---|
421 | scope.crawl();
|
---|
422 | }
|
---|
423 | function convertAssignmentExpression(path, addHelper, arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns) {
|
---|
424 | const {
|
---|
425 | node,
|
---|
426 | scope,
|
---|
427 | parentPath
|
---|
428 | } = path;
|
---|
429 | const nodes = [];
|
---|
430 | const destructuring = new DestructuringTransformer({
|
---|
431 | operator: node.operator,
|
---|
432 | scope: scope,
|
---|
433 | nodes: nodes,
|
---|
434 | arrayLikeIsIterable,
|
---|
435 | iterableIsArray,
|
---|
436 | objectRestNoSymbols,
|
---|
437 | useBuiltIns,
|
---|
438 | addHelper
|
---|
439 | });
|
---|
440 | let ref;
|
---|
441 | if (!parentPath.isExpressionStatement() && !parentPath.isSequenceExpression() || path.isCompletionRecord()) {
|
---|
442 | ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");
|
---|
443 | nodes.push(core.types.variableDeclaration("var", [core.types.variableDeclarator(ref, node.right)]));
|
---|
444 | if (core.types.isArrayExpression(node.right)) {
|
---|
445 | destructuring.arrayRefSet.add(ref.name);
|
---|
446 | }
|
---|
447 | }
|
---|
448 | destructuring.init(node.left, ref || node.right);
|
---|
449 | if (ref) {
|
---|
450 | if (parentPath.isArrowFunctionExpression()) {
|
---|
451 | path.replaceWith(core.types.blockStatement([]));
|
---|
452 | nodes.push(core.types.returnStatement(core.types.cloneNode(ref)));
|
---|
453 | } else {
|
---|
454 | nodes.push(core.types.expressionStatement(core.types.cloneNode(ref)));
|
---|
455 | }
|
---|
456 | }
|
---|
457 | path.replaceWithMultiple(nodes);
|
---|
458 | scope.crawl();
|
---|
459 | }
|
---|
460 |
|
---|
461 | function variableDeclarationHasPattern(node) {
|
---|
462 | for (const declar of node.declarations) {
|
---|
463 | if (core.types.isPattern(declar.id)) {
|
---|
464 | return true;
|
---|
465 | }
|
---|
466 | }
|
---|
467 | return false;
|
---|
468 | }
|
---|
469 | var index = helperPluginUtils.declare((api, options) => {
|
---|
470 | var _ref, _api$assumption, _ref2, _options$allowArrayLi, _ref3, _api$assumption2;
|
---|
471 | api.assertVersion(7);
|
---|
472 | const {
|
---|
473 | useBuiltIns = false
|
---|
474 | } = options;
|
---|
475 | const iterableIsArray = (_ref = (_api$assumption = api.assumption("iterableIsArray")) != null ? _api$assumption : options.loose) != null ? _ref : false;
|
---|
476 | const arrayLikeIsIterable = (_ref2 = (_options$allowArrayLi = options.allowArrayLike) != null ? _options$allowArrayLi : api.assumption("arrayLikeIsIterable")) != null ? _ref2 : false;
|
---|
477 | const objectRestNoSymbols = (_ref3 = (_api$assumption2 = api.assumption("objectRestNoSymbols")) != null ? _api$assumption2 : options.loose) != null ? _ref3 : false;
|
---|
478 | return {
|
---|
479 | name: "transform-destructuring",
|
---|
480 | visitor: {
|
---|
481 | ExportNamedDeclaration(path) {
|
---|
482 | const declaration = path.get("declaration");
|
---|
483 | if (!declaration.isVariableDeclaration()) return;
|
---|
484 | if (!variableDeclarationHasPattern(declaration.node)) return;
|
---|
485 | const specifiers = [];
|
---|
486 | for (const name of Object.keys(path.getOuterBindingIdentifiers())) {
|
---|
487 | specifiers.push(core.types.exportSpecifier(core.types.identifier(name), core.types.identifier(name)));
|
---|
488 | }
|
---|
489 | path.replaceWith(declaration.node);
|
---|
490 | path.insertAfter(core.types.exportNamedDeclaration(null, specifiers));
|
---|
491 | path.scope.crawl();
|
---|
492 | },
|
---|
493 | ForXStatement(path) {
|
---|
494 | const {
|
---|
495 | node,
|
---|
496 | scope
|
---|
497 | } = path;
|
---|
498 | const left = node.left;
|
---|
499 | if (core.types.isPattern(left)) {
|
---|
500 | const temp = scope.generateUidIdentifier("ref");
|
---|
501 | node.left = core.types.variableDeclaration("var", [core.types.variableDeclarator(temp)]);
|
---|
502 | path.ensureBlock();
|
---|
503 | const statementBody = path.node.body.body;
|
---|
504 | const nodes = [];
|
---|
505 | if (statementBody.length === 0 && path.isCompletionRecord()) {
|
---|
506 | nodes.unshift(core.types.expressionStatement(scope.buildUndefinedNode()));
|
---|
507 | }
|
---|
508 | nodes.unshift(core.types.expressionStatement(core.types.assignmentExpression("=", left, core.types.cloneNode(temp))));
|
---|
509 | unshiftForXStatementBody(path, nodes);
|
---|
510 | scope.crawl();
|
---|
511 | return;
|
---|
512 | }
|
---|
513 | if (!core.types.isVariableDeclaration(left)) return;
|
---|
514 | const pattern = left.declarations[0].id;
|
---|
515 | if (!core.types.isPattern(pattern)) return;
|
---|
516 | const key = scope.generateUidIdentifier("ref");
|
---|
517 | node.left = core.types.variableDeclaration(left.kind, [core.types.variableDeclarator(key, null)]);
|
---|
518 | const nodes = [];
|
---|
519 | const destructuring = new DestructuringTransformer({
|
---|
520 | kind: left.kind,
|
---|
521 | scope: scope,
|
---|
522 | nodes: nodes,
|
---|
523 | arrayLikeIsIterable,
|
---|
524 | iterableIsArray,
|
---|
525 | objectRestNoSymbols,
|
---|
526 | useBuiltIns,
|
---|
527 | addHelper: name => this.addHelper(name)
|
---|
528 | });
|
---|
529 | destructuring.init(pattern, key);
|
---|
530 | unshiftForXStatementBody(path, nodes);
|
---|
531 | scope.crawl();
|
---|
532 | },
|
---|
533 | CatchClause({
|
---|
534 | node,
|
---|
535 | scope
|
---|
536 | }) {
|
---|
537 | const pattern = node.param;
|
---|
538 | if (!core.types.isPattern(pattern)) return;
|
---|
539 | const ref = scope.generateUidIdentifier("ref");
|
---|
540 | node.param = ref;
|
---|
541 | const nodes = [];
|
---|
542 | const destructuring = new DestructuringTransformer({
|
---|
543 | kind: "let",
|
---|
544 | scope: scope,
|
---|
545 | nodes: nodes,
|
---|
546 | arrayLikeIsIterable,
|
---|
547 | iterableIsArray,
|
---|
548 | objectRestNoSymbols,
|
---|
549 | useBuiltIns,
|
---|
550 | addHelper: name => this.addHelper(name)
|
---|
551 | });
|
---|
552 | destructuring.init(pattern, ref);
|
---|
553 | node.body.body = [...nodes, ...node.body.body];
|
---|
554 | scope.crawl();
|
---|
555 | },
|
---|
556 | AssignmentExpression(path, state) {
|
---|
557 | if (!core.types.isPattern(path.node.left)) return;
|
---|
558 | convertAssignmentExpression(path, name => state.addHelper(name), arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns);
|
---|
559 | },
|
---|
560 | VariableDeclaration(path, state) {
|
---|
561 | const {
|
---|
562 | node,
|
---|
563 | parent
|
---|
564 | } = path;
|
---|
565 | if (core.types.isForXStatement(parent)) return;
|
---|
566 | if (!parent || !path.container) return;
|
---|
567 | if (!variableDeclarationHasPattern(node)) return;
|
---|
568 | convertVariableDeclaration(path, name => state.addHelper(name), arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns);
|
---|
569 | }
|
---|
570 | }
|
---|
571 | };
|
---|
572 | });
|
---|
573 |
|
---|
574 | exports.buildObjectExcludingKeys = buildObjectExcludingKeys;
|
---|
575 | exports.default = index;
|
---|
576 | exports.unshiftForXStatementBody = unshiftForXStatementBody;
|
---|
577 | //# sourceMappingURL=index.js.map
|
---|