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 ae644636c32db..ddecb66869dda 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -117,14 +117,21 @@ impl<'a> PeepholeMinimizeConditions { expr: &mut UnaryExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { - if expr.operator.is_not() { - if let Expression::UnaryExpression(e1) = &mut expr.argument { - if e1.operator.is_not() && ValueType::from(&e1.argument).is_boolean() { - return Some(ctx.ast.move_expression(&mut e1.argument)); - } + if !expr.operator.is_not() { + return None; + } + match &mut expr.argument { + Expression::UnaryExpression(e) + if e.operator.is_not() && ValueType::from(&e.argument).is_boolean() => + { + Some(ctx.ast.move_expression(&mut e.argument)) } + Expression::BinaryExpression(e) if e.operator.is_equality() => { + e.operator = e.operator.equality_inverse_operator().unwrap(); + Some(ctx.ast.move_expression(&mut expr.argument)) + } + _ => None, } - None } fn try_minimize_if( @@ -1968,6 +1975,9 @@ mod test { #[test] fn minimize_nots_with_binary_expressions() { + test("!(x === undefined)", "x !== undefined"); + test("!(typeof(x) === 'undefined')", "typeof x != 'undefined'"); + test("!(x === void 0)", "x !== void 0"); test("!!delete x.y", "delete x.y"); test("!!!delete x.y", "!delete x.y"); test("!!!!delete x.y", "delete x.y"); diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 484dfaefbdf5f..d8f80c668c751 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -1,7 +1,7 @@ | Oxc | ESBuild | Oxc | ESBuild | Original | minified | minified | gzip | gzip | Fixture ------------------------------------------------------------------------------------- -72.14 kB | 23.71 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js +72.14 kB | 23.70 kB | 23.70 kB | 8.60 kB | 8.54 kB | react.development.js 173.90 kB | 59.79 kB | 59.82 kB | 19.41 kB | 19.33 kB | moment.js