fix(minifier): keep side effects in typeof comparisons (#24940)

Avoid removing `typeof f() == 1`.
This commit is contained in:
sapphi-red committed 2026-07-26 14:04:41 +00:00
1 parent 4597d6f618
commit f7971f181f
2 files changed
+12 -1

No files matched your search

@@ -662,10 +662,14 @@ impl<'a> PeepholeOptimizations {
if let Expression::UnaryExpression(left) = &e.left
&& left.operator.is_typeof()
&& e.operator.is_equality()
&& !e.left.may_have_side_effects(ctx)
{
let right_ty = e.right.value_type(ctx);
if !right_ty.is_undetermined() && right_ty != ValueType::String {
if !right_ty.is_undetermined()
&& right_ty != ValueType::String
&& !e.right.may_have_side_effects(ctx)
{
let new_expr = Expression::new_boolean_literal(
e.span,
e.operator == BinaryOperator::Inequality
@@ -1303,6 +1303,13 @@ fn test_fold_invalid_typeof_comparison() {
fold("typeof foo === 'number'", "typeof foo == 'number'");
}
#[test]
fn test_fold_keep_side_effects_in_typeof_comparison() {
fold_same("typeof f() == 1");
fold("typeof f() === 'asd'", "typeof f() == 'asd'");
fold_same("typeof x === [f()]");
}
#[test]
fn test_issue_8782() {
fold("+(void unknown())", "+void unknown()");