fix(minifier): preserve Annex B labeled-function bindings in implicit-return rewrite (#25349)

fixes #22223
This commit is contained in:
Armano authored and GitHub committed 2026-08-07 19:13:42 +09:00
1 parent 4ec9189b26
commit 697c6294b0
2 files changed
+8 -9

No files matched your search

@@ -1265,7 +1265,7 @@ impl<'a> PeepholeOptimizations {
result.push(Statement::LabeledStatement(labeled_stmt));
}
/// `appendIfOrLabelBodyPreservingScope`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_parser.go#L9852>
/// `appendIfOrLabelBodyPreservingScope`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_parser/js_parser.go#L9839>
fn handle_block(
result: &mut ArenaVec<'a, Statement<'a>>,
block_stmt: ArenaBox<'a, BlockStatement<'a>>,
@@ -1280,7 +1280,7 @@ impl<'a> PeepholeOptimizations {
}
}
/// `statementCaresAboutScope`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_parser.go#L9767>
/// `statementCaresAboutScope`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_parser/js_parser.go#L9754-L9768>
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,
}
@@ -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]