diff --git a/crates/oxc_minifier/src/peephole/minimize_statements.rs b/crates/oxc_minifier/src/peephole/minimize_statements.rs index 8e0a43de71a71..1032abf5efed4 100644 --- a/crates/oxc_minifier/src/peephole/minimize_statements.rs +++ b/crates/oxc_minifier/src/peephole/minimize_statements.rs @@ -520,7 +520,10 @@ impl<'a> PeepholeOptimizations { break; }; if kind.name == id.name { - if decl.init.is_none() { + if decl.init.is_none() + && (decl.kind == VariableDeclarationKind::Var + || assign_expr.right.is_literal_value(true, ctx)) + { // "var a; a = b();" => "var a = b();" decl.init = Some(assign_expr.right.take_in(ctx.ast)); return true; @@ -530,6 +533,8 @@ impl<'a> PeepholeOptimizations { // This is not possible as we need to consider cases when `c()` accesses `a` // - "var a = 1; a = b();" => "var a = b();" // This is not possible as we need to consider cases when `b()` accesses `a` + // - "let a; a = foo(a);" => "let a = foo(a);" + // This is not possible as TDZ error would be introduced break; } // should not move assignment above variables with initializer to keep the execution order diff --git a/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs b/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs index 57e2a0f5513d6..7eece26583906 100644 --- a/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs +++ b/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs @@ -35,6 +35,9 @@ fn merge_assignments_to_declarations_let() { test_same("let a, b; a = c()"); // `c()` may access `b`, `let a = c(), b` will cause TDZ error test("let a, b; a = c(); b = d()", "let a, b; a = c(), b = d()"); // same as above test("let a, b; a = b", "let a, b; a = void 0"); // `let a = b, b` will cause TDZ error + test_same("let a; a = foo(a)"); // `let a = foo(a)` will cause TDZ error + test_same("let a; a = (() => a)()"); // `let a = (() => a)()` will cause TDZ error + test("let a; a = () => a", "let a = () => a"); } #[test] diff --git a/tasks/track_memory_allocations/allocs_minifier.snap b/tasks/track_memory_allocations/allocs_minifier.snap index 9090aa37f704f..f5b0a902b23c5 100644 --- a/tasks/track_memory_allocations/allocs_minifier.snap +++ b/tasks/track_memory_allocations/allocs_minifier.snap @@ -2,7 +2,7 @@ File | File size || Sys allocs | Sys reallocs | ------------------------------------------------------------------------------------------------------------------------------------------- RadixUIAdoptionSection.jsx | 2.52 kB || 75 | 4 || 20 | 6 | 688 B -pdf.mjs | 567.30 kB || 19577 | 2898 || 47403 | 7782 | 1.624 MB +pdf.mjs | 567.30 kB || 19577 | 2900 || 47405 | 7784 | 1.625 MB antd.js | 6.69 MB || 99854 | 13518 || 331725 | 70117 | 17.407 MB