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

feat(minifier): reverse negated conditional exprs #8205

Merged
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
41 changes: 27 additions & 14 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,39 @@ impl<'a> PeepholeMinimizeConditions {
}
}

/// `a ? a : b` -> `a || b`
fn try_minimize_conditional(
expr: &mut ConditionalExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
let Expression::Identifier(test_ident) = &expr.test else { return None };
let Expression::Identifier(consequent_ident) = &expr.consequent else { return None };

if test_ident.name != consequent_ident.name {
return None;
// `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),
));
}
}

let ident = ctx.ast.move_expression(&mut expr.test);
// `!x ? foo() : bar()` -> `x ? bar() : foo()`
if let Expression::UnaryExpression(test_expr) = &mut expr.test {
if test_expr.operator.is_not() {
let test = ctx.ast.move_expression(&mut test_expr.argument);
let consequent = ctx.ast.move_expression(&mut expr.consequent);
let alternate = ctx.ast.move_expression(&mut expr.alternate);
return Some(
ctx.ast.expression_conditional(expr.span, test, alternate, consequent),
);
}
}

Some(ctx.ast.expression_logical(
expr.span,
ident,
LogicalOperator::Or,
ctx.ast.move_expression(&mut expr.alternate),
))
None
}
}

Expand Down Expand Up @@ -696,7 +709,7 @@ mod test {
fold_same("x() ? x() : y()");
fold_same("x?.() ? x?.() : y()");

// fold("!x ? foo() : bar()", "x ? bar() : foo()");
fold("!x ? foo() : bar()", "x ? bar() : foo()");
// fold("while(!(x ? y : z)) foo();", "while(x ? !y : !z) foo();");
// fold("(x ? !y : !z) ? foo() : bar()", "(x ? y : z) ? bar() : foo()");
}
Expand Down
19 changes: 9 additions & 10 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,16 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
expr: &mut ConditionalExpression<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Expression<'a>> {
// Bail `let o = { f() { assert.ok(this !== o); } }; (true ? o.f : false)(); (true ? o.f : false)``;`
let parent = ctx.ancestry.parent();
if parent.is_tagged_template_expression()
|| matches!(parent, Ancestor::CallExpressionCallee(_))
{
return None;
}

match ctx.get_boolean_value(&expr.test) {
Some(true) => {
// Bail `let o = { f() { assert.ok(this !== o); } }; (true ? o.f : false)(); (true ? o.f : false)``;`
let parent = ctx.ancestry.parent();
if parent.is_tagged_template_expression()
|| matches!(parent, Ancestor::CallExpressionCallee(_))
{
return None;
}
Some(ctx.ast.move_expression(&mut expr.consequent))
}
Some(true) => Some(ctx.ast.move_expression(&mut expr.consequent)),
Some(false) => Some(ctx.ast.move_expression(&mut expr.alternate)),
None => None,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn tagged_template() {
test_same("(1, o.f)``");
test_same("(!0 && o.f)()");
test_same("(!0 && o.f)``");
test_same("(!0 ? o.f : !1)()");
test_same("(!0 ? o.f : !1)``");
test("(!0 ? o.f : !1)()", "(0 ? !1: o.f)()");
test("(!0 ? o.f : !1)``", "(0 ? !1: o.f)``");

test("foo(true && o.f)", "foo(o.f)");
test("foo(true ? o.f : false)", "foo(o.f)");
Expand Down
22 changes: 11 additions & 11 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ Original | minified | minified | gzip | gzip | Fixture
-------------------------------------------------------------------------------------
72.14 kB | 23.71 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js

173.90 kB | 59.91 kB | 59.82 kB | 19.45 kB | 19.33 kB | moment.js
173.90 kB | 59.90 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js

287.63 kB | 90.40 kB | 90.07 kB | 32.12 kB | 31.95 kB | jquery.js
287.63 kB | 90.39 kB | 90.07 kB | 32.12 kB | 31.95 kB | jquery.js

342.15 kB | 118.50 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js
342.15 kB | 118.48 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js

544.10 kB | 71.85 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
544.10 kB | 71.84 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js

555.77 kB | 273.49 kB | 270.13 kB | 90.96 kB | 90.80 kB | d3.js
555.77 kB | 273.48 kB | 270.13 kB | 90.94 kB | 90.80 kB | d3.js

1.01 MB | 460.80 kB | 458.89 kB | 126.93 kB | 126.71 kB | bundle.min.js
1.01 MB | 460.76 kB | 458.89 kB | 126.88 kB | 126.71 kB | bundle.min.js

1.25 MB | 653.19 kB | 646.76 kB | 163.58 kB | 163.73 kB | three.js
1.25 MB | 653.18 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js

2.14 MB | 726.75 kB | 724.14 kB | 180.29 kB | 181.07 kB | victory.js
2.14 MB | 726.72 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.21 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.13 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 493.07 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 493.04 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.37 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.11 kB | 915.50 kB | typescript.js

Loading