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 2e2af376a294f..ceb0203308721 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -328,6 +328,20 @@ impl<'a> PeepholeMinimizeConditions { } } + // `foo ? bar : foo` -> `foo && bar` + if let (Expression::Identifier(test_ident), Expression::Identifier(alternate_ident)) = + (&expr.test, &expr.alternate) + { + if test_ident.name == alternate_ident.name { + return Some(ctx.ast.expression_logical( + expr.span, + ctx.ast.move_expression(&mut expr.test), + LogicalOperator::And, + ctx.ast.move_expression(&mut expr.consequent), + )); + } + } + // `!a ? b() : c()` -> `a ? c() : b()` if let Expression::UnaryExpression(test_expr) = &mut expr.test { if test_expr.operator.is_not() { @@ -1509,4 +1523,12 @@ mod test { concat!("function x() {", " return new.target ? 1 : 2;", "}"), ); } + + #[test] + fn compress_conditional() { + test("foo ? foo : bar", "foo || bar"); + test("foo ? bar : foo", "foo && bar"); + test_same("x.y ? x.y : bar"); + test_same("x.y ? bar : x.y"); + } } diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index 0208c0c4bbe12..81c52126a0387 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -97,7 +97,6 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { Expression::NewExpression(e) => Self::try_fold_new_expression(e, ctx), Expression::TemplateLiteral(t) => Self::try_fold_template_literal(t, ctx), Expression::BinaryExpression(e) => Self::try_compress_typeof_undefined(e, ctx), - Expression::ConditionalExpression(e) => Self::try_compress_conditional(e, ctx), Expression::CallExpression(e) => { Self::try_fold_literal_constructor_call_expression(e, ctx) .or_else(|| Self::try_fold_simple_function_call(e, ctx)) @@ -266,37 +265,6 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { Some(ctx.ast.expression_binary(expr.span, left, new_comp_op, right)) } - fn try_compress_conditional( - expr: &mut ConditionalExpression<'a>, - ctx: Ctx<'a, 'b>, - ) -> Option> { - if let Expression::Identifier(ident_test) = &expr.test { - // `foo ? foo : bar` -> `foo || bar` - if let Expression::Identifier(ident_consequent) = &expr.consequent { - if ident_test.name == ident_consequent.name { - return Some(ctx.ast.expression_logical( - expr.span, - ctx.ast.move_expression(&mut expr.test), - LogicalOperator::Or, - ctx.ast.move_expression(&mut expr.alternate), - )); - } - } - // `foo ? bar : foo` -> `foo && bar` - if let Expression::Identifier(ident_alternate) = &expr.alternate { - if ident_test.name == ident_alternate.name { - return Some(ctx.ast.expression_logical( - expr.span, - ctx.ast.move_expression(&mut expr.test), - LogicalOperator::And, - ctx.ast.move_expression(&mut expr.consequent), - )); - } - } - } - None - } - /// Compress `foo === null || foo === undefined` into `foo == null`. /// /// `foo === null || foo === undefined` => `foo == null` @@ -1211,12 +1179,4 @@ mod test { test("typeof foo !== `number`", "typeof foo != 'number'"); test("`number` !== typeof foo", "typeof foo != 'number'"); } - - #[test] - fn compress_conditional() { - test("foo ? foo : bar", "foo || bar"); - test("foo ? bar : foo", "foo && bar"); - test_same("x.y ? x.y : bar"); - test_same("x.y ? bar : x.y"); - } }