From 3da33d364746bf723a45f4dff718c3c7e7a51840 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 13 Aug 2024 06:54:26 +0000 Subject: [PATCH] fix(codegen): missing parenthesis for `PrivateInExpression` (#4865) --- crates/oxc_codegen/src/gen.rs | 14 ++++++++------ crates/oxc_codegen/tests/integration/unit.rs | 8 +++++++- 2 files changed, 15 insertions(+), 7 deletions(-) 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]