From 97680e9dfde6b9a161d9ca5b2f7a0b7e393fd119 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 13 May 2015 19:55:40 +0100 Subject: [PATCH] properly hoist all var patterns when wrapping bodies in the es6.blockScoping transformer - fixes #1521 --- CHANGELOG.md | 5 +++++ .../transformation/transformers/es6/block-scoping.js | 10 +++++++--- .../transformation/transformers/es6/destructuring.js | 2 +- .../es6.block-scoping/hoisting/actual.js | 1 + .../es6.block-scoping/hoisting/expected.js | 5 ++++- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c49c95a4f..af280852c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog. +## 5.3.2 + + * **Bug Fix** + * Fix patterns not being considered when hoisting variables in the `es6.blockScoping` transformer. + ## 5.3.1 * **Bug Fix** diff --git a/src/babel/transformation/transformers/es6/block-scoping.js b/src/babel/transformation/transformers/es6/block-scoping.js index e6a8d9bcd2..0b3d17b43e 100644 --- a/src/babel/transformation/transformers/es6/block-scoping.js +++ b/src/babel/transformation/transformers/es6/block-scoping.js @@ -557,9 +557,13 @@ class BlockScoping { */ pushDeclar(node: { type: "VariableDeclaration" }): Array { - this.body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) { - return t.variableDeclarator(declar.id); - }))); + var declars = []; + var names = t.getBindingIdentifiers(node); + for (var name in names) { + declars.push(t.variableDeclarator(names[name])); + } + + this.body.push(t.variableDeclaration(node.kind, declars)); var replace = []; diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index 6906d4e637..ef02dbcc88 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -179,7 +179,7 @@ export function VariableDeclaration(node, parent, scope, file) { file: file }); - if (t.isPattern(pattern) && patternId) { + if (t.isPattern(pattern)) { destructuring.init(pattern, patternId); if (+i !== node.declarations.length - 1) { diff --git a/test/core/fixtures/transformation/es6.block-scoping/hoisting/actual.js b/test/core/fixtures/transformation/es6.block-scoping/hoisting/actual.js index 03757cd57d..50a10043c5 100644 --- a/test/core/fixtures/transformation/es6.block-scoping/hoisting/actual.js +++ b/test/core/fixtures/transformation/es6.block-scoping/hoisting/actual.js @@ -1,5 +1,6 @@ for (let i of nums) { var x = 5; + var { f } = { f: 2 }; fns.push(function () { return i * x; }); diff --git a/test/core/fixtures/transformation/es6.block-scoping/hoisting/expected.js b/test/core/fixtures/transformation/es6.block-scoping/hoisting/expected.js index 8ca7125d32..087049941e 100644 --- a/test/core/fixtures/transformation/es6.block-scoping/hoisting/expected.js +++ b/test/core/fixtures/transformation/es6.block-scoping/hoisting/expected.js @@ -8,6 +8,8 @@ try { var _loop = function () { var i = _step.value; x = 5; + var _f = { f: 2 }; + f = _f.f; fns.push(function () { return i * x; @@ -16,6 +18,7 @@ try { for (var _iterator = nums[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var x; + var f; _loop(); } @@ -32,4 +35,4 @@ try { throw _iteratorError; } } -} \ No newline at end of file +}