From 52a070770810e5f9fb83d49d89e39578bf5dadec Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sat, 8 Jul 2017 18:22:52 +0200 Subject: [PATCH 1/2] Fix block removal during early continue transform + Fix #560 --- .../__tests__/simplify-test.js | 20 +++++++++++++++++++ .../babel-plugin-minify-simplify/src/index.js | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index adc68ed12..11ea7d29a 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 (;;) { + const { x: c } = a, + { y: d } = 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]); } } From 262344c279ac802266cadfb36d838c11c612519a Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sat, 8 Jul 2017 18:28:10 +0200 Subject: [PATCH 2/2] Fix test suite --- .../babel-plugin-minify-simplify/__tests__/simplify-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index 11ea7d29a..9642c602a 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -2525,9 +2525,9 @@ describe("simplify-plugin", () => { `, ` function foo() { - for (;;) { - const { x: c } = a, - { y: d } = b; + for (; true;) { + const { x } = a; + const { y } = b; } } `