From 697c6294b07ee49da5f01a0cad0ab3aa6304781e Mon Sep 17 00:00:00 2001 From: Armano Date: Fri, 7 Aug 2026 12:13:42 +0200 Subject: [PATCH] fix(minifier): preserve Annex B labeled-function bindings in implicit-return rewrite (#25349) fixes #22223 --- crates/oxc_minifier/src/peephole/minimize_statements.rs | 8 ++++---- .../oxc_minifier/tests/peephole/minimize_exit_points.rs | 9 ++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/crates/oxc_minifier/src/peephole/minimize_statements.rs b/crates/oxc_minifier/src/peephole/minimize_statements.rs index 12e7a45eaf..80908cf6e5 100644 --- a/crates/oxc_minifier/src/peephole/minimize_statements.rs +++ b/crates/oxc_minifier/src/peephole/minimize_statements.rs @@ -1265,7 +1265,7 @@ impl<'a> PeepholeOptimizations { result.push(Statement::LabeledStatement(labeled_stmt)); } - /// `appendIfOrLabelBodyPreservingScope`: + /// `appendIfOrLabelBodyPreservingScope`: fn handle_block( result: &mut ArenaVec<'a, Statement<'a>>, block_stmt: ArenaBox<'a, BlockStatement<'a>>, @@ -1280,7 +1280,7 @@ impl<'a> PeepholeOptimizations { } } - /// `statementCaresAboutScope`: + /// `statementCaresAboutScope`: pub fn statement_cares_about_scope(stmt: &Statement<'a>) -> bool { match stmt { Statement::BlockStatement(_) @@ -1299,8 +1299,8 @@ impl<'a> PeepholeOptimizations { | Statement::ReturnStatement(_) | Statement::ThrowStatement(_) | Statement::BreakStatement(_) - | Statement::ContinueStatement(_) - | Statement::LabeledStatement(_) => false, + | Statement::ContinueStatement(_) => false, + Statement::LabeledStatement(label) => Self::statement_cares_about_scope(&label.body), Statement::VariableDeclaration(decl) => !decl.kind.is_var(), _ => true, } diff --git a/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs b/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs index d2f470819a..ca422368e3 100644 --- a/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs +++ b/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs @@ -284,12 +284,11 @@ fn test_for_continue_optimization() { } #[test] -#[ignore = "TODO: Code motion with function hoisting not yet implemented"] fn test_code_motion_doesnt_break_function_hoisting() { - test( - "function f() { if (x) return; foo(); function foo() {} }", - "function f() { if (x); else { function foo() {} foo(); } }", - ); + test_same("function f() { if (x) return; foo(); function foo() {} }"); + test_same("function f() { if (x) return; foo(); label: function foo() {} }"); + test_same("function a() { if (typeof f == 'function') return; function f() {} }"); + test_same("function a() { if (typeof f == 'function') return; label: function f() {} }"); } #[test]