From 4043e8e8b9f14cae9c09f4ebddc1f9fe9380655c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 30 May 2015 22:33:09 -0400 Subject: [PATCH] fix destructuring transformer AssignmentExpression to properly handle completion records --- .../transformers/es6/destructuring.js | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index d46e4c2576..00f0f30218 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -110,12 +110,7 @@ export function CatchClause(node, parent, scope, file) { export function AssignmentExpression(node, parent, scope, file) { if (!t.isPattern(node.left)) return; - var ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref"); - var nodes = []; - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(ref, node.right) - ])); var destructuring = new DestructuringTransformer({ operator: node.operator, @@ -124,13 +119,24 @@ export function AssignmentExpression(node, parent, scope, file) { nodes: nodes }); - if (t.isArrayExpression(node.right)) { - destructuring.arrays[ref.name] = true; + var ref; + if (this.isCompletionRecord() || !this.parentPath.isExpressionStatement()) { + ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");; + + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(ref, node.right) + ])); + + if (t.isArrayExpression(node.right)) { + destructuring.arrays[ref.name] = true; + } } - destructuring.init(node.left, ref); + destructuring.init(node.left, ref || node.right); - nodes.push(t.expressionStatement(ref)); + if (ref) { + nodes.push(t.expressionStatement(ref)); + } return nodes; }