diff --git a/crates/rome_js_formatter/src/jsx/tag/closing_element.rs b/crates/rome_js_formatter/src/jsx/tag/closing_element.rs index 47c00e0d1f5..f30d5041699 100644 --- a/crates/rome_js_formatter/src/jsx/tag/closing_element.rs +++ b/crates/rome_js_formatter/src/jsx/tag/closing_element.rs @@ -7,24 +7,42 @@ use rome_js_syntax::{JsxClosingElement, JsxClosingElementFields}; pub struct FormatJsxClosingElement; impl FormatNodeRule 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(), ] diff --git a/crates/rome_js_formatter/src/jsx/tag/closing_fragment.rs b/crates/rome_js_formatter/src/jsx/tag/closing_fragment.rs index a713bf84353..ca3def0f26b 100644 --- a/crates/rome_js_formatter/src/jsx/tag/closing_fragment.rs +++ b/crates/rome_js_formatter/src/jsx/tag/closing_fragment.rs @@ -42,7 +42,7 @@ impl FormatNodeRule 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(), ] ] } diff --git a/crates/rome_js_formatter/tests/specs/jsx/comments.jsx b/crates/rome_js_formatter/tests/specs/jsx/comments.jsx new file mode 100644 index 00000000000..7b099b2ead5 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/jsx/comments.jsx @@ -0,0 +1,5 @@ +; + +; \ No newline at end of file diff --git a/crates/rome_js_formatter/tests/specs/jsx/comments.jsx.snap b/crates/rome_js_formatter/tests/specs/jsx/comments.jsx.snap new file mode 100644 index 00000000000..fe0431687df --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/jsx/comments.jsx.snap @@ -0,0 +1,43 @@ +--- +source: crates/rome_formatter_test/src/snapshot_builder.rs +assertion_line: 212 +info: jsx/comments.jsx +--- + +# Input + +```jsx +; + +; +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +Quote properties: As needed +Trailing comma: All +Semicolons: Always +----- + +```jsx +; + +; +``` + +