mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(minifier): minimize !0 + null !== 1 -> !0 + null != 1 (#8332)
This commit is contained in:
parent
98f2b1ccd5
commit
e88a6bdd94
2 changed files with 10 additions and 4 deletions
|
|
@ -103,15 +103,20 @@ impl<'a> From<&BinaryExpression<'a>> for ValueType {
|
|||
fn from(e: &BinaryExpression<'a>) -> Self {
|
||||
match e.operator {
|
||||
BinaryOperator::Addition => {
|
||||
let left_ty = Self::from(&e.left);
|
||||
let right_ty = Self::from(&e.right);
|
||||
if left_ty == Self::String || right_ty == Self::String {
|
||||
let left = Self::from(&e.left);
|
||||
let right = Self::from(&e.right);
|
||||
if left == Self::Boolean
|
||||
&& matches!(right, Self::Undefined | Self::Null | Self::Number)
|
||||
{
|
||||
return Self::Number;
|
||||
}
|
||||
if left == Self::String || right == Self::String {
|
||||
return Self::String;
|
||||
}
|
||||
// There are some pretty weird cases for object types:
|
||||
// {} + [] === "0"
|
||||
// [] + {} === "[object Object]"
|
||||
if left_ty == Self::Object || right_ty == Self::Object {
|
||||
if left == Self::Object || right == Self::Object {
|
||||
return Self::Undetermined;
|
||||
}
|
||||
Self::Undetermined
|
||||
|
|
|
|||
|
|
@ -1755,6 +1755,7 @@ mod test {
|
|||
|
||||
test("(x = 1) === 1", "(x = 1) == 1");
|
||||
test("(x = 1) !== 1", "(x = 1) != 1");
|
||||
test("!0 + null !== 1", "!0 + null != 1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue