diff --git a/crates/oxc_codegen/src/context.rs b/crates/oxc_codegen/src/context.rs index 211bd8c26..7aaef02a3 100644 --- a/crates/oxc_codegen/src/context.rs +++ b/crates/oxc_codegen/src/context.rs @@ -4,7 +4,8 @@ bitflags! { #[derive(Debug, Clone, Copy)] pub struct Context: u8 { /// [In] - const In = 1 << 0; + const In = 1 << 0; + const FORBID_CALL = 1 << 1; } } @@ -20,12 +21,23 @@ impl Context { self.contains(Self::In) } + #[inline] + pub fn has_forbid_call(self) -> bool { + self.contains(Self::FORBID_CALL) + } + #[inline] #[must_use] pub fn and_in(self, include: bool) -> Self { self.and(Self::In, include) } + #[inline] + #[must_use] + pub fn and_forbid_call(self, include: bool) -> Self { + self.and(Self::FORBID_CALL, include) + } + #[inline] fn and(self, flag: Self, set: bool) -> Self { if set { diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index a46ea1a60..31acc50d7 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1254,7 +1254,9 @@ impl<'a, const MINIFY: bool> GenExpr for PrivateFieldExpression<'a> { impl<'a, const MINIFY: bool> GenExpr for CallExpression<'a> { fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { - p.wrap(precedence > self.precedence(), |p| { + let wrap = precedence > self.precedence() || ctx.has_forbid_call(); + let ctx = ctx.and_forbid_call(false); + p.wrap(wrap, |p| { self.callee.gen_expr(p, self.precedence(), ctx); if self.optional { p.print_str(b"?."); @@ -1800,7 +1802,7 @@ impl<'a, const MINIFY: bool> GenExpr for NewExpression<'a> { fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { p.wrap(precedence > self.precedence(), |p| { p.print_str(b"new "); - self.callee.gen_expr(p, self.precedence(), ctx); + self.callee.gen_expr(p, Precedence::NewWithoutArgs, ctx.and_forbid_call(true)); p.wrap(true, |p| { p.print_list(&self.arguments, ctx); }); diff --git a/crates/oxc_codegen/tests/mod.rs b/crates/oxc_codegen/tests/mod.rs index 1d3bb3900..b2a68675c 100644 --- a/crates/oxc_codegen/tests/mod.rs +++ b/crates/oxc_codegen/tests/mod.rs @@ -108,3 +108,8 @@ fn module_decl() { test("import {} from './foo.js' with {}", "import './foo.js' with {\n};\n"); test("export * from './foo.js' with {}", "export * from './foo.js' with {\n};\n"); } + +#[test] +fn new_expr() { + test("new (foo()).bar();", "new (foo()).bar();\n"); +}