diff --git a/src/babel/transformation/transformers/es6/for-of.js b/src/babel/transformation/transformers/es6/for-of.js index cb7f0ed326..fad3407e03 100644 --- a/src/babel/transformation/transformers/es6/for-of.js +++ b/src/babel/transformation/transformers/es6/for-of.js @@ -52,6 +52,9 @@ var breakVisitor = { if (!node.label && state.ignoreLabeless) return; if (node.label && node.label.name !== state.label) return; + // break statements mean something different in this context + if (t.isSwitchCase(parent)) return; + var ret = t.expressionStatement( t.callExpression(t.memberExpression(state.iteratorKey, t.identifier("return")), []) ); diff --git a/test/fixtures/transformation/es6-for-of-loose/ignore-cases/actual.js b/test/fixtures/transformation/es6-for-of-loose/ignore-cases/actual.js new file mode 100644 index 0000000000..e791410529 --- /dev/null +++ b/test/fixtures/transformation/es6-for-of-loose/ignore-cases/actual.js @@ -0,0 +1,6 @@ +for (var i of foo) { + switch (i) { + case 1: + break; + } +} diff --git a/test/fixtures/transformation/es6-for-of-loose/ignore-cases/expected.js b/test/fixtures/transformation/es6-for-of-loose/ignore-cases/expected.js new file mode 100644 index 0000000000..d7576bf4f7 --- /dev/null +++ b/test/fixtures/transformation/es6-for-of-loose/ignore-cases/expected.js @@ -0,0 +1,21 @@ +"use strict"; + +for (var _iterator = foo, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + var i = _ref; + + switch (i) { + case 1: + break; + } +} diff --git a/test/fixtures/transformation/es6-for-of/ignore-cases/actual.js b/test/fixtures/transformation/es6-for-of/ignore-cases/actual.js new file mode 100644 index 0000000000..e791410529 --- /dev/null +++ b/test/fixtures/transformation/es6-for-of/ignore-cases/actual.js @@ -0,0 +1,6 @@ +for (var i of foo) { + switch (i) { + case 1: + break; + } +} diff --git a/test/fixtures/transformation/es6-for-of/ignore-cases/expected.js b/test/fixtures/transformation/es6-for-of/ignore-cases/expected.js new file mode 100644 index 0000000000..c6a1ba302a --- /dev/null +++ b/test/fixtures/transformation/es6-for-of/ignore-cases/expected.js @@ -0,0 +1,29 @@ +"use strict"; + +var _iteratorNormalCompletion = true; +var _didIteratorError = false; +var _iteratorError = undefined; + +try { + for (var _iterator = foo[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var i = _step.value; + + switch (i) { + case 1: + break; + } + } +} catch (err) { + _didIteratorError = true; + _iteratorError = err; +} finally { + try { + if (!_iteratorNormalCompletion && _iterator["return"]) { + _iterator["return"](); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } +}