Skip to content

Commit

Permalink
refactor(minifier): move all conditional minification logic to minimz…
Browse files Browse the repository at this point in the history
…e_conditions
  • Loading branch information
camc314 committed Jan 3, 2025
1 parent 51f4792 commit 35e6216
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
22 changes: 22 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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<Expression<'a>> {
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`
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 35e6216

Please sign in to comment.