Skip to content

Commit

Permalink
feat(codegen): minify arrow expr (x) => y -> x => y (#8078)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 24, 2024
1 parent 728ed20 commit 4727667
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
19 changes: 16 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4727667

Please sign in to comment.