Skip to content

Commit bba9689

Browse files
committed
fix(formatter): correct printing comments around the expression of ComputedMemberExpression (#14486)
1 parent dc5e08a commit bba9689

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

crates/oxc_formatter/src/generated/ast_nodes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4189,7 +4189,7 @@ impl<'a> AstNode<'a, ComputedMemberExpression<'a>> {
41894189

41904190
#[inline]
41914191
pub fn expression(&self) -> &AstNode<'a, Expression<'a>> {
4192-
let following_node = self.following_node;
4192+
let following_node = None;
41934193
self.allocator.alloc(AstNode {
41944194
inner: &self.inner.expression,
41954195
allocator: self.allocator,

crates/oxc_formatter/src/utils/member_chain/chain_member.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ impl<'a> Format<'a> for FormatComputedMemberExpressionWithoutObject<'a, '_> {
141141
write!(f, [soft_line_break(), FormatLeadingComments::Comments(comments)])?;
142142
}
143143

144-
if matches!(self.expression, Expression::NumericLiteral(_)) {
144+
if matches!(self.expression, Expression::NumericLiteral(_))
145+
&& !f.comments().has_comment_before(self.span.end)
146+
{
145147
write!(f, [self.optional().then_some("?."), "[", self.expression(), "]"])
146148
} else {
147149
write!(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prop[
2+
0
3+
/* shouldCast */
4+
] = shouldCast;
5+
6+
let handler =
7+
props[handlerName = toHandlerKey(event)] || // also try camelCase event handler (#2249)
8+
props[handlerName = toHandlerKey(camelize(event))];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
prop[
6+
0
7+
/* shouldCast */
8+
] = shouldCast;
9+
10+
let handler =
11+
props[handlerName = toHandlerKey(event)] || // also try camelCase event handler (#2249)
12+
props[handlerName = toHandlerKey(camelize(event))];
13+
14+
==================== Output ====================
15+
prop[
16+
0
17+
/* shouldCast */
18+
] = shouldCast;
19+
20+
let handler =
21+
props[handlerName = toHandlerKey(event)] || // also try camelCase event handler (#2249)
22+
props[handlerName = toHandlerKey(camelize(event))];
23+
24+
===================== End =====================

tasks/ast_tools/src/generators/formatter/ast_nodes.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ const FORMATTER_CRATE_PATH: &str = "crates/oxc_formatter";
2020

2121
/// Based on the printing comments algorithm, the last child of these AST nodes don't need to print comments.
2222
/// Without following nodes could lead to only print comments that before the end of the node, which is what we want.
23-
const AST_NODE_WITHOUT_FOLLOWING_NODE_LIST: &[&str] =
24-
&["AssignmentExpression", "FormalParameters", "StaticMemberExpression", "ObjectProperty"];
23+
const AST_NODE_WITHOUT_FOLLOWING_NODE_LIST: &[&str] = &[
24+
"AssignmentExpression",
25+
"FormalParameters",
26+
"StaticMemberExpression",
27+
"ComputedMemberExpression",
28+
"ObjectProperty",
29+
];
2530

2631
const AST_NODE_WITH_FOLLOWING_NODE_LIST: &[&str] = &["Function", "Class"];
2732

0 commit comments

Comments
 (0)