From d53c4a9eba3bc3c4dbad9ea3c4f702c71ec92c75 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 10 Jan 2025 13:16:39 +0800 Subject: [PATCH] fix(minifier): `a in b` has error throwing side effect --- .../src/side_effects/check_for_state_change.rs | 7 +++---- .../src/ast_passes/peephole_remove_dead_code.rs | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/oxc_ecmascript/src/side_effects/check_for_state_change.rs b/crates/oxc_ecmascript/src/side_effects/check_for_state_change.rs index 331d1f46b9731d..94dd82117aac4b 100644 --- a/crates/oxc_ecmascript/src/side_effects/check_for_state_change.rs +++ b/crates/oxc_ecmascript/src/side_effects/check_for_state_change.rs @@ -1,5 +1,5 @@ use oxc_ast::ast::*; -use oxc_syntax::operator::UnaryOperator; +use oxc_syntax::operator::{BinaryOperator, UnaryOperator}; /// A "simple" operator is one whose children are expressions, has no direct side-effects. fn is_simple_unary_operator(operator: UnaryOperator) -> bool { @@ -63,7 +63,6 @@ impl<'a> CheckForStateChange<'a, '_> for Expression<'a> { if check_for_new_objects { return true; } - object_expr .properties .iter() @@ -95,8 +94,8 @@ impl<'a> CheckForStateChange<'a, '_> for UnaryExpression<'a> { impl<'a> CheckForStateChange<'a, '_> for BinaryExpression<'a> { fn check_for_state_change(&self, check_for_new_objects: bool) -> bool { - // `instanceof` can throw `TypeError` - if self.operator.is_instance_of() { + // `instanceof` and `in` can throw `TypeError` + if matches!(self.operator, BinaryOperator::In | BinaryOperator::Instanceof) { return true; } let left = self.left.check_for_state_change(check_for_new_objects); diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index 76e5db8c98a436..e26a3668375807 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -718,6 +718,7 @@ mod test { fold_same("(0, o.f)();"); fold("var obj = Object((null, 2, 3), 1, 2);", "var obj = Object(3, 1, 2);"); fold_same("(0 instanceof 0, foo)"); + fold_same("(0 in 0, foo)"); } #[test]