Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle default statements with fallthrough properly [fix #423] #482

Merged
merged 2 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
22 changes: 8 additions & 14 deletions packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ module.exports = ({ types: t }) => {

const cons = switchCase.consequent[0];

// default case
if (!switchCase.test) {
if (!t.isReturnStatement(cons)) {
return;
Expand All @@ -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;
}

Expand All @@ -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(
Expand All @@ -1310,8 +1308,8 @@ module.exports = ({ types: t }) => {
),
test
);
fallThru = [];
}
fallThru = [];

consTestPairs.push([test, cons.argument || VOID_0]);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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(
Expand All @@ -1426,8 +1420,8 @@ module.exports = ({ types: t }) => {
),
test
);
fallThru = [];
}
fallThru = [];

exprTestPairs.push([test, cons.expression]);
}
Expand Down