Skip to content
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
20 changes: 18 additions & 2 deletions crates/oxc_formatter/src/print/jsx/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,24 @@ impl<'a> AnyJsxTagWithChildren<'a, '_> {
&& arrow.expression
{
f.context().comments().comments_before(arrow.span.end)
} else if matches!(self.parent(), AstNodes::ConditionalExpression(_)) {
f.context().comments().end_of_line_comments_after(self.span().end)
} else if let AstNodes::ConditionalExpression(conditional) = self.parent() {
if self.span() == conditional.alternate.span() {
// Since `preserveParens` is disabled, `conditional.alternate.span` only covers
// `<Success />`, not the surrounding parentheses or comments within them:
// ```jsx
// false ? (
// <Error />
// ) : (
// <Success />
// /* comment */
// )
// ```
// To capture comments like the one above, we get all comments before the
// conditional expression's end (which includes the closing paren).
f.context().comments().comments_before(conditional.span.end)
} else {
f.context().comments().end_of_line_comments_after(self.span().end)
}
} else {
// Fall back to default trailing comments behavior
return match self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ const x = (
)}
</>
);

// https://github.com/oxc-project/oxc/issues/18419
const Component = () => (
<div>
{"error" ? (
<Error />
) : (
<Success />
/* This is a comment inside the expression */
)}
</div>
);
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ const x = (
</>
);

// https://github.com/oxc-project/oxc/issues/18419
const Component = () => (
<div>
{"error" ? (
<Error />
) : (
<Success />
/* This is a comment inside the expression */
)}
</div>
);

==================== Output ====================
------------------
{ printWidth: 80 }
Expand Down Expand Up @@ -112,6 +124,18 @@ const x = (
</>
);

// https://github.com/oxc-project/oxc/issues/18419
const Component = () => (
<div>
{"error" ? (
<Error />
) : (
<Success />
/* This is a comment inside the expression */
)}
</div>
);

-------------------
{ printWidth: 100 }
-------------------
Expand Down Expand Up @@ -169,4 +193,16 @@ const x = (
</>
);

// https://github.com/oxc-project/oxc/issues/18419
const Component = () => (
<div>
{"error" ? (
<Error />
) : (
<Success />
/* This is a comment inside the expression */
)}
</div>
);

===================== End =====================
Loading