Skip to content

Commit

Permalink
feat(codegen): new Foo() -> new Foo when minify (#8077)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 23, 2024
1 parent 5397fe9 commit e3f78fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
36 changes: 21 additions & 15 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ impl GenExpr for StaticMemberExpression<'_> {

impl GenExpr for PrivateFieldExpression<'_> {
fn gen_expr(&self, p: &mut Codegen, _precedence: Precedence, ctx: Context) {
self.object.print_expr(p, Precedence::Prefix, ctx.intersection(Context::FORBID_CALL));
self.object.print_expr(p, Precedence::Postfix, ctx.intersection(Context::FORBID_CALL));
if self.optional {
p.print_str("?");
}
Expand Down Expand Up @@ -2240,23 +2240,29 @@ impl GenExpr for NewExpression<'_> {
p.print_annotation_comments(self.span.start);
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str("new ");
p.print_str("new");
p.print_soft_space();
self.callee.print_expr(p, Precedence::New, Context::FORBID_CALL);
p.print_ascii_byte(b'(');
let has_comment = (self.span.end > 0 && p.has_comment(self.span.end - 1))
|| self.arguments.iter().any(|item| p.has_comment(item.span().start));
if has_comment {
p.indent();
p.print_list_with_comments(&self.arguments, ctx);
// Handle `/* comment */);`
if self.span.end > 0 && !p.print_expr_comments(self.span.end - 1) {
p.print_soft_newline();

// Omit the "()" when minifying, but only when safe to do so
if !p.options.minify || !self.arguments.is_empty() || precedence >= Precedence::Postfix
{
p.print_ascii_byte(b'(');
let has_comment = (self.span.end > 0 && p.has_comment(self.span.end - 1))
|| self.arguments.iter().any(|item| p.has_comment(item.span().start));
if has_comment {
p.indent();
p.print_list_with_comments(&self.arguments, ctx);
// Handle `/* comment */);`
if self.span.end > 0 && !p.print_expr_comments(self.span.end - 1) {
p.print_soft_newline();
}
p.dedent();
} else {
p.print_list(&self.arguments, ctx);
}
p.dedent();
} else {
p.print_list(&self.arguments, ctx);
p.print_ascii_byte(b')');
}
p.print_ascii_byte(b')');
});
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_codegen/tests/integration/esbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ fn test_new() {
test("new (import('foo')[bar])", "new (import(\"foo\"))[bar]();\n");
test("new (import('foo'))[bar]", "new (import(\"foo\"))[bar]();\n");

// test_minify("new x", "new x;");
// test_minify("new x.y", "new x.y;");
// test_minify("(new x).y", "new x().y;");
// test_minify("new x().y", "new x().y;");
// test_minify("new x() + y", "new x+y;");
// test_minify("new x() ** 2", "new x**2;");
test_minify("new x", "new x;");
test_minify("new x.y", "new x.y;");
test_minify("(new x).y", "new x().y;");
test_minify("new x().y", "new x().y;");
test_minify("new x() + y", "new x+y;");
test_minify("new x() ** 2", "new x**2;");

// Test preservation of Webpack-specific comments
test(
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn module_decl() {
#[test]
fn expr() {
test("new (foo()).bar();", "new (foo()).bar();\n");
test_minify("x in new Error()", "x in new Error();");
test_minify("x in new Error()", "x in new Error;");

test("1000000000000000128.0.toFixed(0)", "0xde0b6b3a7640080.toFixed(0);\n");
test_minify("1000000000000000128.0.toFixed(0)", "0xde0b6b3a7640080.toFixed(0);");
Expand Down

0 comments on commit e3f78fb

Please sign in to comment.