Skip to content

Commit 4283fd8

Browse files
committed
fix(formatter): correct printing comments for JSXAttributeValue (#14719)
1 parent 59c9e1b commit 4283fd8

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

crates/oxc_formatter/src/write/jsx/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use crate::{
1717
formatter::{
1818
Comments, Formatter,
1919
prelude::*,
20-
trivia::{DanglingIndentMode, FormatDanglingComments},
20+
trivia::{DanglingIndentMode, FormatDanglingComments, FormatTrailingComments},
2121
},
22+
utils::format_node_without_trailing_comments::FormatNodeWithoutTrailingComments,
2223
write,
2324
};
2425

@@ -202,16 +203,23 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> {
202203
}
203204
}
204205
} else {
206+
// JSXAttributeValue
205207
let should_inline = !has_comment(f) && should_inline_jsx_expression(self, f.comments());
206208

209+
let format_expression = format_once(|f| {
210+
write!(f, FormatNodeWithoutTrailingComments(&self.expression()));
211+
let comments = f.context().comments().comments_before(self.span.end);
212+
write!(f, FormatTrailingComments::Comments(comments))
213+
});
214+
207215
if should_inline {
208-
write!(f, ["{", self.expression(), line_suffix_boundary(), "}"])
216+
write!(f, ["{", format_expression, line_suffix_boundary(), "}"])
209217
} else {
210218
write!(
211219
f,
212220
[group(&format_args!(
213221
"{",
214-
soft_block_indent(&self.expression()),
222+
soft_block_indent(&format_expression),
215223
line_suffix_boundary(),
216224
"}"
217225
))]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div
2+
key={formMessageId} // Use a unique key to help with animations
3+
initial={{ opacity: 0, y: -5, height: 0 }} // Start slightly hidden
4+
animate={{ opacity: 1, y: 0, height: "auto" }} // Fade in and slide up
5+
exit={{ opacity: 0, y: -5, height: 0 }} // Fade out and slide back up
6+
transition={{ duration: 0.15, ease: "easeInOut" }} // Smooth transition
7+
style={{ /* comment */ overflow: "hidden" /* comment */ }}
8+
></div>;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
<div
6+
key={formMessageId} // Use a unique key to help with animations
7+
initial={{ opacity: 0, y: -5, height: 0 }} // Start slightly hidden
8+
animate={{ opacity: 1, y: 0, height: "auto" }} // Fade in and slide up
9+
exit={{ opacity: 0, y: -5, height: 0 }} // Fade out and slide back up
10+
transition={{ duration: 0.15, ease: "easeInOut" }} // Smooth transition
11+
style={{ /* comment */ overflow: "hidden" /* comment */ }}
12+
></div>;
13+
==================== Output ====================
14+
<div
15+
key={formMessageId} // Use a unique key to help with animations
16+
initial={{ opacity: 0, y: -5, height: 0 }} // Start slightly hidden
17+
animate={{ opacity: 1, y: 0, height: "auto" }} // Fade in and slide up
18+
exit={{ opacity: 0, y: -5, height: 0 }} // Fade out and slide back up
19+
transition={{ duration: 0.15, ease: "easeInOut" }} // Smooth transition
20+
style={{ /* comment */ overflow: "hidden" /* comment */ }}
21+
></div>;
22+
23+
===================== End =====================

0 commit comments

Comments
 (0)