Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(minifier): move all conditional minification logic to minimze_conditions #8231

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Loading