diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index 377ebe6d69a62..d6052fa4b21fb 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -265,6 +265,20 @@ impl<'a> Expression<'a> { expr } + #[allow(missing_docs)] + pub fn into_chain_element(self) -> Option> { + 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(_)) diff --git a/crates/oxc_minifier/src/peephole/minimize_conditions.rs b/crates/oxc_minifier/src/peephole/minimize_conditions.rs index 53a8c70929309..d9bef3c5156f2 100644 --- a/crates/oxc_minifier/src/peephole/minimize_conditions.rs +++ b/crates/oxc_minifier/src/peephole/minimize_conditions.rs @@ -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), + ); + } + } } } } @@ -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` @@ -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;"); diff --git a/crates/oxc_minifier/tests/peephole/esbuild.rs b/crates/oxc_minifier/tests/peephole/esbuild.rs index e6c025f601719..b1f8df7f0cd11 100644 --- a/crates/oxc_minifier/tests/peephole/esbuild.rs +++ b/crates/oxc_minifier/tests/peephole/esbuild.rs @@ -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);"); @@ -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();"); diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 5bd6c27a469e0..7f91fdf3048c4 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -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