Skip to content

Commit

Permalink
fix(js_formatter): fix wrong Indentation for leading comments in empt…
Browse files Browse the repository at this point in the history
…y statement (#858)
  • Loading branch information
TaKO8Ki authored Nov 23, 2023
1 parent c61b4b6 commit a69a094
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/biome_js_formatter/src/js/statements/empty_statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use biome_formatter::write;
use biome_formatter::{write, CstFormatContext};

use biome_js_syntax::{JsEmptyStatement, JsEmptyStatementFields, JsSyntaxKind};
use biome_rowan::{AstNode, SyntaxNodeOptionExt};
Expand All @@ -12,6 +12,18 @@ impl FormatNodeRule<JsEmptyStatement> for FormatJsEmptyStatement {
let JsEmptyStatementFields { semicolon_token } = node.as_fields();
let parent_kind = node.syntax().parent().kind();

let leading_comments_with_break = f
.context()
.comments()
.leading_comments(node.syntax())
.iter()
.any(|comment| comment.lines_before() > 0 || comment.kind().is_line());

if leading_comments_with_break {
write!(f, [hard_line_break()])?;
}
write!(f, [format_leading_comments(node.syntax())])?;

if matches!(
parent_kind,
Some(
Expand All @@ -30,4 +42,9 @@ impl FormatNodeRule<JsEmptyStatement> for FormatJsEmptyStatement {
write!(f, [format_removed(&semicolon_token?)])
}
}

fn fmt_leading_comments(&self, _: &JsEmptyStatement, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted inside of `fmt_fields`
Ok(())
}
}
8 changes: 8 additions & 0 deletions crates/biome_js_formatter/tests/specs/js/module/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ statement /* comment */;
"test" /*trailing*/ ;

/* EOF comment */

function foo() {
if (bar) {
foo
}
// empty statement leading comments
;
}
15 changes: 15 additions & 0 deletions crates/biome_js_formatter/tests/specs/js/module/comments.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ statement /* comment */;

/* EOF comment */

function foo() {
if (bar) {
foo
}
// empty statement leading comments
;
}

```


Expand Down Expand Up @@ -195,6 +203,13 @@ statement /* comment */;
("test") /*trailing*/;

/* EOF comment */

function foo() {
if (bar) {
foo;
}
// empty statement leading comments
}
```


0 comments on commit a69a094

Please sign in to comment.