fix(minifier): keep side effects when folding Number call (#24937)

Avoid compressing `Number([f(), 1])` to `NaN`.
This commit is contained in:
sapphi-red committed 2026-07-26 14:04:40 +00:00
1 parent a086d91c83
commit e19f183dad
2 files changed
+8 -4

No files matched your search

@@ -1136,10 +1136,12 @@ impl<'a> PeepholeOptimizations {
span,
match arg {
None => 0.0,
Some(arg) => match arg.to_number(ctx) {
Some(n) => n,
None => return,
},
Some(arg) => {
match arg.to_number(ctx).filter(|_| !arg.may_have_side_effects(ctx)) {
Some(n) => n,
None => return,
}
}
},
None,
NumberBase::Decimal,
@@ -757,6 +757,8 @@ fn test_fold_number_constructor() {
test("x = Number(true)", "x = 1");
test("x = Number(false)", "x = 0");
test("x = Number('foo')", "x = NaN");
test_same("x = Number(void f())");
test_same("x = Number([f(), 1])");
}
#[test]