From 93feabb82e7b3aeb343b8d739442eba11bef73a5 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 8 Apr 2015 08:10:53 -0700 Subject: [PATCH] fix forOf loop inheritance - fixes #1169 --- .../transformation/transformers/es6/for-of.js | 4 +-- .../misc/regression-1169/actual.js | 11 +++++++ .../misc/regression-1169/expected.js | 33 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test/core/fixtures/transformation/misc/regression-1169/actual.js create mode 100644 test/core/fixtures/transformation/misc/regression-1169/expected.js diff --git a/src/babel/transformation/transformers/es6/for-of.js b/src/babel/transformation/transformers/es6/for-of.js index fd67d272a5..46b54a8be9 100644 --- a/src/babel/transformation/transformers/es6/for-of.js +++ b/src/babel/transformation/transformers/es6/for-of.js @@ -17,9 +17,6 @@ export function ForOfStatement(node, parent, scope, file) { var loop = build.loop; var block = loop.body; - // inherit comments from the original loop - t.inheritsComments(loop, node); - // ensure that it's a block so we can take all its statements t.ensureBlock(node); @@ -32,6 +29,7 @@ export function ForOfStatement(node, parent, scope, file) { block.body = block.body.concat(node.body.body); t.inherits(loop, node); + t.inherits(loop.body, node.body); if (build.replaceParent) { this.parentPath.replaceWithMultiple(build.node); diff --git a/test/core/fixtures/transformation/misc/regression-1169/actual.js b/test/core/fixtures/transformation/misc/regression-1169/actual.js new file mode 100644 index 0000000000..1846454a90 --- /dev/null +++ b/test/core/fixtures/transformation/misc/regression-1169/actual.js @@ -0,0 +1,11 @@ +function foo() { + let input = ['a', 'b', 'c'] + let output = {} + + for (let c of input) { + let name = c + output[name] = name + } + + return output +} diff --git a/test/core/fixtures/transformation/misc/regression-1169/expected.js b/test/core/fixtures/transformation/misc/regression-1169/expected.js new file mode 100644 index 0000000000..f9d3032932 --- /dev/null +++ b/test/core/fixtures/transformation/misc/regression-1169/expected.js @@ -0,0 +1,33 @@ +'use strict'; + +function foo() { + var input = ['a', 'b', 'c']; + var output = {}; + + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = input[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var c = _step.value; + + var _name = c; + output[_name] = _name; + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator['return']) { + _iterator['return'](); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return output;