From 9b60616fbf57f96941face38e0cab2ce691255fa Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 8 Jan 2025 12:15:13 +0800 Subject: [PATCH] feat(minifier): minimize `!0 + null !== 1` -> `!0 + null != 1` --- .../src/constant_evaluation/value_type.rs | 13 +++++++++---- .../src/ast_passes/peephole_minimize_conditions.rs | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs b/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs index d2130876b9433..9243180470571 100644 --- a/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs +++ b/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs @@ -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 diff --git a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs index af72cb3d6fc52..43d7f6d58ef01 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -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]