Skip to content

Commit

Permalink
fix(minifier): instanceof has error throwing side effect (#8378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 9, 2025
1 parent c1c0d71 commit c0a3dda
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/oxc_ecmascript/src/side_effects/check_for_state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ 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() {
return true;
}
let left = self.left.check_for_state_change(check_for_new_objects);
let right = self.right.check_for_state_change(check_for_new_objects);

left || right
if left {
return true;
}
self.right.check_for_state_change(check_for_new_objects)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ mod test {
fold("(function k() {}, k(), baz())", "k(), baz()");
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)");
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_syntax/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ impl BinaryOperator {
self == Self::In
}

/// Returns `true` if this is an [`In`](BinaryOperator::Instanceof) operator.
pub fn is_instance_of(self) -> bool {
self == Self::Instanceof
}

/// Returns `true` for any bitwise operator
#[rustfmt::skip]
pub fn is_bitwise(self) -> bool {
Expand Down

0 comments on commit c0a3dda

Please sign in to comment.