Skip to content

Commit 8c7b973

Browse files
authored
Fix block removal during early continue transform (#623)
* Fix block removal during early continue transform + Fix #560 * Fix test suite
1 parent 5e1870b commit 8c7b973

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/babel-plugin-minify-simplify/__tests__/simplify-test.js

+20
Original file line numberDiff line numberDiff line change
@@ -2512,4 +2512,24 @@ describe("simplify-plugin", () => {
25122512
const b = () => {};
25132513
`
25142514
);
2515+
2516+
thePlugin(
2517+
"should NOT remove block for early continue transforms - fix issue#560",
2518+
`
2519+
function foo() {
2520+
while (true) {
2521+
const {x} = a;
2522+
const {y} = b;
2523+
}
2524+
}
2525+
`,
2526+
`
2527+
function foo() {
2528+
for (; true;) {
2529+
const { x } = a;
2530+
const { y } = b;
2531+
}
2532+
}
2533+
`
2534+
);
25152535
});

packages/babel-plugin-minify-simplify/src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,8 @@ module.exports = ({ types: t }) => {
15191519
t.isTryStatement(parent) ||
15201520
t.isCatchClause(parent) ||
15211521
t.isSwitchStatement(parent) ||
1522-
(isSingleBlockScopeDeclaration(node) && t.isIfStatement(parent))
1522+
(isSingleBlockScopeDeclaration(node) &&
1523+
(t.isIfStatement(parent) || t.isLoop(parent)))
15231524
);
15241525
}
15251526

@@ -1581,7 +1582,7 @@ module.exports = ({ types: t }) => {
15811582
}
15821583

15831584
// We may have reduced the body to a single statement.
1584-
if (node.body.body.length === 1) {
1585+
if (node.body.body.length === 1 && !needsBlock(node.body, node)) {
15851586
path.get("body").replaceWith(node.body.body[0]);
15861587
}
15871588
}

0 commit comments

Comments
 (0)