Skip to content

Commit

Permalink
avoid printing unnecessary parentheses for ChainExpression inside Log…
Browse files Browse the repository at this point in the history
…icalExpression
  • Loading branch information
Dunqing committed Nov 17, 2024
1 parent 20fe3cd commit dddea2a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crates/oxc_codegen/src/binary_expr_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,17 @@ impl<'a> BinaryExpressionVisitor<'a> {
let e = self.e;
self.operator = e.operator();

self.wrap = self.precedence >= self.operator.precedence()
let precedence_check =
// `a?.b && b?.c`
// ^^^^ Avoid printing unnecessary parentheses for `b?.c`,
// this only happens after transformation, because the
// transformed AST is a little bit different from parsed AST.
if matches!(self.precedence, Precedence::LogicalAnd) {
self.precedence > self.operator.precedence()
} else {
self.precedence >= self.operator.precedence()
};
self.wrap = precedence_check
|| (self.operator == BinaryishOperator::Binary(BinaryOperator::In)
&& self.ctx.intersects(Context::FORBID_IN));

Expand Down

0 comments on commit dddea2a

Please sign in to comment.