fix(minifier): keep side effects when folding const conditional exprs (#8591)

This commit is contained in:
camc314 2025-01-19 01:53:20 +00:00
parent 8f5be07ed6
commit 93d643e6a4

View file

@ -495,8 +495,24 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
}
match ctx.get_boolean_value(&expr.test) {
Some(true) => Some(ctx.ast.move_expression(&mut expr.consequent)),
Some(false) => Some(ctx.ast.move_expression(&mut expr.alternate)),
Some(v) => {
if expr.test.may_have_side_effects() {
let mut exprs = ctx.ast.vec_with_capacity(2);
exprs.push(ctx.ast.move_expression(&mut expr.test));
exprs.push(ctx.ast.move_expression(if v {
&mut expr.consequent
} else {
&mut expr.alternate
}));
Some(ctx.ast.expression_sequence(expr.span, exprs))
} else {
Some(ctx.ast.move_expression(if v {
&mut expr.consequent
} else {
&mut expr.alternate
}))
}
}
None => None,
}
}
@ -782,6 +798,14 @@ mod test {
test("if (true) {}", "");
}
#[test]
fn test_fold_conditional() {
test("true ? foo() : bar()", "foo()");
test("false ? foo() : bar()", "bar()");
test_same("foo() ? bar() : baz()");
test("foo && false ? foo() : bar()", "(foo && false, bar());");
}
#[test]
fn test_fold_iife() {
fold_same("var k = () => {}");