mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 18:43:44 +00:00
fix(minifier): keep side effects in typeof comparisons (#24940)
Avoid removing `typeof f() == 1`.
This commit is contained in:
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()");
|
||||
|
||||
Reference in new issue
Block a user