fix(minifier): do not constant fold 0 instanceof F (#8199)

This commit is contained in:
Boshen 2024-12-31 08:46:57 +00:00
parent bf266e13b0
commit 56b7f133fa
3 changed files with 14 additions and 14 deletions

View file

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

View file

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

View file

@ -1609,6 +1609,7 @@ mod test {
// An unknown value should never be folded.
test_same("x instanceof Foo");
test_same("0 instanceof Foo");
}
#[test]