From 1a30f1aafc09cb139220ef574e6ba0bf4afc22aa Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 11 Apr 2015 18:13:47 -0700 Subject: [PATCH] fix regression with completion records for assignment expressions - fixes #1204 --- .../transformers/es6/destructuring.js | 10 ++++++++-- src/babel/traversal/path/index.js | 15 ++++++++++++++- .../actual.js | 2 ++ .../expected.js | 7 +++++++ .../assignment-expression/expected.js | 4 ++-- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/actual.js create mode 100644 test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/expected.js diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index 510eebfee7..3d774451d7 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -126,10 +126,11 @@ export function AssignmentExpression(node, parent, scope, file) { if (!t.isPattern(node.left)) return; var ref = scope.generateUidIdentifier("temp"); - scope.push({ id: ref }); var nodes = []; - nodes.push(t.expressionStatement(t.assignmentExpression("=", ref, node.right))); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(ref, node.right) + ])); var destructuring = new DestructuringTransformer({ operator: node.operator, @@ -137,6 +138,11 @@ export function AssignmentExpression(node, parent, scope, file) { scope: scope, nodes: nodes }); + + if (t.isArrayExpression(node.right)) { + destructuring.arrays[ref.name] = true; + } + destructuring.init(node.left, ref); nodes.push(t.expressionStatement(ref)); diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index 0356e9929d..b7476c109d 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -132,11 +132,24 @@ export default class TraversalPath { _maybePopFromStatements(nodes) { var last = nodes[nodes.length - 1]; - if (t.isExpressionStatement(last) && t.isIdentifier(last.expression)) { + if (t.isExpressionStatement(last) && t.isIdentifier(last.expression) && !this.isCompletionRecord()) { nodes.pop(); } } + isCompletionRecord() { + var path = this; + + do { + var container = path.container; + if (Array.isArray(container) && path.key !== container.length - 1) { + return false; + } + } while (path = path.parentPath && !path.isProgram()); + + return true; + } + isStatementOrBlock() { if (t.isLabeledStatement(this.parent) || t.isBlockStatement(this.container)) { return false; diff --git a/test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/actual.js b/test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/actual.js new file mode 100644 index 0000000000..63c2b6595f --- /dev/null +++ b/test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/actual.js @@ -0,0 +1,2 @@ +var x, y; +[x, y] = [1, 2]; diff --git a/test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/expected.js b/test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/expected.js new file mode 100644 index 0000000000..8f31c828a4 --- /dev/null +++ b/test/core/fixtures/transformation/es6.destructuring/assignment-expression-completion-record/expected.js @@ -0,0 +1,7 @@ +"use strict"; + +var x, y; +var _temp = [1, 2]; +x = _temp[0]; +y = _temp[1]; +_temp; diff --git a/test/core/fixtures/transformation/es6.destructuring/assignment-expression/expected.js b/test/core/fixtures/transformation/es6.destructuring/assignment-expression/expected.js index 9ef5dc3448..72bddcac55 100644 --- a/test/core/fixtures/transformation/es6.destructuring/assignment-expression/expected.js +++ b/test/core/fixtures/transformation/es6.destructuring/assignment-expression/expected.js @@ -1,5 +1,5 @@ "use strict"; -var _temp, _temp2; +var _temp; -console.log((_temp = [123], _temp2 = babelHelpers.slicedToArray(_temp, 1), x = _temp2[0], _temp)); +console.log((_temp = [123], x = _temp[0], _temp));