Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_formatter): placements of line comments between function parentheses and body #1485

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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

### Formatter

#### Bug fixes

- Fix [#1172](https://github.com/biomejs/biome/issues/1172). Fix placement of line comment after function expression parentheses, they are now attached to first statement in body. Contributed by @kalleep

### JavaScript APIs

### Linter
Expand Down
42 changes: 18 additions & 24 deletions crates/biome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl CommentStyle for JsCommentStyle {
) -> CommentPlacement<Self::Language> {
match comment.text_position() {
CommentTextPosition::EndOfLine => handle_typecast_comment(comment)
.or_else(handle_function_declaration_comment)
.or_else(handle_function_comment)
.or_else(handle_conditional_comment)
.or_else(handle_if_statement_comment)
.or_else(handle_while_comment)
Expand All @@ -119,7 +119,7 @@ impl CommentStyle for JsCommentStyle {
.or_else(handle_after_arrow_fat_arrow_comment)
.or_else(handle_import_export_specifier_comment),
CommentTextPosition::OwnLine => handle_member_expression_comment(comment)
.or_else(handle_function_declaration_comment)
.or_else(handle_function_comment)
.or_else(handle_if_statement_comment)
.or_else(handle_while_comment)
.or_else(handle_try_comment)
Expand Down Expand Up @@ -641,38 +641,32 @@ fn handle_member_expression_comment(
}
}

fn handle_function_declaration_comment(
comment: DecoratedComment<JsLanguage>,
) -> CommentPlacement<JsLanguage> {
let is_function_declaration = matches!(
fn handle_function_comment(comment: DecoratedComment<JsLanguage>) -> CommentPlacement<JsLanguage> {
if !matches!(
comment.enclosing_node().kind(),
JsSyntaxKind::JS_FUNCTION_DECLARATION
| JsSyntaxKind::JS_FUNCTION_EXPORT_DEFAULT_DECLARATION
);
| JsSyntaxKind::JS_FUNCTION_EXPRESSION
) || !comment.kind().is_line()
{
return CommentPlacement::Default(comment);
};

let following = match comment.following_node() {
Some(following) if is_function_declaration => following,
_ => return CommentPlacement::Default(comment),
let Some(body) = comment.following_node().and_then(JsFunctionBody::cast_ref) else {
return CommentPlacement::Default(comment);
};

// Make comments between the `)` token and the function body leading comments
// of the first non empty statement or dangling comments of the body.
// Make line comments between the `)` token and the function body leading comments
// of the first statement or dangling comments of the body.
// ```javascript
// function test() /* comment */ {
// function test() // comment
// {
// console.log("Hy");
// }
// ```
if let Some(body) = JsFunctionBody::cast_ref(following) {
match body
.statements()
.iter()
.find(|statement| !matches!(statement, AnyJsStatement::JsEmptyStatement(_)))
{
Some(first) => CommentPlacement::leading(first.into_syntax(), comment),
None => CommentPlacement::dangling(body.into_syntax(), comment),
}
} else {
CommentPlacement::Default(comment)
match body.statements().first() {
Some(first) => CommentPlacement::leading(first.into_syntax(), comment),
None => CommentPlacement::dangling(body.into_syntax(), comment),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,29 @@ function b() // leading comment

}


function c( //some comment
foo, bar,
) {}
) {}


(function d()
// a
{
return 42
});

function e()
// a
{
;
};

function f()
// a
{
a;
};

function h() /* a */ {
a;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,33 @@ function b() // leading comment

}


function c( //some comment
foo, bar,
) {}


(function d()
// a
{
return 42
});

function e()
// a
{
;
};

function f()
// a
{
a;
};

function h() /* a */ {
a;
};

```


Expand Down Expand Up @@ -65,6 +88,24 @@ function c(
foo,
bar,
) {}

(function d() {
// a
return 42;
});

function e() {
// a
}

function f() {
// a
a;
}

function h() /* a */ {
a;
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ function function_declaration()
return 42
}

// FIXME
// TODO: reformat issue
// (function named()
// // this is a function
// {
// return 42
// })();
(function named()
// this is a function
{
return 42
})();


// FIXME
// TODO: reformat issue
// (function ()
// // this is a function
// {
// return 42
// })();
(function ()
// this is a function
{
return 42
})();

/* anonymous declaration */
export default function ()
Expand All @@ -28,14 +24,12 @@ export default function ()
return 42
}

// FIXME
// TODO: reformat issue
a = {
foo()
// this is a function
{},

// bar: function()
// // this is a function
// {},
bar: function()
// this is a function
{},
}

This file was deleted.

4 changes: 4 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

### Formatter

#### Bug fixes

- Fix [#1172](https://github.com/biomejs/biome/issues/1172). Fix placement of line comment after function expression parentheses, they are now attached to first statement in body. Contributed by @kalleep

### JavaScript APIs

### Linter
Expand Down