diff --git a/crates/oxc_formatter/src/write/mod.rs b/crates/oxc_formatter/src/write/mod.rs index 51ff8f9505de3..57b904de2f70a 100644 --- a/crates/oxc_formatter/src/write/mod.rs +++ b/crates/oxc_formatter/src/write/mod.rs @@ -836,14 +836,17 @@ impl<'a> FormatWrite<'a> for AstNode<'a, IfStatement<'a>> { } if has_dangling_comments { - write!( - f, - FormatDanglingComments::Comments { comments, indent: DanglingIndentMode::None } - )?; - if has_line_comment { + write!(f, FormatTrailingComments::Comments(comments))?; write!(f, hard_line_break())?; } else { + write!( + f, + FormatDanglingComments::Comments { + comments, + indent: DanglingIndentMode::None + } + )?; write!(f, space())?; } } diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/if.ts b/crates/oxc_formatter/tests/fixtures/ts/comments/if.ts new file mode 100644 index 0000000000000..fcb3f51b3df34 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/if.ts @@ -0,0 +1,8 @@ +if (true) {} + +// comment1 +else if (false) {} + +// comment2 + +else {} \ No newline at end of file diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/if.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/comments/if.ts.snap new file mode 100644 index 0000000000000..d840490facd5b --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/if.ts.snap @@ -0,0 +1,25 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +if (true) {} + +// comment1 +else if (false) {} + +// comment2 + +else {} +==================== Output ==================== +if (true) { +} + +// comment1 +else if (false) { +} + +// comment2 +else { +} + +===================== End =====================