Skip to content

Commit e353a01

Browse files
committed
feat(minifier): compress a != null ? a.b : undefined to a?.b (#8802)
1 parent 3ac5020 commit e353a01

File tree

4 files changed

+139
-15
lines changed

4 files changed

+139
-15
lines changed

crates/oxc_ast/src/ast_impl/js.rs

+14
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ impl<'a> Expression<'a> {
265265
expr
266266
}
267267

268+
#[allow(missing_docs)]
269+
pub fn into_chain_element(self) -> Option<ChainElement<'a>> {
270+
match self {
271+
Expression::StaticMemberExpression(e) => Some(ChainElement::StaticMemberExpression(e)),
272+
Expression::ComputedMemberExpression(e) => {
273+
Some(ChainElement::ComputedMemberExpression(e))
274+
}
275+
Expression::PrivateFieldExpression(e) => Some(ChainElement::PrivateFieldExpression(e)),
276+
Expression::CallExpression(e) => Some(ChainElement::CallExpression(e)),
277+
Expression::TSNonNullExpression(e) => Some(ChainElement::TSNonNullExpression(e)),
278+
_ => None,
279+
}
280+
}
281+
268282
/// Returns `true` if this [`Expression`] is an [`IdentifierReference`].
269283
pub fn is_identifier_reference(&self) -> bool {
270284
matches!(self, Expression::Identifier(_))

crates/oxc_minifier/src/peephole/minimize_conditions.rs

+112-2
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,23 @@ impl<'a> PeepholeOptimizations {
612612
));
613613
}
614614

615-
// TODO: try using optional chaining
615+
// "a == null ? undefined : a.b.c[d](e)" => "a?.b.c[d](e)"
616+
// "a != null ? a.b.c[d](e) : undefined" => "a?.b.c[d](e)"
617+
let target_expr =
618+
if is_negate { &expr.alternate } else { &expr.consequent };
619+
if ctx.is_expression_undefined(target_expr) {
620+
let expr_to_inject_optional_chaining =
621+
if is_negate { &mut expr.consequent } else { &mut expr.alternate };
622+
if Self::inject_optional_chaining_if_matched(
623+
id_expr,
624+
expr_to_inject_optional_chaining,
625+
ctx,
626+
) {
627+
return Some(
628+
ctx.ast.move_expression(expr_to_inject_optional_chaining),
629+
);
630+
}
631+
}
616632
}
617633
}
618634
}
@@ -678,6 +694,91 @@ impl<'a> PeepholeOptimizations {
678694
ctx.ast.expression_logical(span, a, op, b)
679695
}
680696

