Skip to content

Commit

Permalink
feat(codegen): minify class method async*fn(){} (#8083)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 24, 2024
1 parent 55d6eb9 commit f8f067b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
34 changes: 23 additions & 11 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ impl Gen for IdentifierReference<'_> {

impl Gen for IdentifierName<'_> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str(self.name.as_str());
}
Expand Down Expand Up @@ -2665,36 +2666,47 @@ impl Gen for MethodDefinition<'_> {
decorator.print(p, ctx);
p.print_hard_space();
}

if let Some(accessibility) = &self.accessibility {
p.print_space_before_identifier();
p.print_str(accessibility.as_str());
p.print_hard_space();
p.print_soft_space();
}
if self.r#type == MethodDefinitionType::TSAbstractMethodDefinition {
p.print_str("abstract ");
p.print_space_before_identifier();
p.print_str("abstract");
p.print_soft_space();
}
if self.r#static {
p.print_str("static ");
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str("static");
p.print_soft_space();
}

match &self.kind {
MethodDefinitionKind::Constructor | MethodDefinitionKind::Method => {}
MethodDefinitionKind::Get => {
p.print_str("get ");
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str("get");
p.print_soft_space();
}
MethodDefinitionKind::Set => {
p.print_str("set ");
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str("set");
p.print_soft_space();
}
}

if self.value.r#async {
p.print_str("async ");
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str("async");
p.print_soft_space();
}

if self.value.generator {
p.add_source_mapping(self.span);
p.print_str("*");
}

if self.computed {
p.print_ascii_byte(b'[');
}
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ fn private_in() {
}

#[test]
fn access_property() {
fn class() {
test(
"export default class Foo { @x @y accessor #aDef = 1 }",
"export default class Foo {\n\t@x @y accessor #aDef = 1;\n}\n",
);
test_minify("class F { static async * foo () {} }", "class F{static async*foo(){}}");
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Original | minified | minified | gzip | gzip | Fixture

1.01 MB | 467.00 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js

1.25 MB | 661.59 kB | 646.76 kB | 163.94 kB | 163.73 kB | three.js
1.25 MB | 661.60 kB | 646.76 kB | 163.94 kB | 163.73 kB | three.js

2.14 MB | 740.48 kB | 724.14 kB | 181.35 kB | 181.07 kB | victory.js

3.20 MB | 1.02 MB | 1.01 MB | 332.01 kB | 331.56 kB | echarts.js

6.69 MB | 2.39 MB | 2.31 MB | 495.62 kB | 488.28 kB | antd.js

10.95 MB | 3.54 MB | 3.49 MB | 909.71 kB | 915.50 kB | typescript.js
10.95 MB | 3.54 MB | 3.49 MB | 909.70 kB | 915.50 kB | typescript.js

0 comments on commit f8f067b

Please sign in to comment.