From 47276679f584d79b3d82243e44dd9cdb81878c6f Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 24 Dec 2024 14:25:01 +0000 Subject: [PATCH] feat(codegen): minify arrow expr `(x) => y` -> `x => y` (#8078) --- crates/oxc_codegen/src/gen.rs | 19 ++++++++++++++++--- crates/oxc_codegen/tests/integration/unit.rs | 16 ++++++++-------- tasks/minsize/minsize.snap | 6 +++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 5b20c8f385474..549624b3f1d98 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1791,9 +1791,22 @@ impl GenExpr for ArrowFunctionExpression<'_> { type_parameters.print(p, ctx); } p.add_source_mapping(self.span); - p.print_ascii_byte(b'('); - self.params.print(p, ctx); - p.print_ascii_byte(b')'); + let remove_params_wrap = p.options.minify + && self.params.items.len() == 1 + && self.params.rest.is_none() + && self.type_parameters.is_none() + && self.return_type.is_none() + && { + let param = &self.params.items[0]; + param.decorators.is_empty() + && !param.has_modifier() + && param.pattern.kind.is_binding_identifier() + && param.pattern.type_annotation.is_none() + && !param.pattern.optional + }; + p.wrap(!remove_params_wrap, |p| { + self.params.print(p, ctx); + }); if let Some(return_type) = &self.return_type { p.print_str(":"); p.print_soft_space(); diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index fa0434aed7fdc..1a8525f828e94 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -189,14 +189,14 @@ fn r#yield() { #[test] fn arrow() { - test_minify("x => a, b", "(x)=>a,b;"); - test_minify("x => (a, b)", "(x)=>(a,b);"); - test_minify("x => (a => b)", "(x)=>(a)=>b;"); - test_minify("x => y => a, b", "(x)=>(y)=>a,b;"); - test_minify("x => y => (a = b)", "(x)=>(y)=>a=b;"); - test_minify("x => y => z => a = b, c", "(x)=>(y)=>(z)=>a=b,c;"); - test_minify("x => y => z => a = (b, c)", "(x)=>(y)=>(z)=>a=(b,c);"); - test_minify("x => ({} + 0)", "(x)=>({})+0;"); + test_minify("x => a, b", "x=>a,b;"); + test_minify("x => (a, b)", "x=>(a,b);"); + test_minify("x => (a => b)", "x=>a=>b;"); + test_minify("x => y => a, b", "x=>y=>a,b;"); + test_minify("x => y => (a = b)", "x=>y=>a=b;"); + test_minify("x => y => z => a = b, c", "x=>y=>z=>a=b,c;"); + test_minify("x => y => z => a = (b, c)", "x=>y=>z=>a=(b,c);"); + test_minify("x => ({} + 0)", "x=>({})+0;"); } #[test] diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index d5f589c8b3033..76c69877c44b5 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -11,13 +11,13 @@ Original | minified | minified | gzip | gzip | Fixture 544.10 kB | 73.32 kB | 72.48 kB | 26.13 kB | 26.20 kB | lodash.js -555.77 kB | 276.06 kB | 270.13 kB | 91.14 kB | 90.80 kB | d3.js +555.77 kB | 276.01 kB | 270.13 kB | 91.13 kB | 90.80 kB | d3.js -1.01 MB | 466.82 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js +1.01 MB | 466.62 kB | 458.89 kB | 126.69 kB | 126.71 kB | bundle.min.js 1.25 MB | 661.47 kB | 646.76 kB | 163.94 kB | 163.73 kB | three.js -2.14 MB | 740.44 kB | 724.14 kB | 181.35 kB | 181.07 kB | victory.js +2.14 MB | 740.43 kB | 724.14 kB | 181.34 kB | 181.07 kB | victory.js 3.20 MB | 1.02 MB | 1.01 MB | 332.00 kB | 331.56 kB | echarts.js