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(formatter): refine the logic for identifying comments on the last attribute in JSX #4835

Merged
merged 3 commits into from
Jan 6, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- `noDuplicateProperties` now throws lint errors properly when we use `@supports` (fix [#4756](https://github.com/biomejs/biome/issues/4756)) Contributed by @mehm8128

- Fix [#4719](https://github.com/biomejs/biome/issues/4719), `bracketSameLine` now performs as expected when a comment is placed before the last JSX attribute. Contributed by @bushuai

### JavaScript APIs

### Linter
Expand Down
20 changes: 4 additions & 16 deletions crates/biome_js_formatter/src/jsx/tag/opening_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ impl AnyJsxOpeningElement {
fn compute_layout(&self, comments: &JsComments) -> SyntaxResult<OpeningElementLayout> {
let attributes = self.attributes();
let name = self.name()?;
let last_attribute_has_comments = self.attributes().last().map_or(false, |attribute| {
comments.has_trailing_comments(attribute.syntax())
});

let name_has_comments = comments.has_comments(name.syntax())
|| self
Expand All @@ -171,7 +174,7 @@ impl AnyJsxOpeningElement {
} else {
OpeningElementLayout::IndentAttributes {
name_has_comments,
last_attribute_has_comments: has_last_attribute_comments(self, comments),
last_attribute_has_comments,
}
};

Expand Down Expand Up @@ -249,18 +252,3 @@ fn as_string_literal_attribute_value(attribute: &AnyJsxAttribute) -> Option<JsxS
JsxSpreadAttribute(_) => None,
}
}

fn has_last_attribute_comments(element: &AnyJsxOpeningElement, comments: &JsComments) -> bool {
let has_comments_on_last_attribute = element
.attributes()
.last()
.map_or(false, |attribute| comments.has_comments(attribute.syntax()));

let last_attribute_has_comments = element
.syntax()
.tokens()
.map(|token| token.text().contains('>') && token.has_leading_comments())
.any(|has_comment| has_comment);

has_comments_on_last_attribute || last_attribute_has_comments
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ const a = <div></div>;
Hi
</Foo>;

<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
// comment
reallyLongAttributeName3={yetAnotherLongValue}
>
Hi
</Foo>;

<div className="hi" />;
<div className="hi"></div>;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
assertion_line: 212
info: jsx/bracket_same_line/bracket_same_line.jsx
---
# Input
Expand Down Expand Up @@ -37,6 +36,16 @@ const a = <div></div>;
Hi
</Foo>;

<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
// comment
reallyLongAttributeName3={yetAnotherLongValue}
>
Hi
</Foo>;

<div className="hi" />;
<div className="hi"></div>;

Expand Down Expand Up @@ -97,6 +106,16 @@ const a = <div></div>;
Hi
</Foo>;

<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
// comment
reallyLongAttributeName3={yetAnotherLongValue}
>
Hi
</Foo>;

<div className="hi" />;
<div className="hi"></div>;
```
Expand Down Expand Up @@ -150,6 +169,15 @@ const a = <div></div>;
Hi
</Foo>;

<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
// comment
reallyLongAttributeName3={yetAnotherLongValue}>
Hi
</Foo>;

<div className="hi" />;
<div className="hi"></div>;
```
Loading