Skip to content

Commit

Permalink
feat(minifier): minimize !0 + null !== 1 -> !0 + null != 1 (#8332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 8, 2025
1 parent 98f2b1c commit e88a6bd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/oxc_ecmascript/src/constant_evaluation/value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit e88a6bd

Please sign in to comment.