Skip to content

Commit

Permalink
feat(codegen): minify more whitespace (#8089)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 24, 2024
1 parent 6355b7c commit 6237c05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
56 changes: 36 additions & 20 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl Gen for ForInStatement<'_> {
p.print_soft_space();
p.print_space_before_identifier();
p.print_str("in");
p.print_hard_space();
p.print_soft_space();
p.print_expression(&self.right);
p.print_ascii_byte(b')');
p.print_body(&self.body, false, ctx);
Expand Down Expand Up @@ -503,7 +503,7 @@ impl Gen for ContinueStatement<'_> {
p.print_indent();
p.print_str("continue");
if let Some(label) = &self.label {
p.print_hard_space();
p.print_soft_space();
label.print(p, ctx);
}
p.print_semicolon_after_statement();
Expand All @@ -516,7 +516,7 @@ impl Gen for BreakStatement<'_> {
p.print_indent();
p.print_str("break");
if let Some(label) = &self.label {
p.print_hard_space();
p.print_soft_space();
label.print(p, ctx);
}
p.print_semicolon_after_statement();
Expand Down Expand Up @@ -800,14 +800,17 @@ impl Gen for FormalParameter<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
for decorator in &self.decorators {
decorator.print(p, ctx);
p.print_hard_space();
p.print_soft_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.readonly {
p.print_str("readonly ");
p.print_space_before_identifier();
p.print_str("readonly");
p.print_soft_space();
}
self.pattern.print(p, ctx);
}
Expand Down Expand Up @@ -1237,6 +1240,7 @@ impl Gen for BindingIdentifier<'_> {

impl Gen for LabelIdentifier<'_> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
p.print_space_before_identifier();
p.add_source_mapping_for_name(self.span, &self.name);
p.print_str(self.name.as_str());
}
Expand Down Expand Up @@ -1843,7 +1847,7 @@ impl GenExpr for UnaryExpression<'_> {
if self.operator.is_keyword() {
p.print_space_before_identifier();
p.print_str(operator);
p.print_hard_space();
p.print_soft_space();
} else {
p.print_space_before_operator(self.operator.into());
p.print_str(operator);
Expand Down Expand Up @@ -2089,21 +2093,33 @@ impl Gen for AssignmentTargetPropertyIdentifier<'_> {

impl Gen for AssignmentTargetPropertyProperty<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
match &self.name {
PropertyKey::StaticIdentifier(ident) => {
ident.print(p, ctx);
}
PropertyKey::PrivateIdentifier(ident) => {
ident.print(p, ctx);
}
key @ match_expression!(PropertyKey) => {
p.print_ascii_byte(b'[');
key.to_expression().print_expr(p, Precedence::Comma, Context::empty());
p.print_ascii_byte(b']');
let omit_key = if p.options.minify {
let key_name = match &self.name {
PropertyKey::StaticIdentifier(ident) => Some(&ident.name),
_ => None,
};
let value_name = self.binding.name();
key_name.is_some() && value_name.is_some() && key_name == value_name.as_ref()
} else {
false
};
if !omit_key {
match &self.name {
PropertyKey::StaticIdentifier(ident) => {
ident.print(p, ctx);
}
PropertyKey::PrivateIdentifier(ident) => {
ident.print(p, ctx);
}
key @ match_expression!(PropertyKey) => {
p.print_ascii_byte(b'[');
key.to_expression().print_expr(p, Precedence::Comma, Context::empty());
p.print_ascii_byte(b']');
}
}
p.print_colon();
p.print_soft_space();
}
p.print_colon();
p.print_soft_space();
self.binding.print(p, ctx);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn expr() {
test_minify_same("await import(\"\");");

test("delete 2e308", "delete (0, Infinity);\n");
test_minify("delete 2e308", "delete (1/0);");
test_minify("delete 2e308", "delete(1/0);");
}

#[test]
Expand Down Expand Up @@ -154,7 +154,7 @@ fn assignment() {
test_minify("a /= () => {}", "a/=()=>{};");
test_minify("a %= async () => {}", "a%=async()=>{};");
test_minify("a -= (1, 2)", "a-=(1,2);");
test_minify("a >>= b >>= c", "a>>=b>>=c;");
test_minify("({ x: x = flag1 = true } = {})", "({x=flag1=true}={});");
}

#[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 @@ -13,15 +13,15 @@ Original | minified | minified | gzip | gzip | Fixture

555.77 kB | 276.06 kB | 270.13 kB | 91.14 kB | 90.80 kB | d3.js

1.01 MB | 466.83 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js
1.01 MB | 466.82 kB | 458.89 kB | 126.74 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

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

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

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

0 comments on commit 6237c05

Please sign in to comment.