Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_js_formatter): comments in closing JSX elements #4244

Merged
merged 1 commit into from
Mar 4, 2023
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
32 changes: 25 additions & 7 deletions crates/rome_js_formatter/src/jsx/tag/closing_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,42 @@ use rome_js_syntax::{JsxClosingElement, JsxClosingElementFields};
pub struct FormatJsxClosingElement;

impl FormatNodeRule<JsxClosingElement> for FormatJsxClosingElement {
fn fmt_fields(
&self,
node: &JsxClosingElement,
formatter: &mut JsFormatter,
) -> FormatResult<()> {
fn fmt_fields(&self, node: &JsxClosingElement, f: &mut JsFormatter) -> FormatResult<()> {
let JsxClosingElementFields {
l_angle_token,
slash_token,
name,
r_angle_token,
} = node.as_fields();
let name = name?;

let mut name_has_leading_comment = false;
let mut name_has_own_line_leading_comment = false;
for leading_comment in f.comments().leading_comments(name.syntax()) {
name_has_leading_comment = true;
name_has_own_line_leading_comment =
name_has_own_line_leading_comment || leading_comment.kind().is_line()
}

let format_name = format_with(|f| {
if name_has_own_line_leading_comment {
write!(f, [hard_line_break()])?;
} else if name_has_leading_comment {
write!(f, [space()])?;
}
if name_has_own_line_leading_comment {
write!(f, [block_indent(&name.format()), hard_line_break()])
} else {
write!(f, [name.format()])
}
});

write![
formatter,
f,
[
l_angle_token.format(),
slash_token.format(),
name.format(),
&format_name,
line_suffix_boundary(),
r_angle_token.format(),
]
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/jsx/tag/closing_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl FormatNodeRule<JsxClosingFragment> for FormatJsxClosingFragment {
slash_token.format(),
indent(&format_comments),
has_own_line_comment.then_some(hard_line_break()),
r_angle_token.format()
r_angle_token.format(),
]
]
}
Expand Down
5 changes: 5 additions & 0 deletions crates/rome_js_formatter/tests/specs/jsx/comments.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<a></ /* block1 */
a>;

<a></ /* block1 */ // test
a>;
43 changes: 43 additions & 0 deletions crates/rome_js_formatter/tests/specs/jsx/comments.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
assertion_line: 212
info: jsx/comments.jsx
---

# Input

```jsx
<a></ /* block1 */
a>;

<a></ /* block1 */ // test
a>;
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Line width: 80
Quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
-----

```jsx
<a></ /* block1 */
a>;

<a></
/* block1 */ // test
a
>;
```