From 0d89df9254b6b3ab50583c2550564b15c69e57ab Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 3 Jan 2025 19:17:50 +0800 Subject: [PATCH] refactor(minifier): move `foo ? bar : foo` to `MinimizeConditiosn` --- .../peephole_minimize_conditions.rs | 39 ++++++++++++------ .../peephole_substitute_alternate_syntax.rs | 40 ------------------- 2 files changed, 27 insertions(+), 52 deletions(-) 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..c5525f8abb2fb 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -313,18 +313,28 @@ impl<'a> PeepholeMinimizeConditions { ctx: &mut TraverseCtx<'a>, ) -> Option> { // `a ? a : b` -> `a || b` - if let (Expression::Identifier(test_ident), Expression::Identifier(consequent_ident)) = - (&expr.test, &expr.consequent) - { - if test_ident.name == consequent_ident.name { - let ident = ctx.ast.move_expression(&mut expr.test); - - return Some(ctx.ast.expression_logical( - expr.span, - ident, - LogicalOperator::Or, - ctx.ast.move_expression(&mut expr.alternate), - )); + 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), + )); + } } } @@ -729,6 +739,11 @@ mod test { fold_same("(x || true) && y()"); fold("(x || false) && y()", "x && y()"); + + 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"); } #[test] 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"); - } }