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): minimize if(!x) foo() -> x || foo() #8122

Merged
merged 1 commit into from
Dec 26, 2024
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
29 changes: 21 additions & 8 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,26 @@ impl<'a> PeepholeMinimizeConditions {
) -> Option<Statement<'a>> {
let Statement::IfStatement(if_stmt) = stmt else { unreachable!() };
if if_stmt.alternate.is_none() {
// `if(x)foo();` -> `x&&foo();`
if let Some(right) = Self::is_foldable_express_block(&mut if_stmt.consequent, ctx) {
let left = ctx.ast.move_expression(&mut if_stmt.test);
let logical_expr =
ctx.ast.expression_logical(if_stmt.span, left, LogicalOperator::And, right);
return Some(ctx.ast.statement_expression(if_stmt.span, logical_expr));
let test = ctx.ast.move_expression(&mut if_stmt.test);
// `if(!x) foo()` -> `x || foo()`
if let Expression::UnaryExpression(unary_expr) = test {
if unary_expr.operator.is_not() {
let left = unary_expr.unbox().argument;
let logical_expr = ctx.ast.expression_logical(
if_stmt.span,
left,
LogicalOperator::Or,
right,
);
return Some(ctx.ast.statement_expression(if_stmt.span, logical_expr));
}
} else {
// `if(x) foo()` -> `x && foo()`
let logical_expr =
ctx.ast.expression_logical(if_stmt.span, test, LogicalOperator::And, right);
return Some(ctx.ast.statement_expression(if_stmt.span, logical_expr));
}
}
}
None
Expand Down Expand Up @@ -356,12 +370,11 @@ mod test {
}

#[test]
#[ignore]
fn test_not_cond() {
fold("function f(){if(!x)foo()}", "function f(){x||foo()}");
fold("function f(){if(!x)b=1}", "function f(){x||(b=1)}");
fold("if(!x)z=1;else if(y)z=2", "x ? y&&(z=2) : z=1;");
fold("if(x)y&&(z=2);else z=1;", "x ? y&&(z=2) : z=1");
// fold("if(!x)z=1;else if(y)z=2", "x ? y&&(z=2) : z=1;");
// fold("if(x)y&&(z=2);else z=1;", "x ? y&&(z=2) : z=1");
fold("function f(){if(!(x=1))a.b=1}", "function f(){(x=1)||(a.b=1)}");
}

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.89 kB | 23.70 kB | 8.64 kB | 8.54 kB | react.development.js

173.90 kB | 61.42 kB | 59.82 kB | 19.60 kB | 19.33 kB | moment.js
173.90 kB | 61.39 kB | 59.82 kB | 19.60 kB | 19.33 kB | moment.js

287.63 kB | 92.09 kB | 90.07 kB | 32.46 kB | 31.95 kB | jquery.js
287.63 kB | 92.05 kB | 90.07 kB | 32.44 kB | 31.95 kB | jquery.js

342.15 kB | 120.77 kB | 118.14 kB | 44.86 kB | 44.37 kB | vue.js
342.15 kB | 120.72 kB | 118.14 kB | 44.86 kB | 44.37 kB | vue.js

544.10 kB | 73.17 kB | 72.48 kB | 26.28 kB | 26.20 kB | lodash.js
544.10 kB | 73.15 kB | 72.48 kB | 26.28 kB | 26.20 kB | lodash.js

555.77 kB | 275.52 kB | 270.13 kB | 91.48 kB | 90.80 kB | d3.js
555.77 kB | 275.48 kB | 270.13 kB | 91.48 kB | 90.80 kB | d3.js

1.01 MB | 465.56 kB | 458.89 kB | 127.14 kB | 126.71 kB | bundle.min.js
1.01 MB | 465.45 kB | 458.89 kB | 127.12 kB | 126.71 kB | bundle.min.js

1.25 MB | 659.76 kB | 646.76 kB | 164.45 kB | 163.73 kB | three.js
1.25 MB | 659.74 kB | 646.76 kB | 164.45 kB | 163.73 kB | three.js

2.14 MB | 739.69 kB | 724.14 kB | 181.77 kB | 181.07 kB | victory.js
2.14 MB | 739.59 kB | 724.14 kB | 181.75 kB | 181.07 kB | victory.js

3.20 MB | 1.02 MB | 1.01 MB | 333.18 kB | 331.56 kB | echarts.js
3.20 MB | 1.02 MB | 1.01 MB | 332.98 kB | 331.56 kB | echarts.js

6.69 MB | 2.39 MB | 2.31 MB | 496.55 kB | 488.28 kB | antd.js
6.69 MB | 2.39 MB | 2.31 MB | 496.49 kB | 488.28 kB | antd.js

10.95 MB | 3.54 MB | 3.49 MB | 912.55 kB | 915.50 kB | typescript.js
10.95 MB | 3.54 MB | 3.49 MB | 912.49 kB | 915.50 kB | typescript.js

Loading