Skip to content

Commit

Permalink
Fix labeled break stmt removal from last switch case (close #238) (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi authored and kangax committed Nov 2, 2016
1 parent 43335e6 commit eb9df7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
27 changes: 27 additions & 0 deletions packages/babel-plugin-minify-simplify/__tests__/simplify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,33 @@ describe("simplify-plugin", () => {
expect(transform(source)).toBe(expected);
});

it("should not remove last break statement if it contains a label", () => {
const source = unpad(`
loop: while (foo) {
switch (bar) {
case 47:
break;
}
switch (baz) {
default:
break loop;
}
}
`);
const expected = unpad(`
loop: for (; foo;) {
switch (bar) {
case 47:
}
switch (baz) {
default:
break loop;
}
}
`);
expect(transform(source)).toBe(expected);
});

it("should convert consequents in switch into sequence expressions", () => {
const source = unpad(`
function bar() {
Expand Down
6 changes: 2 additions & 4 deletions packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,9 @@ module.exports = ({ types: t }) => {
}

const potentialBreak = lastCase.get("consequent")[lastCase.node.consequent.length - 1];
if (!t.isBreakStatement(potentialBreak)) {
return;
if (t.isBreakStatement(potentialBreak) && potentialBreak.node.label === null) {
potentialBreak.remove();
}

potentialBreak.remove();
},

createPrevExpressionEater("switch"),
Expand Down

0 comments on commit eb9df7a

Please sign in to comment.