fix(minifier): instanceof has error throwing side effect (#8378)

This commit is contained in:
Boshen 2025-01-09 06:49:27 +00:00
parent c1c0d71162
commit c0a3ddac28
3 changed files with 14 additions and 3 deletions

View file

@ -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)
}
}

View file

@ -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]

View file

@ -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 {