From f0b1ee5a06ad778b3ab1c095241eb002a8b9528f Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Thu, 26 Dec 2024 09:42:34 +0000 Subject: [PATCH] feat(minifier): minimize `if(!x) foo()` -> `x || foo()` (#8122) --- .../peephole_minimize_conditions.rs | 29 ++++++++++++++----- tasks/minsize/minsize.snap | 22 +++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs index 43f090e59be86..62eec660ef192 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -74,12 +74,26 @@ impl<'a> PeepholeMinimizeConditions { ) -> Option> { 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 @@ -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)}"); } diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index a311960b71cef..b9f1059afe8d7 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -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