Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/oxc_minifier/src/peephole/minimize_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tasks/track_memory_allocations/allocs_minifier.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading