diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index ee4875b3ca016..abffd4a087352 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1054,7 +1054,7 @@ impl<'a, const MINIFY: bool> GenExpr for Expression<'a> { Self::UpdateExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::UnaryExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::BinaryExpression(expr) => expr.gen_expr(p, precedence, ctx), - Self::PrivateInExpression(expr) => expr.gen(p, ctx), + Self::PrivateInExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::LogicalExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::ConditionalExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::AssignmentExpression(expr) => expr.gen_expr(p, precedence, ctx), @@ -1778,11 +1778,13 @@ impl Gen for BinaryOperator { } } -impl<'a, const MINIFY: bool> Gen for PrivateInExpression<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { - self.left.gen(p, ctx); - p.print_str(" in "); - self.right.gen_expr(p, Precedence::Shift, Context::empty()); +impl<'a, const MINIFY: bool> GenExpr for PrivateInExpression<'a> { + fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { + p.wrap(precedence >= Precedence::Compare, |p| { + self.left.gen(p, ctx); + p.print_str(" in "); + self.right.gen_expr(p, Precedence::Equals, Context::empty()); + }); } } diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index d638c6a99215f..5e7372cd9cd52 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -9,8 +9,14 @@ fn module_decl() { } #[test] -fn new_expr() { +fn expr() { test("new (foo()).bar();", "new (foo()).bar();\n"); + test( + "class Foo { #test + bar() { if (!(#test in Foo)) { } } + }", + "class Foo {\n\t#test;\n\tbar() {\n\t\tif (!(#test in Foo)) {}\n\t}\n}\n", + ); } #[test]