Skip to content

Commit 096651f

Browse files
committed
fix(formatter): block comments without a trailing linebreak that come after the ? operator in a conditional expression should be printed as-is
1 parent 2a1de04 commit 096651f

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

crates/oxc_formatter/src/utils/conditional.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ fn format_trailing_comments<'a>(
144144
if source_text.contains_newline_between(start, comment.span.start) {
145145
return &comments[..index];
146146
}
147-
// If this comment is a line comment, then it is a end of line comment, so we stop here and return the comments with this comment
148-
else if comment.is_line() {
147+
// If this comment is a line comment or an end of line comment, so we stop here and return the comments with this comment
148+
else if comment.is_line() || source_text.is_end_of_line_comment(comment) {
149149
return &comments[..=index];
150150
}
151151
// Store the index of the comment before the operator, if no line comment or no new line is found, then return all comments before operator
152152
else if source_text.bytes_contain(start, comment.span.start, operator) {
153-
index_before_operator = Some(index + 1);
153+
index_before_operator = Some(index);
154154
}
155155

156156
// Update the start position for the next iteration
@@ -161,9 +161,7 @@ fn format_trailing_comments<'a>(
161161
};
162162

163163
let comments = get_comments(f);
164-
FormatTrailingComments::Comments(comments).fmt(f)?;
165-
166-
Ok(())
164+
FormatTrailingComments::Comments(comments).fmt(f)
167165
}
168166

169167
impl<'a> FormatConditionalLike<'a, '_> {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test /* comment1 */ ? /* comment2 */ consequent /* comment3 */ : /* comment4 */ alternative
2+
3+
test
4+
/* comment1 */ ? /* comment2 */ consequent
5+
/* comment3 */ : /* comment4 */ alternative
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
test /* comment1 */ ? /* comment2 */ consequent /* comment3 */ : /* comment4 */ alternative
6+
7+
test
8+
/* comment1 */ ? /* comment2 */ consequent
9+
/* comment3 */ : /* comment4 */ alternative
10+
11+
==================== Output ====================
12+
test /* comment1 */
13+
? /* comment2 */ consequent /* comment3 */
14+
: /* comment4 */ alternative;
15+
16+
test
17+
? /* comment1 */ /* comment2 */ consequent
18+
: /* comment3 */ /* comment4 */ alternative;
19+
20+
===================== End =====================

0 commit comments

Comments
 (0)