Skip to content

Commit

Permalink
fix(js_formatter): placements of line comments between function paren…
Browse files Browse the repository at this point in the history
…theses and body (#1485)
  • Loading branch information
kalleep authored Jan 11, 2024
1 parent 660c38a commit b7390da
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 198 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 @@ -29,6 +29,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

0 comments on commit b7390da

Please sign in to comment.