From 2beae06a57c72d04a07cab729dfe2ee7502a7da4 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Wed, 19 Jul 2017 15:01:06 +0200 Subject: [PATCH] Don't remove else with blockscoped decl Fix #637 --- .../__tests__/simplify-test.js | 25 +++++++++++++++++++ .../src/if-statement.js | 13 +++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index 9642c602a..9f15e450c 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -2532,4 +2532,29 @@ describe("simplify-plugin", () => { } ` ); + + // TODO: issue-637 + thePlugin( + "should NOT remove else block when ", + ` + function test(a) { + const clash = () => {}; + if (a) { + return clash(); + } else { + const clash = () => {}; + return clash(); + } + } + `, + ` + function test(a) { + const clash = () => {}; + if (a) return clash();else { + const clash = () => {}; + return clash(); + } + } + ` + ); }); diff --git a/packages/babel-plugin-minify-simplify/src/if-statement.js b/packages/babel-plugin-minify-simplify/src/if-statement.js index 37cefc1af..61dd213a6 100644 --- a/packages/babel-plugin-minify-simplify/src/if-statement.js +++ b/packages/babel-plugin-minify-simplify/src/if-statement.js @@ -276,7 +276,18 @@ module.exports = t => { (consequent.isBlockStatement() && t.isReturnStatement( consequent.node.body[consequent.node.body.length - 1] - ))) + ))) && + // don't hoist declarations + // TODO: validate declarations after fixing scope issues + (alternate.isBlockStatement() + ? !alternate + .get("body") + .some( + stmt => + stmt.isVariableDeclaration({ kind: "let" }) || + stmt.isVariableDeclaration({ kind: "const" }) + ) + : true) ) { path.insertAfter( alternate.isBlockStatement()