diff --git a/crates/oxc_formatter/src/write/jsx/mod.rs b/crates/oxc_formatter/src/write/jsx/mod.rs index f4d99f1696d73..079c4b435dc0d 100644 --- a/crates/oxc_formatter/src/write/jsx/mod.rs +++ b/crates/oxc_formatter/src/write/jsx/mod.rs @@ -194,7 +194,12 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> { f, [group(&format_args!( "{", - soft_block_indent(&self.expression()), + soft_block_indent(&format_with(|f| { + write!(f, [self.expression()])?; + let comments = + f.context().comments().comments_before(self.span.end); + write!(f, [FormatTrailingComments::Comments(comments)]) + })), line_suffix_boundary(), "}" ))] @@ -212,7 +217,11 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> { f, [group(&format_args!( "{", - soft_block_indent(&self.expression()), + soft_block_indent(&format_with(|f| { + write!(f, [self.expression()])?; + let comments = f.context().comments().comments_before(self.span.end); + write!(f, [FormatTrailingComments::Comments(comments)]) + })), line_suffix_boundary(), "}" ))] diff --git a/crates/oxc_formatter/tests/fixtures/js/jsx/expression-with-dangling-comment.jsx b/crates/oxc_formatter/tests/fixtures/js/jsx/expression-with-dangling-comment.jsx new file mode 100644 index 0000000000000..33032ea1de97f --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/jsx/expression-with-dangling-comment.jsx @@ -0,0 +1,21 @@ +const X = (<> + { + value + // this comment should stay here + } +); + +const Y = <> + { + value + // this comment should stay here + // this comment should stay here too! + } +; + +// This will be one line +const Z = <> + { + value + } +; diff --git a/crates/oxc_formatter/tests/fixtures/js/jsx/expression-with-dangling-comment.jsx.snap b/crates/oxc_formatter/tests/fixtures/js/jsx/expression-with-dangling-comment.jsx.snap new file mode 100644 index 0000000000000..e42aa40094bf5 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/jsx/expression-with-dangling-comment.jsx.snap @@ -0,0 +1,50 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +const X = (<> + { + value + // this comment should stay here + } +); + +const Y = <> + { + value + // this comment should stay here + // this comment should stay here too! + } +; + +// This will be one line +const Z = <> + { + value + } +; + +==================== Output ==================== +const X = ( + <> + { + value + // this comment should stay here + } + +); + +const Y = ( + <> + { + value + // this comment should stay here + // this comment should stay here too! + } + +); + +// This will be one line +const Z = <>{value}; + +===================== End =====================