diff --git a/.changeset/famous-keys-shake.md b/.changeset/famous-keys-shake.md new file mode 100644 index 00000000000..dd104ce70e5 --- /dev/null +++ b/.changeset/famous-keys-shake.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_minifier: patch +--- + +fix: compress consecutive return statements properly diff --git a/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs b/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs index 7baf0ac5c89..e5ad4d9de2b 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs @@ -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; + } + _ => {} } } diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9619/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/9619/config.json new file mode 100644 index 00000000000..04a71251750 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9619/config.json @@ -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 +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9619/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/9619/input.js new file mode 100644 index 00000000000..8b5f08909ec --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9619/input.js @@ -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); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9619/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/9619/output.js new file mode 100644 index 00000000000..33664f1d9a9 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9619/output.js @@ -0,0 +1,2 @@ +var a = (()=>"expected")(); +console.log(a);