Skip to content

Commit

Permalink
feat(minifier): compress a != null ? a.b : undefined to a?.b (#8802)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jan 31, 2025
1 parent 3ac5020 commit e353a01
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 15 deletions.
14 changes: 14 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ impl<'a> Expression<'a> {
expr
}

#[allow(missing_docs)]
pub fn into_chain_element(self) -> Option<ChainElement<'a>> {
match self {
Expression::StaticMemberExpression(e) => Some(ChainElement::StaticMemberExpression(e)),
Expression::ComputedMemberExpression(e) => {
Some(ChainElement::ComputedMemberExpression(e))
}
Expression::PrivateFieldExpression(e) => Some(ChainElement::PrivateFieldExpression(e)),
Expression::CallExpression(e) => Some(ChainElement::CallExpression(e)),
Expression::TSNonNullExpression(e) => Some(ChainElement::TSNonNullExpression(e)),
_ => None,
}
}

/// Returns `true` if this [`Expression`] is an [`IdentifierReference`].
pub fn is_identifier_reference(&self) -> bool {
matches!(self, Expression::Identifier(_))
Expand Down
114 changes: 112 additions & 2 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,23 @@ impl<'a> PeepholeOptimizations {
));
}

// TODO: try using optional chaining
// "a == null ? undefined : a.b.c[d](e)" => "a?.b.c[d](e)"
// "a != null ? a.b.c[d](e) : undefined" => "a?.b.c[d](e)"
let target_expr =
if is_negate { &expr.alternate } else { &expr.consequent };
if ctx.is_expression_undefined(target_expr) {
let expr_to_inject_optional_chaining =
if is_negate { &mut expr.consequent } else { &mut expr.alternate };
if Self::inject_optional_chaining_if_matched(
id_expr,
expr_to_inject_optional_chaining,
ctx,
) {
return Some(
ctx.ast.move_expression(expr_to_inject_optional_chaining),
);
}
}
}
}
}
Expand Down Expand Up @@ -678,6 +694,91 @@ impl<'a> PeepholeOptimizations {
ctx.ast.expression_logical(span, a, op, b)
}

/// Modify `expr` if that has `target_expr` as a parent, and returns true if modified.
///
/// For `target_expr` = `a`, `expr` = `a.b`, this function changes `expr` to `a?.b` and returns true.
fn inject_optional_chaining_if_matched(
target_expr: &Expression<'a>,
expr: &mut Expression<'a>,
ctx: Ctx<'a, '_>,
) -> bool {
fn inject(target_expr: &Expression<'_>, expr: &mut Expression<'_>) -> bool {
match expr {
Expression::StaticMemberExpression(e) => {
if e.object.content_eq(target_expr) {
e.optional = true;
return true;
}
if inject(target_expr, &mut e.object) {
return true;
}
}
Expression::ComputedMemberExpression(e) => {
if e.object.content_eq(target_expr) {
e.optional = true;
return true;
}
if inject(target_expr, &mut e.object) {
return true;
}
}
Expression::CallExpression(e) => {
if e.callee.content_eq(target_expr) {
e.optional = true;
return true;
}
if inject(target_expr, &mut e.callee) {
return true;
}
}
Expression::ChainExpression(e) => match &mut e.expression {
ChainElement::StaticMemberExpression(e) => {
if e.object.content_eq(target_expr) {
e.optional = true;
return true;
}
if inject(target_expr, &mut e.object) {
return true;
}
}
ChainElement::ComputedMemberExpression(e) => {
if e.object.content_eq(target_expr) {
e.optional = true;
return true;
}
if inject(target_expr, &mut e.object) {
return true;
}
}
ChainElement::CallExpression(e) => {
if e.callee.content_eq(target_expr) {
e.optional = true;
return true;
}
if inject(target_expr, &mut e.callee) {
return true;
}
}
_ => {}
},
_ => {}
}
false
}

if inject(target_expr, expr) {
if !matches!(expr, Expression::ChainExpression(_)) {
*expr = ctx.ast.expression_chain(
expr.span(),
ctx.ast.move_expression(expr).into_chain_element().unwrap(),
);
}
true
} else {
false
}
}

