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()