697+
/// Modify `expr` if that has `target_expr` as a parent, and returns true if modified.
698+
///
699+
/// For `target_expr` = `a`, `expr` = `a.b`, this function changes `expr` to `a?.b` and returns true.
700+
fn inject_optional_chaining_if_matched(
701+
target_expr: &Expression<'a>,
702+
expr: &mut Expression<'a>,
703+
ctx: Ctx<'a, '_>,
704+
) -> bool {
705+
fn inject(target_expr: &Expression<'_>, expr: &mut Expression<'_>) -> bool {
706+
match expr {
707+
Expression::StaticMemberExpression(e) => {
708+
if e.object.content_eq(target_expr) {
709+
e.optional = true;
710+
return true;
711+
}
712+
if inject(target_expr, &mut e.object) {
713+
return true;
714+
}
715+
}
716+
Expression::ComputedMemberExpression(e) => {
717+
if e.object.content_eq(target_expr) {
718+
e.optional = true;
719+
return true;
720+
}
721+
if inject(target_expr, &mut e.object) {
722+
return true;
723+
}
724+
}
725+
Expression::CallExpression(e) => {
726+
if e.callee.content_eq(target_expr) {
727+
e.optional = true;
728+
return true;
729+
}
730+
if inject(target_expr, &mut e.callee) {
731+
return true;
732+
}
733+
}
734+
Expression::ChainExpression(e) => match &mut e.expression {
735+
ChainElement::StaticMemberExpression(e) => {
736+
if e.object.content_eq(target_expr) {
737+
e.optional = true;
738+
return true;
739+
}
740+
if inject(target_expr, &mut e.object) {
741+
return true;
742+
}
743+
}
744+
ChainElement::ComputedMemberExpression(e) => {
745+
if e.object.content_eq(target_expr) {
746+
e.optional = true;
747+
return true;
748+
}
749+
if inject(target_expr, &mut e.object) {
750+
return true;
751+
}
752+
}
753+
ChainElement::CallExpression(e) => {
754+
if e.callee.content_eq(target_expr) {
755+
e.optional = true;
756+
return true;
757+
}
758+
if inject(target_expr, &mut e.callee) {
759+
return true;
760+
}
761+
}
762+
_ => {}
763+
},
764+
_ => {}
765+
}
766+
false
767+
}
768+
769+
if inject(target_expr, expr) {
770+
if !matches!(expr, Expression::ChainExpression(_)) {
771+
*expr = ctx.ast.expression_chain(
772+
expr.span(),
773+
ctx.ast.move_expression(expr).into_chain_element().unwrap(),
774+
);
775+
}
776+
true
777+
} else {
778+
false
779+
}
780+
}
781+
681782
/// Merge `consequent` and `alternate` of `ConditionalExpression` inside.
682783
///
683784
/// - `x ? a = 0 : a = 1` -> `a = x ? 0 : 1`
@@ -2319,7 +2420,16 @@ mod test {
23192420
test("var a; a != null ? a : b", "var a; a ?? b");
23202421
test("a != null ? a : b", "a == null ? b : a"); // accessing global `a` may have a getter with side effects
23212422
test_es2019("var a; a != null ? a : b", "var a; a == null ? b : a");
2322-
// test("a != null ? a.b.c[d](e) : undefined", "a?.b.c[d](e)");
2423+
test("var a; a != null ? a.b.c[d](e) : undefined", "var a; a?.b.c[d](e)");
2424+
test("a != null ? a.b.c[d](e) : undefined", "a != null && a.b.c[d](e)"); // accessing global `a` may have a getter with side effects
2425+
test(
2426+
"var a, undefined = 1; a != null ? a.b.c[d](e) : undefined",
2427+
"var a, undefined = 1; a == null ? undefined : a.b.c[d](e)",
2428+
);
2429+
test_es2019(
2430+
"var a; a != null ? a.b.c[d](e) : undefined",
2431+
"var a; a != null && a.b.c[d](e)",
2432+
);
23232433
test("cmp !== 0 ? cmp : (bar, cmp);", "cmp === 0 && bar, cmp;");
23242434
test("cmp === 0 ? cmp : (bar, cmp);", "cmp === 0 || bar, cmp;");
23252435
test("cmp !== 0 ? (bar, cmp) : cmp;", "cmp === 0 || bar, cmp;");

crates/oxc_minifier/tests/peephole/esbuild.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,6 @@ fn js_parser_test() {
761761
"if (a) x: { if (b) break x } else return c",
762762
"if (a) { x: if (b) break x;} else return c;",
763763
);
764-
}
765-
766-
#[test]
767-
#[ignore]
768-
fn test_ignored() {
769764
test("let a; return a != null ? a.b : undefined", "let a;return a?.b;");
770765
test("let a; return a != null ? a[b] : undefined", "let a;return a?.[b];");
771766
test("let a; return a != null ? a(b) : undefined", "let a;return a?.(b);");
@@ -778,17 +773,22 @@ fn test_ignored() {
778773
test("let a; return null == a ? undefined : a.b", "let a;return a?.b;");
779774
test("let a; return null == a ? undefined : a[b]", "let a;return a?.[b];");
780775
test("let a; return null == a ? undefined : a(b)", "let a;return a?.(b);");
781-
test("return a != null ? a.b : undefined", "return a != null ? a.b : void 0;");
782-
test("let a; return a != null ? a.b : null", "let a;return a != null ? a.b : null;");
783-
test("let a; return a != null ? b.a : undefined", "let a;return a != null ? b.a : void 0;");
784-
test("let a; return a != 0 ? a.b : undefined", "let a;return a != 0 ? a.b : void 0;");
785-
test("let a; return a !== null ? a.b : undefined", "let a;return a !== null ? a.b : void 0;");
776+
test("return a != null ? a.b : undefined", "return a == null ? void 0 : a.b;");
777+
test("let a; return a != null ? a.b : null", "let a;return a == null ? null : a.b;");
778+
test("let a; return a != null ? b.a : undefined", "let a;return a == null ? void 0 : b.a;");
779+
test("let a; return a != 0 ? a.b : undefined", "let a;return a == 0 ? void 0 : a.b;");
780+
test("let a; return a !== null ? a.b : undefined", "let a;return a === null ? void 0 : a.b;");
786781
test("let a; return a != undefined ? a.b : undefined", "let a;return a?.b;");
787782
test("let a; return a != null ? a?.b : undefined", "let a;return a?.b;");
788783
test("let a; return a != null ? a.b.c[d](e) : undefined", "let a;return a?.b.c[d](e);");
789784
test("let a; return a != null ? a?.b.c[d](e) : undefined", "let a;return a?.b.c[d](e);");
790785
test("let a; return a != null ? a.b.c?.[d](e) : undefined", "let a;return a?.b.c?.[d](e);");
791786
test("let a; return a != null ? a?.b.c?.[d](e) : undefined", "let a;return a?.b.c?.[d](e);");
787+
}
788+
789+
#[test]
790+
#[ignore]
791+
fn test_ignored() {
792792
test("a != null && a.b()", "a?.b();");
793793
test("a == null || a.b()", "a?.b();");
794794
test("null != a && a.b()", "a?.b();");

tasks/minsize/minsize.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ Original | minified | minified | gzip | gzip | Fixture
1717

1818
1.25 MB | 650.36 kB | 646.76 kB | 161.02 kB | 163.73 kB | three.js
1919

20-
2.14 MB | 718.64 kB | 724.14 kB | 162.14 kB | 181.07 kB | victory.js
20+
2.14 MB | 718.61 kB | 724.14 kB | 162.14 kB | 181.07 kB | victory.js
2121

2222
3.20 MB | 1.01 MB | 1.01 MB | 324.31 kB | 331.56 kB | echarts.js
2323

24-
6.69 MB | 2.30 MB | 2.31 MB | 469.23 kB | 488.28 kB | antd.js
24+
6.69 MB | 2.30 MB | 2.31 MB | 468.88 kB | 488.28 kB | antd.js
2525

26-
10.95 MB | 3.37 MB | 3.49 MB | 864.49 kB | 915.50 kB | typescript.js
26+
10.95 MB | 3.37 MB | 3.49 MB | 863.73 kB | 915.50 kB | typescript.js
2727

0 commit comments

Comments
 (0)