fix(es/minifier): Compress consecutive return statements properly (#9620)

**Description:**

Compress consecutive return statements properly.

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/9619
This commit is contained in:
canalun 2024-10-08 07:58:16 +09:00 committed by GitHub
parent 7b98bb5e93
commit 8263da1766
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---
fix: compress consecutive return statements properly

View File

@ -219,6 +219,16 @@ impl Optimizer<'_> {
}
},
(
Some(Stmt::Block(BlockStmt { stmts: s1, .. })),
Some(Stmt::Block(BlockStmt { stmts: s2, .. })),
) if s1.iter().any(|s| matches!(s, Stmt::Return(..)))
&& s2.iter().any(|s| matches!(s, Stmt::Return(..))) =>
{
log_abort!("if_return: [x] Aborting because early return is observed");
return;
}
_ => {}
}
}

View File

@ -0,0 +1,47 @@
{
"arguments": false,
"arrows": false,
"booleans": false,
"booleans_as_integers": false,
"collapse_vars": false,
"comparisons": false,
"computed_props": false,
"conditionals": false,
"dead_code": true,
"directives": false,
"drop_console": false,
"drop_debugger": false,
"evaluate": false,
"expression": false,
"hoist_funs": false,
"hoist_props": false,
"hoist_vars": false,
"if_return": true,
"join_vars": false,
"keep_classnames": false,
"keep_fargs": false,
"keep_fnames": false,
"keep_infinity": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": false,
"switches": true,
"typeofs": false,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": false,
"pristine_globals": false,
"passes": 1
}

View File

@ -0,0 +1,17 @@
var a = (() => {
switch ("production") {
case "production":
return "expected";
default:
return "unexpected1";
}
switch ("production") {
case "production":
return "unexpected2";
default:
return "unexpected3";
}
})();
console.log(a);

View File

@ -0,0 +1,2 @@
var a = (()=>"expected")();
console.log(a);