diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index f3c35fa84..0f36d4401 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -3098,4 +3098,57 @@ describe("simplify-plugin", () => { } expect(transform(source)).toBe(expected); }); + + it("should fix issue#423 with fallthrough in default case", () => { + const source = unpad( + ` + function foo(bar) { + switch (bar) { + case 'a': + return 1; + case 'b': + default: + return 4; + case 'c': + return 3; + } + } + ` + ); + const expected = unpad( + ` + function foo(bar) { + return bar === 'a' ? 1 : bar === 'c' ? 3 : 4; + } + ` + ); + expect(transform(source)).toBe(expected); + }); + + it("should convert multiple fallthrough in switch to conditional expression", () => { + const source = unpad( + ` + function foo(bar) { + switch (bar) { + case 'a': + case 'b': + return 1; + case 'd': + default: + return 4; + case 'c': + return 3; + } + } + ` + ); + const expected = unpad( + ` + function foo(bar) { + return bar === 'a' || bar === 'b' ? 1 : bar === 'c' ? 3 : 4; + } + ` + ); + expect(transform(source)).toBe(expected); + }); }); diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index d4960e4bc..751ed88ab 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -1273,6 +1273,7 @@ module.exports = ({ types: t }) => { const cons = switchCase.consequent[0]; + // default case if (!switchCase.test) { if (!t.isReturnStatement(cons)) { return; @@ -1282,11 +1283,7 @@ module.exports = ({ types: t }) => { } if (!switchCase.consequent.length) { - if (fallThru.length) { - fallThru.push(switchCase.test); - } else { - fallThru = [switchCase.test]; - } + fallThru.push(switchCase.test); continue; } @@ -1300,7 +1297,8 @@ module.exports = ({ types: t }) => { node.discriminant, switchCase.test ); - if (fallThru.length) { + + if (fallThru.length && !defaultRet) { test = fallThru.reduceRight( (right, test) => t.logicalExpression( @@ -1310,8 +1308,8 @@ module.exports = ({ types: t }) => { ), test ); - fallThru = []; } + fallThru = []; consTestPairs.push([test, cons.argument || VOID_0]); } @@ -1387,11 +1385,7 @@ module.exports = ({ types: t }) => { } if (!switchCase.consequent.length) { - if (fallThru.length) { - fallThru.push(switchCase.test); - } else { - fallThru = [switchCase.test]; - } + fallThru.push(switchCase.test); continue; } @@ -1416,7 +1410,7 @@ module.exports = ({ types: t }) => { node.discriminant, switchCase.test ); - if (fallThru.length) { + if (fallThru.length && !defaultExpr) { test = fallThru.reduceRight( (right, test) => t.logicalExpression( @@ -1426,8 +1420,8 @@ module.exports = ({ types: t }) => { ), test ); - fallThru = []; } + fallThru = []; exprTestPairs.push([test, cons.expression]); }