Skip to content
Open
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 @@ -1623,6 +1623,66 @@ describe("simplify-plugin", () => {
`
);

thePlugin(
"should ignore statements after return",
`
function bar(foo) {
switch (foo) {
case 'foo':
console.log('foo')
return 1;
console.log('bar')
case 'bar':
return 2;
default:
return 3;
}
}
`,
`
function bar(foo) {
return foo === 'foo' ? (console.log('foo'), 1) : foo === 'bar' ? 2 : 3;
}
`
);

thePlugin(
"should deoptimize if statement could not be converted to sequenceExpression",
`
function bar(foo) {
switch (foo) {
case 'foo':
var something = foo();
console.log('foo');
return 1;
console.log('ignore');

case 'bar':
return 2;
default:
return 3;
}
}
`,
`
function bar(foo) {
switch (foo) {
case 'foo':
var something = foo();

return console.log('foo'), 1;
console.log('ignore');


case 'bar':
return 2;
default:
return 3;
}
}
`
);

thePlugin(
"should convert switch statements with next return as default to returns",
`
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ module.exports = ({ types: t }) => {
let defaultRet;
for (const switchCase of node.cases) {
if (switchCase.consequent.length > 1) {
return;
if (!t.isReturnStatement(switchCase.consequent[0])) return;
}

const cons = switchCase.consequent[0];
Expand Down