Skip to content

Commit

Permalink
refactor(minifier): remove the buggy ?? transform
Browse files Browse the repository at this point in the history
e.g. `(a != null ? a : b);` is not `a ?? b`

There are also no unit tests.
  • Loading branch information
Boshen committed Jan 10, 2025
1 parent 0efc845 commit 02f1017
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 44 deletions.
37 changes: 3 additions & 34 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{ctx::Ctx, CompressorPass};
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
pub struct PeepholeMinimizeConditions {
#[allow(unused)]
target: ESTarget,
pub(crate) changed: bool,
}
Expand Down Expand Up @@ -589,39 +590,7 @@ impl<'a> PeepholeMinimizeConditions {
}
}

// Try using the "??" or "?." operators
if let Expression::BinaryExpression(bin_expr) = &mut expr.test {
if bin_expr.operator == BinaryOperator::Equality
|| bin_expr.operator == BinaryOperator::Inequality
{
if let Some(check) = {
if bin_expr.left.is_null() {
Some(&bin_expr.right)
} else {
Some(&bin_expr.left)
}
} {
// `a != null ? a : b` -> `a ?? b``
if check.content_eq(if bin_expr.operator == BinaryOperator::Equality {
&expr.alternate
} else {
&expr.consequent
}) && self.target >= ESTarget::ES2020
// TODO: this is probably a but too aggressive
&& matches!(check, Expression::Identifier(_))
{
return Some(ctx.ast.expression_logical(
SPAN,
ctx.ast.move_expression(&mut expr.consequent),
LogicalOperator::Coalesce,
ctx.ast.move_expression(&mut expr.alternate),
));
}

// TODO: `a != null ? a.b.c[d](e) : undefined` -> `a?.b.c[d](e)``
}
}
}
// TODO: Try using the "??" or "?." operators

// Non esbuild optimizations

Expand Down Expand Up @@ -2023,8 +1992,8 @@ mod test {
test("var a; a ? b(c, d) : b(e, d)", "var a; b(a ? c : e, d)");
test("var a; a ? b(...c) : b(...e)", "var a; b(...a ? c : e)");
test("var a; a ? b(c) : b(e)", "var a; b(a ? c : e)");
// test("a != null ? a : b", "a ?? b");
test("a() != null ? a() : b", "a() == null ? b : a()");
// test("a != null ? a : b", "a ?? b");
// test("a != null ? a.b.c[d](e) : undefined", "a?.b.c[d](e)");
}

Expand Down
20 changes: 10 additions & 10 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.61 kB | 8.54 kB | react.development.js

173.90 kB | 59.77 kB | 59.82 kB | 19.40 kB | 19.33 kB | moment.js
173.90 kB | 59.79 kB | 59.82 kB | 19.41 kB | 19.33 kB | moment.js

287.63 kB | 90.03 kB | 90.07 kB | 32.02 kB | 31.95 kB | jquery.js
287.63 kB | 90.09 kB | 90.07 kB | 32.03 kB | 31.95 kB | jquery.js

342.15 kB | 118.10 kB | 118.14 kB | 44.43 kB | 44.37 kB | vue.js
342.15 kB | 118.11 kB | 118.14 kB | 44.44 kB | 44.37 kB | vue.js

544.10 kB | 71.74 kB | 72.48 kB | 26.16 kB | 26.20 kB | lodash.js
544.10 kB | 71.76 kB | 72.48 kB | 26.15 kB | 26.20 kB | lodash.js

555.77 kB | 272.92 kB | 270.13 kB | 90.86 kB | 90.80 kB | d3.js
555.77 kB | 273.21 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js

1.01 MB | 460.13 kB | 458.89 kB | 126.75 kB | 126.71 kB | bundle.min.js
1.01 MB | 460.18 kB | 458.89 kB | 126.76 kB | 126.71 kB | bundle.min.js

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

2.14 MB | 726.02 kB | 724.14 kB | 180.01 kB | 181.07 kB | victory.js
2.14 MB | 726.27 kB | 724.14 kB | 180.09 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 331.67 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 331.78 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 492.60 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 492.64 kB | 488.28 kB | antd.js

10.95 MB | 3.49 MB | 3.49 MB | 907.27 kB | 915.50 kB | typescript.js
10.95 MB | 3.49 MB | 3.49 MB | 907.46 kB | 915.50 kB | typescript.js

0 comments on commit 02f1017

Please sign in to comment.