From d1dcc76a27c2bf5809454f522001d13067f7ba15 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Tue, 21 Mar 2017 17:36:22 +0100 Subject: [PATCH 1/2] handle default statements with fallthrough properly [fix #423] --- .../__tests__/simplify-test.js | 26 +++++++++++++++++++ .../babel-plugin-minify-simplify/src/index.js | 24 +++++++---------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index f3c35fa84..9a35aedaa 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -3098,4 +3098,30 @@ 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); + }); }); diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index d4960e4bc..9a0c1d8d9 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -1263,7 +1263,7 @@ module.exports = ({ types: t }) => { return; } - const consTestPairs = []; + let consTestPairs = []; let fallThru = []; let defaultRet; for (const switchCase of node.cases) { @@ -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]); } From 7f0b3999f0435870866f8a2240d15aae6e6ffb03 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Wed, 22 Mar 2017 18:10:07 +0100 Subject: [PATCH 2/2] add multiple fallthrough test case --- .../__tests__/simplify-test.js | 27 +++++++++++++++++++ .../babel-plugin-minify-simplify/src/index.js | 2 +- 2 files changed, 28 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 9a35aedaa..0f36d4401 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -3124,4 +3124,31 @@ describe("simplify-plugin", () => { ); 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 9a0c1d8d9..751ed88ab 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -1263,7 +1263,7 @@ module.exports = ({ types: t }) => { return; } - let consTestPairs = []; + const consTestPairs = []; let fallThru = []; let defaultRet; for (const switchCase of node.cases) {