diff --git a/crates/oxc_formatter/src/write/binary_like_expression.rs b/crates/oxc_formatter/src/write/binary_like_expression.rs index 8ce81033b47de..f23ab40d3d617 100644 --- a/crates/oxc_formatter/src/write/binary_like_expression.rs +++ b/crates/oxc_formatter/src/write/binary_like_expression.rs @@ -3,12 +3,16 @@ use std::mem::transmute_copy; use oxc_allocator::{Address, CloneIn, GetAddress}; use oxc_ast::{ast::*, precedence}; use oxc_span::GetSpan; -use oxc_syntax::precedence::{GetPrecedence, Precedence}; +use oxc_syntax::{ + operator, + precedence::{GetPrecedence, Precedence}, +}; use crate::{ Format, formatter::{FormatResult, Formatter, trivia::FormatTrailingComments}, generated::ast_nodes::{AstNode, AstNodes}, + parentheses::NeedsParentheses, utils::format_node_without_trailing_comments::FormatNodeWithoutTrailingComments, }; @@ -405,7 +409,12 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> { } if *root { - write!(f, FormatNodeWithoutTrailingComments(right)) + write!(f, FormatNodeWithoutTrailingComments(right))?; + let comments = f + .context() + .comments() + .comments_before(binary_like_expression.span().end); + write!(f, FormatTrailingComments::Comments(comments)) } else { write!(f, right) } diff --git a/crates/oxc_formatter/tests/fixtures/js/comments/logical.js b/crates/oxc_formatter/tests/fixtures/js/comments/logical.js new file mode 100644 index 0000000000000..92617eaa84c76 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/comments/logical.js @@ -0,0 +1 @@ +code || !escapeless && (true /* 1 */ || false /* 2 */) \ No newline at end of file diff --git a/crates/oxc_formatter/tests/fixtures/js/comments/logical.js.snap b/crates/oxc_formatter/tests/fixtures/js/comments/logical.js.snap new file mode 100644 index 0000000000000..c807da5436896 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/comments/logical.js.snap @@ -0,0 +1,9 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +code || !escapeless && (true /* 1 */ || false /* 2 */) +==================== Output ==================== +code || (!escapeless && (true /* 1 */ || false) /* 2 */); + +===================== End =====================