Skip to content

Commit a5b4f41

Browse files
committed
refactor(formatter): align printing trailing comments with Prettier
1 parent 48e65dd commit a5b4f41

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

crates/oxc_formatter/src/formatter/format_element/document.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ impl FormatElements for [FormatElement<'_>] {
605605
return true;
606606
}
607607
}
608-
608+
FormatElement::Line(line) if line.will_break() => {
609+
return true;
610+
}
609611
element if ignore_depth == 0 && element.will_break() => {
610612
return true;
611613
}

crates/oxc_formatter/src/formatter/format_element/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ impl LineMode {
103103
pub const fn is_hard(self) -> bool {
104104
matches!(self, LineMode::Hard)
105105
}
106+
107+
pub const fn will_break(self) -> bool {
108+
matches!(self, LineMode::Hard | LineMode::Empty)
109+
}
106110
}
107111

108112
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -240,7 +244,7 @@ impl FormatElements for FormatElement<'_> {
240244
match self {
241245
FormatElement::ExpandParent => true,
242246
FormatElement::Tag(Tag::StartGroup(group)) => !group.mode().is_flat(),
243-
FormatElement::Line(line_mode) => matches!(line_mode, LineMode::Hard | LineMode::Empty),
247+
FormatElement::Line(line_mode) => line_mode.will_break(),
244248
FormatElement::StaticText { text } | FormatElement::DynamicText { text } => {
245249
text.contains('\n')
246250
}

crates/oxc_formatter/src/formatter/trivia.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,36 +207,35 @@ impl<'a> Format<'a> for FormatTrailingComments<'a> {
207207
// trailing comment for `2`. We can simulate the above by checking
208208
// if this a comment on its own line; normal trailing comments are
209209
// always at the end of another expression.
210-
if total_lines_before > 0 {
210+
if total_lines_before > 0
211+
|| previous_comment.is_some_and(|comment| comment.is_line())
212+
{
211213
write!(
212214
f,
213-
[
214-
line_suffix(&format_with(|f| {
215-
match lines_before {
216-
_ if should_nestle => {}
217-
0 => {
218-
// If the comment is immediately following a block-like comment,
219-
// then it can stay on the same line with just a space between.
220-
// Otherwise, it gets a hard break.
221-
//
222-
// [>* hello <] // hi
223-
// [>*
224-
// * docs
225-
// */ [> still on the same line <]
226-
if previous_comment.copied().is_some_and(Comment::is_line) {
227-
write!(f, [hard_line_break()])?;
228-
} else {
229-
write!(f, [space()])?;
230-
}
215+
[line_suffix(&format_with(|f| {
216+
match lines_before {
217+
_ if should_nestle => {}
218+
0 => {
219+
// If the comment is immediately following a block-like comment,
220+
// then it can stay on the same line with just a space between.
221+
// Otherwise, it gets a hard break.
222+
//
223+
// [>* hello <] // hi
224+
// [>*
225+
// * docs
226+
// */ [> still on the same line <]
227+
if previous_comment.copied().is_some_and(Comment::is_line) {
228+
write!(f, [hard_line_break()])?;
229+
} else {
230+
write!(f, [space()])?;
231231
}
232-
1 => write!(f, [hard_line_break()])?,
233-
_ => write!(f, [empty_line()])?,
234232
}
233+
1 => write!(f, [hard_line_break()])?,
234+
_ => write!(f, [empty_line()])?,
235+
}
235236

236-
write!(f, [comment])
237-
})),
238-
expand_parent()
239-
]
237+
write!(f, [comment])
238+
}))]
240239
)?;
241240
} else {
242241
let content =

crates/oxc_formatter/tests/fixtures/js/comments/for-statements.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ for (let i of [
1616
==================== Output ====================
1717
for (let i in [
1818
// comment1
19-
1, 2,
20-
3,
19+
1, 2, 3,
2120
// comment2
2221
]);
2322

2423
for (let i of [
2524
// comment1
26-
1, 2,
27-
3,
25+
1, 2, 3,
2826
// comment2
2927
]);
3028

0 commit comments

Comments
 (0)