Skip to content

Commit

Permalink
fix(minifier): do not constant fold 0 instanceof F (#8199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 31, 2024
1 parent bf266e1 commit 56b7f13
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
23 changes: 9 additions & 14 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,22 +352,17 @@ pub trait ConstantEvaluation<'a> {
if left.may_have_side_effects() {
return None;
}

let left_ty = ValueType::from(left);
if left_ty == ValueType::Undetermined {
return None;
}
if left_ty == ValueType::Object {
if let Some(right_ident) = right.get_identifier_reference() {
if right_ident.name == "Object" && self.is_global_reference(right_ident) {
return Some(ConstantValue::Boolean(true));
}
if let Some(right_ident) = right.get_identifier_reference() {
let name = right_ident.name.as_str();
if matches!(name, "Object" | "Number" | "Boolean" | "String")
&& self.is_global_reference(right_ident)
{
return Some(ConstantValue::Boolean(
name == "Object" && ValueType::from(left).is_object(),
));
}
None
} else {
// Non-object types are never instances.
Some(ConstantValue::Boolean(false))
}
None
}
_ => None,
}
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_ecmascript/src/constant_evaluation/value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl ValueType {
pub fn is_boolean(self) -> bool {
self == Self::Boolean
}

pub fn is_object(self) -> bool {
self == Self::Object
}
}

/// `get_known_value_type`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,7 @@ mod test {

// An unknown value should never be folded.
test_same("x instanceof Foo");
test_same("0 instanceof Foo");
}

#[test]
Expand Down

0 comments on commit 56b7f13

Please sign in to comment.