Skip to content

Commit fda2142

Browse files
committed
fix(formatter): correct printing comments for logical expression (#14500)
1 parent 613ef97 commit fda2142

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

crates/oxc_formatter/src/write/binary_like_expression.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ use std::mem::transmute_copy;
33
use oxc_allocator::{Address, CloneIn, GetAddress};
44
use oxc_ast::{ast::*, precedence};
55
use oxc_span::GetSpan;
6-
use oxc_syntax::precedence::{GetPrecedence, Precedence};
6+
use oxc_syntax::{
7+
operator,
8+
precedence::{GetPrecedence, Precedence},
9+
};
710

811
use crate::{
912
Format,
1013
formatter::{FormatResult, Formatter, trivia::FormatTrailingComments},
1114
generated::ast_nodes::{AstNode, AstNodes},
15+
parentheses::NeedsParentheses,
1216
utils::format_node_without_trailing_comments::FormatNodeWithoutTrailingComments,
1317
};
1418

@@ -405,7 +409,12 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
405409
}
406410

407411
if *root {
408-
write!(f, FormatNodeWithoutTrailingComments(right))
412+
write!(f, FormatNodeWithoutTrailingComments(right))?;
413+
let comments = f
414+
.context()
415+
.comments()
416+
.comments_before(binary_like_expression.span().end);
417+
write!(f, FormatTrailingComments::Comments(comments))
409418
} else {
410419
write!(f, right)
411420
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
code || !escapeless && (true /* 1 */ || false /* 2 */)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
code || !escapeless && (true /* 1 */ || false /* 2 */)
6+
==================== Output ====================
7+
code || (!escapeless && (true /* 1 */ || false) /* 2 */);
8+
9+
===================== End =====================

0 commit comments

Comments
 (0)