/// Merge `consequent` and `alternate` of `ConditionalExpression` inside.
///
/// - `x ? a = 0 : a = 1` -> `a = x ? 0 : 1`
Expand Down Expand Up @@ -2319,7 +2420,16 @@ mod test {
test("var a; a != null ? a : b", "var a; a ?? b");
test("a != null ? a : b", "a == null ? b : a"); // accessing global `a` may have a getter with side effects
test_es2019("var a; a != null ? a : b", "var a; a == null ? b : a");
// test("a != null ? a.b.c[d](e) : undefined", "a?.b.c[d](e)");
test("var a; a != null ? a.b.c[d](e) : undefined", "var a; a?.b.c[d](e)");
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
test(
"var a, undefined = 1; a != null ? a.b.c[d](e) : undefined",
"var a, undefined = 1; a == null ? undefined : a.b.c[d](e)",
);
test_es2019(
"var a; a != null ? a.b.c[d](e) : undefined",
"var a; a != null && a.b.c[d](e)",
);
test("cmp !== 0 ? cmp : (bar, cmp);", "cmp === 0 && bar, cmp;");
test("cmp === 0 ? cmp : (bar, cmp);", "cmp === 0 || bar, cmp;");
test("cmp !== 0 ? (bar, cmp) : cmp;", "cmp === 0 || bar, cmp;");
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_minifier/tests/peephole/esbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,6 @@ fn js_parser_test() {
"if (a) x: { if (b) break x } else return c",
"if (a) { x: if (b) break x;} else return c;",
);
}

#[test]
#[ignore]
fn test_ignored() {
test("let a; return a != null ? a.b : undefined", "let a;return a?.b;");
test("let a; return a != null ? a[b] : undefined", "let a;return a?.[b];");
test("let a; return a != null ? a(b) : undefined", "let a;return a?.(b);");
Expand All @@ -778,17 +773,22 @@ fn test_ignored() {
test("let a; return null == a ? undefined : a.b", "let a;return a?.b;");
test("let a; return null == a ? undefined : a[b]", "let a;return a?.[b];");
test("let a; return null == a ? undefined : a(b)", "let a;return a?.(b);");
test("return a != null ? a.b : undefined", "return a != null ? a.b : void 0;");
test("let a; return a != null ? a.b : null", "let a;return a != null ? a.b : null;");
test("let a; return a != null ? b.a : undefined", "let a;return a != null ? b.a : void 0;");
test("let a; return a != 0 ? a.b : undefined", "let a;return a != 0 ? a.b : void 0;");
test("let a; return a !== null ? a.b : undefined", "let a;return a !== null ? a.b : void 0;");
test("return a != null ? a.b : undefined", "return a == null ? void 0 : a.b;");
test("let a; return a != null ? a.b : null", "let a;return a == null ? null : a.b;");
test("let a; return a != null ? b.a : undefined", "let a;return a == null ? void 0 : b.a;");
test("let a; return a != 0 ? a.b : undefined", "let a;return a == 0 ? void 0 : a.b;");
test("let a; return a !== null ? a.b : undefined", "let a;return a === null ? void 0 : a.b;");
test("let a; return a != undefined ? a.b : undefined", "let a;return a?.b;");
test("let a; return a != null ? a?.b : undefined", "let a;return a?.b;");
test("let a; return a != null ? a.b.c[d](e) : undefined", "let a;return a?.b.c[d](e);");
test("let a; return a != null ? a?.b.c[d](e) : undefined", "let a;return a?.b.c[d](e);");
test("let a; return a != null ? a.b.c?.[d](e) : undefined", "let a;return a?.b.c?.[d](e);");
test("let a; return a != null ? a?.b.c?.[d](e) : undefined", "let a;return a?.b.c?.[d](e);");
}

#[test]
#[ignore]
fn test_ignored() {
test("a != null && a.b()", "a?.b();");
test("a == null || a.b()", "a?.b();");
test("null != a && a.b()", "a?.b();");
Expand Down
6 changes: 3 additions & 3 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Original | minified | minified | gzip | gzip | Fixture

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

2.14 MB | 718.64 kB | 724.14 kB | 162.14 kB | 181.07 kB | victory.js
2.14 MB | 718.61 kB | 724.14 kB | 162.14 kB | 181.07 kB | victory.js

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

6.69 MB | 2.30 MB | 2.31 MB | 469.23 kB | 488.28 kB | antd.js
6.69 MB | 2.30 MB | 2.31 MB | 468.88 kB | 488.28 kB | antd.js

10.95 MB | 3.37 MB | 3.49 MB | 864.49 kB | 915.50 kB | typescript.js
10.95 MB | 3.37 MB | 3.49 MB | 863.73 kB | 915.50 kB | typescript.js

0 comments on commit e353a01

Please sign in to comment.