diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index adc68ed12..9642c602a 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -2512,4 +2512,24 @@ describe("simplify-plugin", () => { const b = () => {}; ` ); + + thePlugin( + "should NOT remove block for early continue transforms - fix issue#560", + ` + function foo() { + while (true) { + const {x} = a; + const {y} = b; + } + } + `, + ` + function foo() { + for (; true;) { + const { x } = a; + const { y } = b; + } + } + ` + ); }); diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index 3a45ab228..fbf3ae45b 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -1519,7 +1519,8 @@ module.exports = ({ types: t }) => { t.isTryStatement(parent) || t.isCatchClause(parent) || t.isSwitchStatement(parent) || - (isSingleBlockScopeDeclaration(node) && t.isIfStatement(parent)) + (isSingleBlockScopeDeclaration(node) && + (t.isIfStatement(parent) || t.isLoop(parent))) ); } @@ -1581,7 +1582,7 @@ module.exports = ({ types: t }) => { } // We may have reduced the body to a single statement. - if (node.body.body.length === 1) { + if (node.body.body.length === 1 && !needsBlock(node.body, node)) { path.get("body").replaceWith(node.body.body[0]); } }