Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ impl FormatElements for [FormatElement<'_>] {
return true;
}
}

FormatElement::Line(line) if line.will_break() => {
return true;
}
element if ignore_depth == 0 && element.will_break() => {
return true;
}
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_formatter/src/formatter/format_element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ impl LineMode {
pub const fn is_hard(self) -> bool {
matches!(self, LineMode::Hard)
}

pub const fn will_break(self) -> bool {
matches!(self, LineMode::Hard | LineMode::Empty)
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -240,7 +244,7 @@ impl FormatElements for FormatElement<'_> {
match self {
FormatElement::ExpandParent => true,
FormatElement::Tag(Tag::StartGroup(group)) => !group.mode().is_flat(),
FormatElement::Line(line_mode) => matches!(line_mode, LineMode::Hard | LineMode::Empty),
FormatElement::Line(line_mode) => line_mode.will_break(),
FormatElement::StaticText { text } | FormatElement::DynamicText { text } => {
text.contains('\n')
}
Expand Down
49 changes: 24 additions & 25 deletions crates/oxc_formatter/src/formatter/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,36 +207,35 @@ impl<'a> Format<'a> for FormatTrailingComments<'a> {
// trailing comment for `2`. We can simulate the above by checking
// if this a comment on its own line; normal trailing comments are
// always at the end of another expression.
if total_lines_before > 0 {
if total_lines_before > 0
|| previous_comment.is_some_and(|comment| comment.is_line())
{
write!(
f,
[
line_suffix(&format_with(|f| {
match lines_before {
_ if should_nestle => {}
0 => {
// If the comment is immediately following a block-like comment,
// then it can stay on the same line with just a space between.
// Otherwise, it gets a hard break.
//
// [>* hello <] // hi
// [>*
// * docs
// */ [> still on the same line <]
if previous_comment.copied().is_some_and(Comment::is_line) {
write!(f, [hard_line_break()])?;
} else {
write!(f, [space()])?;
}
[line_suffix(&format_with(|f| {
match lines_before {
_ if should_nestle => {}
0 => {
// If the comment is immediately following a block-like comment,
// then it can stay on the same line with just a space between.
// Otherwise, it gets a hard break.
//
// [>* hello <] // hi
// [>*
// * docs
// */ [> still on the same line <]
if previous_comment.copied().is_some_and(Comment::is_line) {
write!(f, [hard_line_break()])?;
} else {
write!(f, [space()])?;
}
1 => write!(f, [hard_line_break()])?,
_ => write!(f, [empty_line()])?,
}
1 => write!(f, [hard_line_break()])?,
_ => write!(f, [empty_line()])?,
}

write!(f, [comment])
})),
expand_parent()
]
write!(f, [comment])
}))]
)?;
} else {
let content =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ for (let i of [
==================== Output ====================
for (let i in [
// comment1
1, 2,
3,
1, 2, 3,
// comment2
]);

for (let i of [
// comment1
1, 2,
3,
1, 2, 3,
// comment2
]);

Expand Down
Loading