| 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" && 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") {
|
|---|
| 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 | if (core.types.isRestElement(elem)) {
|
|---|
| 286 | this.push(elem.argument, core.types.callExpression(core.types.memberExpression(core.types.callExpression(this.scope.path.hub.addHelper("arrayLikeToArray"), [arrayRef]), core.types.identifier("slice")), [core.types.numericLiteral(i)]));
|
|---|
| 287 | } else {
|
|---|
| 288 | this.push(elem, core.types.memberExpression(arrayRef, core.types.numericLiteral(i), true));
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | init(pattern, ref) {
|
|---|
| 293 | if (!core.types.isArrayExpression(ref) && !core.types.isMemberExpression(ref)) {
|
|---|
| 294 | const memo = this.scope.maybeGenerateMemoised(ref, true);
|
|---|
| 295 | if (memo) {
|
|---|
| 296 | this.nodes.push(this.buildVariableDeclaration(memo, core.types.cloneNode(ref)));
|
|---|
| 297 | ref = memo;
|
|---|
| 298 | }
|
|---|
| 299 | }
|
|---|
| 300 | this.push(pattern, ref);
|
|---|
| 301 | return this.nodes;
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 | function buildObjectExcludingKeys(excludedKeys, objRef, scope, addHelper, objectRestNoSymbols, useBuiltIns) {
|
|---|
| 305 | const keys = [];
|
|---|
| 306 | let allLiteral = true;
|
|---|
| 307 | let hasTemplateLiteral = false;
|
|---|
| 308 | for (let i = 0; i < excludedKeys.length; i++) {
|
|---|
| 309 | const prop = excludedKeys[i];
|
|---|
| 310 | const key = prop.key;
|
|---|
| 311 | if (core.types.isIdentifier(key) && !prop.computed) {
|
|---|
| 312 | keys.push(core.types.stringLiteral(key.name));
|
|---|
| 313 | } else if (core.types.isTemplateLiteral(key)) {
|
|---|
| 314 | keys.push(core.types.cloneNode(key));
|
|---|
| 315 | hasTemplateLiteral = true;
|
|---|
| 316 | } else if (core.types.isLiteral(key)) {
|
|---|
| 317 | keys.push(core.types.stringLiteral(String(key.value)));
|
|---|
| 318 | } else if (core.types.isPrivateName(key)) ; else {
|
|---|
| 319 | keys.push(core.types.cloneNode(key));
|
|---|
| 320 | allLiteral = false;
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 | let value;
|
|---|
| 324 | if (keys.length === 0) {
|
|---|
| 325 | const extendsHelper = useBuiltIns ? core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")) : addHelper("extends");
|
|---|
| 326 | value = core.types.callExpression(extendsHelper, [core.types.objectExpression([]), core.types.sequenceExpression([core.types.callExpression(addHelper("objectDestructuringEmpty"), [core.types.cloneNode(objRef)]), core.types.cloneNode(objRef)])]);
|
|---|
| 327 | } else {
|
|---|
| 328 | let keyExpression = core.types.arrayExpression(keys);
|
|---|
| 329 | if (!allLiteral) {
|
|---|
| 330 | keyExpression = core.types.callExpression(core.types.memberExpression(keyExpression, core.types.identifier("map")), [addHelper("toPropertyKey")]);
|
|---|
| 331 | } else if (!hasTemplateLiteral && !core.types.isProgram(scope.block)) {
|
|---|
| 332 | const programScope = scope.getProgramParent();
|
|---|
| 333 | const id = programScope.generateUidIdentifier("excluded");
|
|---|
| 334 | programScope.push({
|
|---|
| 335 | id,
|
|---|
| 336 | init: keyExpression,
|
|---|
| 337 | kind: "const"
|
|---|
| 338 | });
|
|---|
| 339 | keyExpression = core.types.cloneNode(id);
|
|---|
| 340 | }
|
|---|
| 341 | value = core.types.callExpression(addHelper(`objectWithoutProperties${objectRestNoSymbols ? "Loose" : ""}`), [core.types.cloneNode(objRef), keyExpression]);
|
|---|
| 342 | }
|
|---|
| 343 | return value;
|
|---|
| 344 | }
|
|---|
| 345 | function convertVariableDeclaration(path, addHelper, arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns) {
|
|---|
| 346 | const {
|
|---|
| 347 | node,
|
|---|
| 348 | scope
|
|---|
| 349 | } = path;
|
|---|
| 350 | const nodeKind = node.kind;
|
|---|
| 351 | const nodeLoc = node.loc;
|
|---|
| 352 | const nodes = [];
|
|---|
| 353 | for (let i = 0; i < node.declarations.length; i++) {
|
|---|
| 354 | const declar = node.declarations[i];
|
|---|
| 355 | const patternId = declar.init;
|
|---|
| 356 | const pattern = declar.id;
|
|---|
| 357 | const destructuring = new DestructuringTransformer({
|
|---|
| 358 | blockHoist: node._blockHoist,
|
|---|
| 359 | nodes: nodes,
|
|---|
| 360 | scope: scope,
|
|---|
| 361 | kind: node.kind,
|
|---|
| 362 | iterableIsArray,
|
|---|
| 363 | arrayLikeIsIterable,
|
|---|
| 364 | useBuiltIns,
|
|---|
| 365 | objectRestNoSymbols,
|
|---|
| 366 | addHelper
|
|---|
| 367 | });
|
|---|
| 368 | if (core.types.isPattern(pattern)) {
|
|---|
| 369 | destructuring.init(pattern, patternId);
|
|---|
| 370 | if (+i !== node.declarations.length - 1) {
|
|---|
| 371 | core.types.inherits(nodes[nodes.length - 1], declar);
|
|---|
| 372 | }
|
|---|
| 373 | } else {
|
|---|
| 374 | nodes.push(core.types.inherits(destructuring.buildVariableAssignment(pattern, patternId), declar));
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 | let tail = null;
|
|---|
| 378 | let nodesOut = [];
|
|---|
| 379 | for (const node of nodes) {
|
|---|
| 380 | if (core.types.isVariableDeclaration(node)) {
|
|---|
| 381 | if (tail !== null) {
|
|---|
| 382 | tail.declarations.push(...node.declarations);
|
|---|
| 383 | continue;
|
|---|
| 384 | } else {
|
|---|
| 385 | node.kind = nodeKind;
|
|---|
| 386 | tail = node;
|
|---|
| 387 | }
|
|---|
| 388 | } else {
|
|---|
| 389 | tail = null;
|
|---|
| 390 | }
|
|---|
| 391 | if (!node.loc) {
|
|---|
| 392 | node.loc = nodeLoc;
|
|---|
| 393 | }
|
|---|
| 394 | nodesOut.push(node);
|
|---|
| 395 | }
|
|---|
| 396 | 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) {
|
|---|
| 397 | const expr = nodesOut[1].expression;
|
|---|
| 398 | expr.arguments = [nodesOut[0].declarations[0].init];
|
|---|
| 399 | nodesOut = [expr];
|
|---|
| 400 | } else {
|
|---|
| 401 | if (core.types.isForStatement(path.parent, {
|
|---|
| 402 | init: node
|
|---|
| 403 | }) && !nodesOut.some(v => core.types.isVariableDeclaration(v))) {
|
|---|
| 404 | for (let i = 0; i < nodesOut.length; i++) {
|
|---|
| 405 | const node = nodesOut[i];
|
|---|
| 406 | if (core.types.isExpressionStatement(node)) {
|
|---|
| 407 | nodesOut[i] = node.expression;
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 | if (nodesOut.length === 1) {
|
|---|
| 413 | path.replaceWith(nodesOut[0]);
|
|---|
| 414 | } else {
|
|---|
| 415 | path.replaceWithMultiple(nodesOut);
|
|---|
| 416 | }
|
|---|
| 417 | scope.crawl();
|
|---|
| 418 | }
|
|---|
| 419 | function convertAssignmentExpression(path, addHelper, arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns) {
|
|---|
| 420 | const {
|
|---|
| 421 | node,
|
|---|
| 422 | scope,
|
|---|
| 423 | parentPath
|
|---|
| 424 | } = path;
|
|---|
| 425 | const nodes = [];
|
|---|
| 426 | const destructuring = new DestructuringTransformer({
|
|---|
| 427 | operator: node.operator,
|
|---|
| 428 | scope: scope,
|
|---|
| 429 | nodes: nodes,
|
|---|
| 430 | arrayLikeIsIterable,
|
|---|
| 431 | iterableIsArray,
|
|---|
| 432 | objectRestNoSymbols,
|
|---|
| 433 | useBuiltIns,
|
|---|
| 434 | addHelper
|
|---|
| 435 | });
|
|---|
| 436 | let ref;
|
|---|
| 437 | if (!parentPath.isExpressionStatement() && !parentPath.isSequenceExpression() || path.isCompletionRecord()) {
|
|---|
| 438 | ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");
|
|---|
| 439 | nodes.push(core.types.variableDeclaration("var", [core.types.variableDeclarator(ref, node.right)]));
|
|---|
| 440 | if (core.types.isArrayExpression(node.right)) {
|
|---|
| 441 | destructuring.arrayRefSet.add(ref.name);
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 | destructuring.init(node.left, ref || node.right);
|
|---|
| 445 | if (ref) {
|
|---|
| 446 | if (parentPath.isArrowFunctionExpression()) {
|
|---|
| 447 | path.replaceWith(core.types.blockStatement([]));
|
|---|
| 448 | nodes.push(core.types.returnStatement(core.types.cloneNode(ref)));
|
|---|
| 449 | } else {
|
|---|
| 450 | nodes.push(core.types.expressionStatement(core.types.cloneNode(ref)));
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 | path.replaceWithMultiple(nodes);
|
|---|
| 454 | scope.crawl();
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | function variableDeclarationHasDestructuringPattern(node) {
|
|---|
| 458 | for (const declar of node.declarations) {
|
|---|
| 459 | if (core.types.isPattern(declar.id) && declar.id.type !== "VoidPattern") {
|
|---|
| 460 | return true;
|
|---|
| 461 | }
|
|---|
| 462 | }
|
|---|
| 463 | return false;
|
|---|
| 464 | }
|
|---|
| 465 | var index = helperPluginUtils.declare((api, options) => {
|
|---|
| 466 | var _ref, _api$assumption, _ref2, _options$allowArrayLi, _ref3, _api$assumption2;
|
|---|
| 467 | api.assertVersion(7);
|
|---|
| 468 | const {
|
|---|
| 469 | useBuiltIns = false
|
|---|
| 470 | } = options;
|
|---|
| 471 | const iterableIsArray = (_ref = (_api$assumption = api.assumption("iterableIsArray")) != null ? _api$assumption : options.loose) != null ? _ref : false;
|
|---|
| 472 | const arrayLikeIsIterable = (_ref2 = (_options$allowArrayLi = options.allowArrayLike) != null ? _options$allowArrayLi : api.assumption("arrayLikeIsIterable")) != null ? _ref2 : false;
|
|---|
| 473 | const objectRestNoSymbols = (_ref3 = (_api$assumption2 = api.assumption("objectRestNoSymbols")) != null ? _api$assumption2 : options.loose) != null ? _ref3 : false;
|
|---|
| 474 | return {
|
|---|
| 475 | name: "transform-destructuring",
|
|---|
| 476 | visitor: {
|
|---|
| 477 | ExportNamedDeclaration(path) {
|
|---|
| 478 | const declaration = path.get("declaration");
|
|---|
| 479 | if (!declaration.isVariableDeclaration()) return;
|
|---|
| 480 | if (!variableDeclarationHasDestructuringPattern(declaration.node)) return;
|
|---|
| 481 | {
|
|---|
| 482 | var _path$splitExportDecl;
|
|---|
| 483 | (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
|
|---|
| 484 | }
|
|---|
| 485 | path.splitExportDeclaration();
|
|---|
| 486 | },
|
|---|
| 487 | ForXStatement(path) {
|
|---|
| 488 | const {
|
|---|
| 489 | node,
|
|---|
| 490 | scope
|
|---|
| 491 | } = path;
|
|---|
| 492 | const left = node.left;
|
|---|
| 493 | if (core.types.isPattern(left)) {
|
|---|
| 494 | const temp = scope.generateUidIdentifier("ref");
|
|---|
| 495 | node.left = core.types.variableDeclaration("var", [core.types.variableDeclarator(temp)]);
|
|---|
| 496 | path.ensureBlock();
|
|---|
| 497 | const statementBody = path.node.body.body;
|
|---|
| 498 | const nodes = [];
|
|---|
| 499 | if (statementBody.length === 0 && path.isCompletionRecord()) {
|
|---|
| 500 | nodes.unshift(core.types.expressionStatement(scope.buildUndefinedNode()));
|
|---|
| 501 | }
|
|---|
| 502 | nodes.unshift(core.types.expressionStatement(core.types.assignmentExpression("=", left, core.types.cloneNode(temp))));
|
|---|
| 503 | unshiftForXStatementBody(path, nodes);
|
|---|
| 504 | scope.crawl();
|
|---|
| 505 | return;
|
|---|
| 506 | }
|
|---|
| 507 | if (!core.types.isVariableDeclaration(left)) return;
|
|---|
| 508 | const pattern = left.declarations[0].id;
|
|---|
| 509 | if (!core.types.isPattern(pattern) || pattern.type === "VoidPattern") return;
|
|---|
| 510 | const key = scope.generateUidIdentifier("ref");
|
|---|
| 511 | node.left = core.types.variableDeclaration(left.kind, [core.types.variableDeclarator(key, null)]);
|
|---|
| 512 | const nodes = [];
|
|---|
| 513 | const destructuring = new DestructuringTransformer({
|
|---|
| 514 | kind: left.kind,
|
|---|
| 515 | scope: scope,
|
|---|
| 516 | nodes: nodes,
|
|---|
| 517 | arrayLikeIsIterable,
|
|---|
| 518 | iterableIsArray,
|
|---|
| 519 | objectRestNoSymbols,
|
|---|
| 520 | useBuiltIns,
|
|---|
| 521 | addHelper: name => this.addHelper(name)
|
|---|
| 522 | });
|
|---|
| 523 | destructuring.init(pattern, key);
|
|---|
| 524 | unshiftForXStatementBody(path, nodes);
|
|---|
| 525 | scope.crawl();
|
|---|
| 526 | },
|
|---|
| 527 | CatchClause({
|
|---|
| 528 | node,
|
|---|
| 529 | scope
|
|---|
| 530 | }) {
|
|---|
| 531 | const pattern = node.param;
|
|---|
| 532 | if (!core.types.isPattern(pattern)) return;
|
|---|
| 533 | const ref = scope.generateUidIdentifier("ref");
|
|---|
| 534 | node.param = ref;
|
|---|
| 535 | const nodes = [];
|
|---|
| 536 | const destructuring = new DestructuringTransformer({
|
|---|
| 537 | kind: "let",
|
|---|
| 538 | scope: scope,
|
|---|
| 539 | nodes: nodes,
|
|---|
| 540 | arrayLikeIsIterable,
|
|---|
| 541 | iterableIsArray,
|
|---|
| 542 | objectRestNoSymbols,
|
|---|
| 543 | useBuiltIns,
|
|---|
| 544 | addHelper: name => this.addHelper(name)
|
|---|
| 545 | });
|
|---|
| 546 | destructuring.init(pattern, ref);
|
|---|
| 547 | node.body.body = [...nodes, ...node.body.body];
|
|---|
| 548 | scope.crawl();
|
|---|
| 549 | },
|
|---|
| 550 | AssignmentExpression(path, state) {
|
|---|
| 551 | if (!core.types.isPattern(path.node.left)) return;
|
|---|
| 552 | convertAssignmentExpression(path, name => state.addHelper(name), arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns);
|
|---|
| 553 | },
|
|---|
| 554 | VariableDeclaration(path, state) {
|
|---|
| 555 | const {
|
|---|
| 556 | node,
|
|---|
| 557 | parent
|
|---|
| 558 | } = path;
|
|---|
| 559 | if (core.types.isForXStatement(parent)) return;
|
|---|
| 560 | if (!parent || !path.container) return;
|
|---|
| 561 | if (!variableDeclarationHasDestructuringPattern(node)) return;
|
|---|
| 562 | convertVariableDeclaration(path, name => state.addHelper(name), arrayLikeIsIterable, iterableIsArray, objectRestNoSymbols, useBuiltIns);
|
|---|
| 563 | }
|
|---|
| 564 | }
|
|---|
| 565 | };
|
|---|
| 566 | });
|
|---|
| 567 |
|
|---|
| 568 | exports.buildObjectExcludingKeys = buildObjectExcludingKeys;
|
|---|
| 569 | exports.default = index;
|
|---|
| 570 | exports.unshiftForXStatementBody = unshiftForXStatementBody;
|
|---|
| 571 | //# sourceMappingURL=index.js.map
|
|---|