Skip to content

Commit aa927ab

Browse files
committed
fix(codegen): don't inject parenthesis in optional chaining within new expressions (#14170)
fixes #14127
1 parent 06b16a9 commit aa927ab

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

crates/oxc_codegen/src/gen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ impl Gen for Function<'_> {
690690
let n = p.code_len();
691691
let wrap = self.is_expression()
692692
&& ((p.start_of_stmt == n || p.start_of_default_export == n) || self.pife);
693+
let ctx = ctx.and_forbid_call(false);
693694
p.wrap(wrap, |p| {
694695
p.print_space_before_identifier();
695696
p.add_source_mapping(self.span);
@@ -2263,6 +2264,7 @@ impl Gen for Class<'_> {
22632264
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
22642265
let n = p.code_len();
22652266
let wrap = self.is_expression() && (p.start_of_stmt == n || p.start_of_default_export == n);
2267+
let ctx = ctx.and_forbid_call(false);
22662268
p.wrap(wrap, |p| {
22672269
p.enter_class();
22682270
p.print_decorators(&self.decorators, ctx);

crates/oxc_codegen/tests/integration/js.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ fn export_type() {
4242
fn expr() {
4343
test("new (foo()).bar();", "new (foo()).bar();\n");
4444
test_minify("x in new Error()", "x in new Error;");
45+
test(
46+
"new function() { let a = foo?.bar().baz; return a; }();",
47+
"new function() {\n\tlet a = foo?.bar().baz;\n\treturn a;\n}();\n",
48+
);
49+
test(
50+
"new class { foo() { let a = foo?.bar().baz; return a; } }();",
51+
"new class {\n\tfoo() {\n\t\tlet a = foo?.bar().baz;\n\t\treturn a;\n\t}\n}();\n",
52+
);
4553

4654
test("1000000000000000128.0.toFixed(0)", "0xde0b6b3a7640080.toFixed(0);\n");
4755
test_minify("1000000000000000128.0.toFixed(0)", "0xde0b6b3a7640080.toFixed(0);");

0 commit comments

Comments
 (0)