From af65c3670f10bb59c852f329054af3c405bfa08e Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Mon, 6 Jan 2025 23:26:20 +0000 Subject: [PATCH] feat(minifier): minimize double negated binary expressions (#8304) --- .../peephole_minimize_conditions.rs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 976a591db..c0afb4088 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -100,6 +100,23 @@ impl<'a> PeepholeMinimizeConditions { if Self::is_in_boolean_context(ctx) { return Some(ctx.ast.move_expression(&mut e1.argument)); } + if let Expression::BinaryExpression(bin_expr) = &e1.argument { + if matches!( + bin_expr.operator, + BinaryOperator::Equality + | BinaryOperator::Inequality + | BinaryOperator::StrictEquality + | BinaryOperator::StrictInequality + | BinaryOperator::LessThan + | BinaryOperator::LessEqualThan + | BinaryOperator::GreaterThan + | BinaryOperator::GreaterEqualThan + | BinaryOperator::In + | BinaryOperator::Instanceof + ) { + return Some(ctx.ast.move_expression(&mut e1.argument)); + } + } } } @@ -1733,9 +1750,10 @@ mod test { } #[test] - fn minimize_nots_with_deletes() { + fn minimize_nots_with_binary_expressions() { test("!!delete x.y", "delete x.y"); test("!!!delete x.y", "!delete x.y"); test("!!!!delete x.y", "delete x.y"); + test("var k = !!(foo instanceof bar)", "var k = foo instanceof bar"); } }