Skip to content

Commit

Permalink
feat(minifier): improve minimizing unary not expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Jan 5, 2025
1 parent a580c32 commit 0e5b5ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
59 changes: 34 additions & 25 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ impl<'a> PeepholeMinimizeConditions {
}
if let Expression::UnaryExpression(e1) = &mut expr.argument {
if e1.operator.is_not() {
// `!!!a` -> `!!a`
if let Expression::UnaryExpression(e2) = &mut e1.argument {
if e2.operator.is_not() {
expr.argument = ctx.ast.move_expression(&mut e2.argument);
return Some(ctx.ast.move_expression(&mut expr.argument));
}
}
// `!!a` -> `a` // ONLY in boolean contexts
if Self::is_in_boolean_context(ctx) {
return Some(ctx.ast.move_expression(&mut e1.argument));
}
}
}

let Expression::BinaryExpression(binary_expr) = &mut expr.argument else { return None };
let new_op = binary_expr.operator.equality_inverse_operator()?;
binary_expr.operator = new_op;
Expand Down Expand Up @@ -386,10 +393,6 @@ impl<'a> PeepholeMinimizeConditions {
}
(true, false) => {
let ident = ctx.ast.move_expression(&mut expr.test);

if Self::is_in_boolean_context(ctx) {
return Some(ident);
}
return Some(ctx.ast.expression_unary(
expr.span,
UnaryOperator::LogicalNot,
Expand All @@ -409,15 +412,11 @@ impl<'a> PeepholeMinimizeConditions {
let ident = ctx.ast.move_expression(&mut expr.test);
return Some(ctx.ast.expression_logical(
expr.span,
if Self::is_in_boolean_context(ctx) {
ident
} else {
ctx.ast.expression_unary(
SPAN,
UnaryOperator::LogicalNot,
ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, ident),
)
},
ctx.ast.expression_unary(
SPAN,
UnaryOperator::LogicalNot,
ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, ident),
),
LogicalOperator::Or,
ctx.ast.move_expression(&mut expr.alternate),
));
Expand Down Expand Up @@ -448,15 +447,11 @@ impl<'a> PeepholeMinimizeConditions {
let ident = ctx.ast.move_expression(&mut expr.test);
return Some(ctx.ast.expression_logical(
expr.span,
if Self::is_in_boolean_context(ctx) {
ident
} else {
ctx.ast.expression_unary(
SPAN,
UnaryOperator::LogicalNot,
ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, ident),
)
},
ctx.ast.expression_unary(
SPAN,
UnaryOperator::LogicalNot,
ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, ident),
),
LogicalOperator::And,
ctx.ast.move_expression(&mut expr.consequent),
));
Expand Down Expand Up @@ -851,10 +846,10 @@ mod test {

#[test]
fn test_minimize_expr_condition() {
fold("(x ? true : false) && y()", "x && y()");
fold("(x ? true : false) && y()", "!!x && y()");
fold("(x ? false : true) && y()", "!x && y()");
fold("(x ? true : y) && y()", "(x || y) && y()");
fold("(x ? y : false) && y()", "(x && y) && y()");
fold("(x ? true : y) && y()", "(!!x || y) && y()");
fold("(x ? y : false) && y()", "(!!x && y) && y()");
fold("var x; (x && true) && y()", "var x; x && y()");
fold("var x; (x && false) && y()", "var x; false && y()");
fold("(x && true) && y()", "x && y()");
Expand Down Expand Up @@ -1688,4 +1683,18 @@ mod test {
test("delete x !== false", "delete x");
test("delete x != false", "delete x");
}

#[test]
fn minimize_duplicate_nots() {
test("!!x", "x");
test("!!!x", "!x");
test("!!!!x", "x");
test("!!!(x && y)", "!(x && y)");
test_same("var k = () => { !!x; }");

test_same("var k = !!x;");
test_same("function k () { return !!x; }");
test_same("var k = () => { return !!x; }");
test_same("var k = () => !!x;");
}
}
18 changes: 9 additions & 9 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ Original | minified | minified | gzip | gzip | Fixture

173.90 kB | 59.87 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js

287.63 kB | 90.19 kB | 90.07 kB | 32.10 kB | 31.95 kB | jquery.js
287.63 kB | 90.18 kB | 90.07 kB | 32.10 kB | 31.95 kB | jquery.js

342.15 kB | 118.25 kB | 118.14 kB | 44.53 kB | 44.37 kB | vue.js
342.15 kB | 118.23 kB | 118.14 kB | 44.52 kB | 44.37 kB | vue.js

544.10 kB | 71.82 kB | 72.48 kB | 26.20 kB | 26.20 kB | lodash.js
544.10 kB | 71.80 kB | 72.48 kB | 26.18 kB | 26.20 kB | lodash.js

555.77 kB | 273.19 kB | 270.13 kB | 90.98 kB | 90.80 kB | d3.js
555.77 kB | 273.17 kB | 270.13 kB | 90.96 kB | 90.80 kB | d3.js

1.01 MB | 460.34 kB | 458.89 kB | 126.86 kB | 126.71 kB | bundle.min.js
1.01 MB | 460.34 kB | 458.89 kB | 126.85 kB | 126.71 kB | bundle.min.js

1.25 MB | 652.70 kB | 646.76 kB | 163.54 kB | 163.73 kB | three.js

2.14 MB | 726.21 kB | 724.14 kB | 180.20 kB | 181.07 kB | victory.js
2.14 MB | 726.18 kB | 724.14 kB | 180.17 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 331.90 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 331.88 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 492.87 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 492.84 kB | 488.28 kB | antd.js

10.95 MB | 3.50 MB | 3.49 MB | 909.30 kB | 915.50 kB | typescript.js
10.95 MB | 3.50 MB | 3.49 MB | 909.21 kB | 915.50 kB | typescript.js

0 comments on commit 0e5b5ec

Please sign in to comment.