From b6d16f4d8020ab57e0a5a621c014596a25d22097 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Mon, 6 Jan 2025 02:21:23 +0000 Subject: [PATCH] fix(codegen): print parenthesis on negative bigint lit when neccessary (#8258) closes #8257 i couldn't really write a test for this, as it only happens when codegen + minifier are used together. Regression should be prevented by the 262 suite in #8256 (failed CI run before i implemented this fix: https://github.com/oxc-project/oxc/actions/runs/12621310419/job/35168016825) --- crates/oxc_codegen/src/gen.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 5a9d864f549f2..2c1a961c79083 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1167,7 +1167,7 @@ impl GenExpr for Expression<'_> { Self::BooleanLiteral(lit) => lit.print(p, ctx), Self::NullLiteral(lit) => lit.print(p, ctx), Self::NumericLiteral(lit) => lit.print_expr(p, precedence, ctx), - Self::BigIntLiteral(lit) => lit.print(p, ctx), + Self::BigIntLiteral(lit) => lit.print_expr(p, precedence, ctx), Self::RegExpLiteral(lit) => lit.print(p, ctx), Self::StringLiteral(lit) => lit.print(p, ctx), Self::Identifier(ident) => ident.print(p, ctx), @@ -1306,16 +1306,20 @@ impl GenExpr for NumericLiteral<'_> { } } -impl Gen for BigIntLiteral<'_> { - fn gen(&self, p: &mut Codegen, _ctx: Context) { +impl GenExpr for BigIntLiteral<'_> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, _ctx: Context) { let raw = self.raw.as_str().cow_replace('_', ""); - if raw.starts_with('-') { - p.print_space_before_operator(Operator::Unary(UnaryOperator::UnaryNegation)); - } - p.print_space_before_identifier(); p.add_source_mapping(self.span); - p.print_str(&raw); + if !raw.starts_with('-') { + p.print_str(&raw); + } else if precedence >= Precedence::Prefix { + p.print_ascii_byte(b'('); + p.print_str(&raw); + p.print_ascii_byte(b')'); + } else { + p.print_str(&raw); + } } } @@ -3310,7 +3314,7 @@ impl Gen for TSLiteral<'_> { Self::BooleanLiteral(decl) => decl.print(p, ctx), Self::NullLiteral(decl) => decl.print(p, ctx), Self::NumericLiteral(decl) => decl.print_expr(p, Precedence::Lowest, ctx), - Self::BigIntLiteral(decl) => decl.print(p, ctx), + Self::BigIntLiteral(decl) => decl.print_expr(p, Precedence::Lowest, ctx), Self::RegExpLiteral(decl) => decl.print(p, ctx), Self::StringLiteral(decl) => decl.print(p, ctx), Self::TemplateLiteral(decl) => decl.print(p, ctx),