From af4feb4d889277a8a803d9a9c449165b11cc6c76 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 20 May 2015 10:34:50 +0100 Subject: [PATCH] fix renaming of assignment expressions to fix pattern renaming in the es6.blockScoping transformer - fixes #1576 --- .../transformers/es6/block-scoping.js | 16 ++++++++++++---- src/babel/traversal/path/index.js | 7 ++++++- .../assignment-patterns/actual.js | 8 ++++++++ .../assignment-patterns/expected.js | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/actual.js create mode 100644 test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/expected.js diff --git a/src/babel/transformation/transformers/es6/block-scoping.js b/src/babel/transformation/transformers/es6/block-scoping.js index 0b3d17b43e..36d91fb64d 100644 --- a/src/babel/transformation/transformers/es6/block-scoping.js +++ b/src/babel/transformation/transformers/es6/block-scoping.js @@ -84,8 +84,6 @@ export function BlockStatement(block, parent, scope, file) { export { BlockStatement as Program }; function replace(node, parent, scope, remaps) { - if (!t.isReferencedIdentifier(node, parent)) return; - var remap = remaps[node.name]; if (!remap) return; @@ -100,11 +98,21 @@ function replace(node, parent, scope, remaps) { } var replaceVisitor = { - enter: replace + ReferencedIdentifier: replace, + + AssignmentExpression(node, parent, scope, remaps) { + var ids = this.getBindingIdentifiers(); + for (var name in ids) { + replace(ids[name], node, scope, remaps); + } + }, }; function traverseReplace(node, parent, scope, remaps) { - replace(node, parent, scope, remaps); + if (t.isIdentifier(node)) { + replace(node, parent, scope, remaps); + } + scope.traverse(node, replaceVisitor, remaps); } diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index 5f6507ca18..3e965e0b1d 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -260,10 +260,15 @@ export default class TraversalPath { do { var container = path.container; + + if (path.isFunction()) { + return false; + } + if (Array.isArray(container) && path.key !== container.length - 1) { return false; } - } while (path = path.parentPath && !path.isProgram()); + } while ((path = path.parentPath) && !path.isProgram()); return true; } diff --git a/test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/actual.js b/test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/actual.js new file mode 100644 index 0000000000..4c0940362a --- /dev/null +++ b/test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/actual.js @@ -0,0 +1,8 @@ +const foo = "foo"; + +function foobar() { + for (let item of [1, 2, 3]) { + let foo = "bar"; + [bar, foo] = [1, 2]; + } +} diff --git a/test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/expected.js b/test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/expected.js new file mode 100644 index 0000000000..c40cc2f668 --- /dev/null +++ b/test/core/fixtures/transformation/es6.block-scoping/assignment-patterns/expected.js @@ -0,0 +1,14 @@ +"use strict"; + +var foo = "foo"; + +function foobar() { + var _arr = [1, 2, 3]; + + for (var _i = 0; _i < _arr.length; _i++) { + var item = _arr[_i]; + var _foo = "bar"; + bar = 1; + _foo = 2; + } +}