@babel/plugin-transform-destructuring
Advanced tools
Comparing version 7.20.0 to 7.20.2
120
lib/index.js
@@ -11,3 +11,2 @@ 'use strict'; | ||
} | ||
function unshiftForXStatementBody(statementPath, newStatements) { | ||
@@ -21,3 +20,2 @@ statementPath.ensureBlock(); | ||
const hasShadowedBlockScopedBindings = Object.keys(bodyScopeBindings).some(name => scope.hasBinding(name)); | ||
if (hasShadowedBlockScopedBindings) { | ||
@@ -37,5 +35,3 @@ node.body = core.types.blockStatement([...newStatements, node.body]); | ||
} | ||
const STOP_TRAVERSAL = {}; | ||
const arrayUnpackVisitor = (node, ancestors, state) => { | ||
@@ -45,3 +41,2 @@ if (!ancestors.length) { | ||
} | ||
if (core.types.isIdentifier(node) && core.types.isReferenced(node, ancestors[ancestors.length - 1].node) && state.bindings[node.name]) { | ||
@@ -52,3 +47,2 @@ state.deopt = true; | ||
}; | ||
class DestructuringTransformer { | ||
@@ -79,7 +73,5 @@ constructor(opts) { | ||
} | ||
getExtendsHelper() { | ||
return this.useBuiltIns ? core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")) : this.addHelper("extends"); | ||
} | ||
buildVariableAssignment(id, init) { | ||
@@ -89,3 +81,2 @@ let op = this.operator; | ||
let node; | ||
if (op) { | ||
@@ -95,3 +86,2 @@ node = core.types.expressionStatement(core.types.assignmentExpression(op, id, core.types.cloneNode(init) || this.scope.buildUndefinedNode())); | ||
let nodeInit; | ||
if ((this.kind === "const" || this.kind === "using") && init === null) { | ||
@@ -102,3 +92,2 @@ nodeInit = this.scope.buildUndefinedNode(); | ||
} | ||
node = core.types.variableDeclaration(this.kind, [core.types.variableDeclarator(id, nodeInit)]); | ||
@@ -110,3 +99,2 @@ } | ||
} | ||
buildVariableDeclaration(id, init) { | ||
@@ -117,6 +105,4 @@ const declar = core.types.variableDeclaration("var", [core.types.variableDeclarator(core.types.cloneNode(id), core.types.cloneNode(init))]); | ||
} | ||
push(id, _init) { | ||
const init = core.types.cloneNode(_init); | ||
if (core.types.isObjectPattern(id)) { | ||
@@ -132,3 +118,2 @@ this.pushObjectPattern(id, init); | ||
} | ||
toArray(node, count) { | ||
@@ -141,3 +126,2 @@ if (this.iterableIsArray || core.types.isIdentifier(node) && this.arrayRefSet.has(node.name)) { | ||
} | ||
pushAssignmentPattern({ | ||
@@ -155,7 +139,5 @@ left, | ||
const tempConditional = core.types.conditionalExpression(core.types.binaryExpression("===", core.types.cloneNode(tempId), this.scope.buildUndefinedNode()), right, core.types.cloneNode(tempId)); | ||
if (core.types.isPattern(left)) { | ||
let patternId; | ||
let node; | ||
if (this.kind === "const" || this.kind === "let" || this.kind === "using") { | ||
@@ -168,3 +150,2 @@ patternId = this.scope.generateUidIdentifier(tempId.name); | ||
} | ||
this.nodes.push(node); | ||
@@ -176,3 +157,2 @@ this.push(left, patternId); | ||
} | ||
pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) { | ||
@@ -182,3 +162,2 @@ const value = buildObjectExcludingKeys(pattern.properties.slice(0, spreadPropIndex), objRef, this.scope, name => this.addHelper(name), this.objectRestNoSymbols, this.useBuiltIns); | ||
} | ||
pushObjectProperty(prop, propRef) { | ||
@@ -188,3 +167,2 @@ if (core.types.isLiteral(prop.key)) prop.computed = true; | ||
const objRef = core.types.memberExpression(core.types.cloneNode(propRef), prop.key, prop.computed); | ||
if (core.types.isPattern(pattern)) { | ||
@@ -196,4 +174,4 @@ this.push(pattern, objRef); | ||
} | ||
pushObjectPattern(pattern, objRef) { | ||
pushObjectPattern(pattern, objRef) { | ||
if (!pattern.properties.length) { | ||
@@ -212,16 +190,12 @@ this.nodes.push(core.types.expressionStatement(core.types.callExpression(this.addHelper("objectDestructuringEmpty"), isPureVoid(objRef) ? [] : [objRef]))); | ||
let copiedPattern; | ||
for (let i = 0; i < pattern.properties.length; i++) { | ||
const prop = pattern.properties[i]; | ||
if (core.types.isRestElement(prop)) { | ||
break; | ||
} | ||
const key = prop.key; | ||
if (prop.computed && !this.scope.isPure(key)) { | ||
const name = this.scope.generateUidIdentifierBasedOnNode(key); | ||
this.nodes.push(this.buildVariableDeclaration(name, key)); | ||
this.nodes.push( | ||
this.buildVariableDeclaration(name, key)); | ||
if (!copiedPattern) { | ||
@@ -232,3 +206,2 @@ copiedPattern = pattern = Object.assign({}, pattern, { | ||
} | ||
copiedPattern.properties[i] = Object.assign({}, prop, { | ||
@@ -240,6 +213,4 @@ key: name | ||
} | ||
for (let i = 0; i < pattern.properties.length; i++) { | ||
const prop = pattern.properties[i]; | ||
if (core.types.isRestElement(prop)) { | ||
@@ -252,19 +223,19 @@ this.pushObjectRest(pattern, objRef, prop, i); | ||
} | ||
canUnpackArrayPattern(pattern, arr) { | ||
if (!core.types.isArrayExpression(arr)) return false; | ||
if (pattern.elements.length > arr.elements.length) return; | ||
if (pattern.elements.length < arr.elements.length && !hasArrayRest(pattern)) { | ||
return false; | ||
} | ||
for (const elem of pattern.elements) { | ||
if (!elem) return false; | ||
if (core.types.isMemberExpression(elem)) return false; | ||
} | ||
for (const elem of arr.elements) { | ||
if (core.types.isSpreadElement(elem)) return false; | ||
if (core.types.isCallExpression(elem)) return false; | ||
if (core.types.isMemberExpression(elem)) return false; | ||
@@ -278,3 +249,2 @@ } | ||
}; | ||
try { | ||
@@ -285,12 +255,8 @@ core.types.traverse(arr, arrayUnpackVisitor, state); | ||
} | ||
return !state.deopt; | ||
} | ||
pushUnpackedArrayPattern(pattern, arr) { | ||
const holeToUndefined = el => el != null ? el : this.scope.buildUndefinedNode(); | ||
for (let i = 0; i < pattern.elements.length; i++) { | ||
const elem = pattern.elements[i]; | ||
if (core.types.isRestElement(elem)) { | ||
@@ -303,3 +269,2 @@ this.push(elem.argument, core.types.arrayExpression(arr.elements.slice(i).map(holeToUndefined))); | ||
} | ||
pushArrayPattern(pattern, arrayRef) { | ||
@@ -310,3 +275,2 @@ if (arrayRef === null) { | ||
} | ||
if (!pattern.elements) return; | ||
@@ -319,4 +283,4 @@ | ||
const count = !hasArrayRest(pattern) && pattern.elements.length; | ||
const toArray = this.toArray(arrayRef, count); | ||
if (core.types.isIdentifier(toArray)) { | ||
@@ -329,11 +293,11 @@ arrayRef = toArray; | ||
} | ||
for (let i = 0; i < pattern.elements.length; i++) { | ||
const elem = pattern.elements[i]; | ||
if (!elem) continue; | ||
let elemRef; | ||
if (core.types.isRestElement(elem)) { | ||
elemRef = this.toArray(arrayRef); | ||
elemRef = core.types.callExpression(core.types.memberExpression(elemRef, core.types.identifier("slice")), [core.types.numericLiteral(i)]); | ||
this.push(elem.argument, elemRef); | ||
@@ -346,7 +310,6 @@ } else { | ||
} | ||
init(pattern, ref) { | ||
init(pattern, ref) { | ||
if (!core.types.isArrayExpression(ref) && !core.types.isMemberExpression(ref)) { | ||
const memo = this.scope.maybeGenerateMemoised(ref, true); | ||
if (memo) { | ||
@@ -357,17 +320,14 @@ this.nodes.push(this.buildVariableDeclaration(memo, core.types.cloneNode(ref))); | ||
} | ||
this.push(pattern, ref); | ||
return this.nodes; | ||
} | ||
} | ||
function buildObjectExcludingKeys(excludedKeys, objRef, scope, addHelper, objectRestNoSymbols, useBuiltIns) { | ||
const keys = []; | ||
let allLiteral = true; | ||
let hasTemplateLiteral = false; | ||
for (let i = 0; i < excludedKeys.length; i++) { | ||
const prop = excludedKeys[i]; | ||
const key = prop.key; | ||
if (core.types.isIdentifier(key) && !prop.computed) { | ||
@@ -385,5 +345,3 @@ keys.push(core.types.stringLiteral(key.name)); | ||
} | ||
let value; | ||
if (keys.length === 0) { | ||
@@ -394,3 +352,2 @@ const extendsHelper = useBuiltIns ? core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")) : addHelper("extends"); | ||
let keyExpression = core.types.arrayExpression(keys); | ||
if (!allLiteral) { | ||
@@ -408,6 +365,4 @@ keyExpression = core.types.callExpression(core.types.memberExpression(keyExpression, core.types.identifier("map")), [addHelper("toPropertyKey")]); | ||
} | ||
value = core.types.callExpression(addHelper(`objectWithoutProperties${objectRestNoSymbols ? "Loose" : ""}`), [core.types.cloneNode(objRef), keyExpression]); | ||
} | ||
return value; | ||
@@ -423,3 +378,2 @@ } | ||
const nodes = []; | ||
for (let i = 0; i < node.declarations.length; i++) { | ||
@@ -440,6 +394,4 @@ const declar = node.declarations[i]; | ||
}); | ||
if (core.types.isPattern(pattern)) { | ||
destructuring.init(pattern, patternId); | ||
if (+i !== node.declarations.length - 1) { | ||
@@ -452,9 +404,4 @@ core.types.inherits(nodes[nodes.length - 1], declar); | ||
} | ||
const inForInit = core.types.isForStatement(path.parent, { | ||
init: node | ||
}); | ||
let tail = null; | ||
const nodesOut = []; | ||
let nodesOut = []; | ||
for (const node of nodes) { | ||
@@ -472,10 +419,23 @@ if (core.types.isVariableDeclaration(node)) { | ||
} | ||
if (!node.loc) { | ||
node.loc = nodeLoc; | ||
} | ||
nodesOut.push(inForInit && node.type === "ExpressionStatement" ? node.expression : node); | ||
nodesOut.push(node); | ||
} | ||
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) { | ||
const expr = nodesOut[1].expression; | ||
expr.arguments = [nodesOut[0].declarations[0].init]; | ||
nodesOut = [expr]; | ||
} else { | ||
if (core.types.isForStatement(path.parent, { | ||
init: node | ||
}) && !nodesOut.some(v => core.types.isVariableDeclaration(v))) { | ||
for (let i = 0; i < nodesOut.length; i++) { | ||
const node = nodesOut[i]; | ||
if (core.types.isExpressionStatement(node)) { | ||
nodesOut[i] = node.expression; | ||
} | ||
} | ||
} | ||
} | ||
if (nodesOut.length === 1) { | ||
@@ -486,3 +446,2 @@ path.replaceWith(nodesOut[0]); | ||
} | ||
scope.crawl(); | ||
@@ -508,7 +467,5 @@ } | ||
let ref; | ||
if (!parentPath.isExpressionStatement() && !parentPath.isSequenceExpression() || path.isCompletionRecord()) { | ||
ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref"); | ||
nodes.push(core.types.variableDeclaration("var", [core.types.variableDeclarator(ref, node.right)])); | ||
if (core.types.isArrayExpression(node.right)) { | ||
@@ -518,5 +475,3 @@ destructuring.arrayRefSet.add(ref.name); | ||
} | ||
destructuring.init(node.left, ref || node.right); | ||
if (ref) { | ||
@@ -530,3 +485,2 @@ if (parentPath.isArrowFunctionExpression()) { | ||
} | ||
path.replaceWithMultiple(nodes); | ||
@@ -542,9 +496,6 @@ scope.crawl(); | ||
} | ||
return false; | ||
} | ||
var index = helperPluginUtils.declare((api, options) => { | ||
var _ref, _api$assumption, _ref2, _options$allowArrayLi, _ref3, _api$assumption2; | ||
api.assertVersion(7); | ||
@@ -565,3 +516,2 @@ const { | ||
const specifiers = []; | ||
for (const name of Object.keys(path.getOuterBindingIdentifiers())) { | ||
@@ -575,3 +525,2 @@ specifiers.push(core.types.exportSpecifier(core.types.identifier(name), core.types.identifier(name))); | ||
}, | ||
ForXStatement(path) { | ||
@@ -583,4 +532,4 @@ const { | ||
const left = node.left; | ||
if (core.types.isPattern(left)) { | ||
if (core.types.isPattern(left)) { | ||
const temp = scope.generateUidIdentifier("ref"); | ||
@@ -591,7 +540,5 @@ node.left = core.types.variableDeclaration("var", [core.types.variableDeclarator(temp)]); | ||
const nodes = []; | ||
if (statementBody.length === 0 && path.isCompletionRecord()) { | ||
nodes.unshift(core.types.expressionStatement(scope.buildUndefinedNode())); | ||
} | ||
nodes.unshift(core.types.expressionStatement(core.types.assignmentExpression("=", left, core.types.cloneNode(temp)))); | ||
@@ -602,3 +549,2 @@ unshiftForXStatementBody(path, nodes); | ||
} | ||
if (!core.types.isVariableDeclaration(left)) return; | ||
@@ -624,3 +570,2 @@ const pattern = left.declarations[0].id; | ||
}, | ||
CatchClause({ | ||
@@ -649,3 +594,2 @@ node, | ||
}, | ||
AssignmentExpression(path, state) { | ||
@@ -655,3 +599,2 @@ if (!core.types.isPattern(path.node.left)) return; | ||
}, | ||
VariableDeclaration(path, state) { | ||
@@ -667,3 +610,2 @@ const { | ||
} | ||
} | ||
@@ -670,0 +612,0 @@ }; |
{ | ||
"name": "@babel/plugin-transform-destructuring", | ||
"version": "7.20.0", | ||
"version": "7.20.2", | ||
"description": "Compile ES2015 destructuring to ES5", | ||
@@ -20,3 +20,3 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.19.0" | ||
"@babel/helper-plugin-utils": "^7.20.2" | ||
}, | ||
@@ -27,5 +27,5 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.19.6", | ||
"@babel/core": "^7.20.2", | ||
"@babel/helper-plugin-test-runner": "^7.18.6", | ||
"@babel/traverse": "^7.20.0" | ||
"@babel/traverse": "^7.20.1" | ||
}, | ||
@@ -32,0 +32,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
81533
540