mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(minifier): move all conditional minification logic to minimze_conditions (#8231)
@Boshen feel free to close this one if you want. 🙂 https://github.com/oxc-project/oxc/pull/8229 compresses some conditionals this PR moves compression of all conditionals into the same place
This commit is contained in:
parent
51f47926ef
commit
62f8fba98c
2 changed files with 22 additions and 40 deletions
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<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`
|
||||
|
|
@ -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…
Reference in a new issue