Skip to content

Commit 48c94d7

Browse files
committed
fix: jsx element in arrow function body should be formatted appropriately
1 parent 94a5cd3 commit 48c94d7

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/parsing/parser.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ fn parse_arrow_func_expr<'a>(node: &'a ArrowExpr, context: &mut Context<'a>) ->
14721472
items.push_str(" =>");
14731473

14741474
let parsed_body = parse_node(node.body.into(), context);
1475-
let parsed_body = if use_new_line_group_for_arrow_body(node) {
1475+
let parsed_body = if use_new_line_group_for_arrow_body(node, context) {
14761476
new_line_group(parsed_body)
14771477
} else {
14781478
parsed_body
@@ -1499,7 +1499,7 @@ fn parse_arrow_func_expr<'a>(node: &'a ArrowExpr, context: &mut Context<'a>) ->
14991499
let end_body_info = Info::new("endBody");
15001500
items.push_info(start_body_info);
15011501

1502-
if should_not_newline_after_arrow(&node.body) {
1502+
if should_not_newline_after_arrow(&node.body, context) {
15031503
items.push_str(" ");
15041504
} else {
15051505
items.push_condition(conditions::if_above_width_or(
@@ -1521,13 +1521,13 @@ fn parse_arrow_func_expr<'a>(node: &'a ArrowExpr, context: &mut Context<'a>) ->
15211521

15221522
return items;
15231523

1524-
fn should_not_newline_after_arrow(body: &BlockStmtOrExpr) -> bool {
1524+
fn should_not_newline_after_arrow(body: &BlockStmtOrExpr, context: &Context) -> bool {
15251525
match body {
15261526
BlockStmtOrExpr::BlockStmt(_) => true,
15271527
BlockStmtOrExpr::Expr(expr) => match expr {
15281528
Expr::Paren(_) | Expr::Array(_) => true,
15291529
Expr::Tpl(tpl) => tpl.quasis[0].raw.value().starts_with(|c: char| c == '\n' || c == '\r'),
1530-
_ => false,
1530+
_ => is_jsx_paren_expr_handled_node(&expr.into(), context),
15311531
},
15321532
}
15331533
}
@@ -2285,16 +2285,16 @@ fn parse_paren_expr<'a>(node: &'a ParenExpr, context: &mut Context<'a>) -> Print
22852285
))
22862286
.into();
22872287

2288-
return if get_use_new_line_group(node) {
2288+
return if get_use_new_line_group(node, context) {
22892289
new_line_group(parsed_items)
22902290
} else {
22912291
parsed_items
22922292
};
22932293

2294-
fn get_use_new_line_group(node: &ParenExpr) -> bool {
2294+
fn get_use_new_line_group(node: &ParenExpr, context: &Context) -> bool {
22952295
if let Node::ArrowExpr(arrow_expr) = node.parent() {
22962296
debug_assert!(arrow_expr.body.lo() == node.lo());
2297-
use_new_line_group_for_arrow_body(arrow_expr)
2297+
use_new_line_group_for_arrow_body(arrow_expr, context)
22982298
} else {
22992299
true
23002300
}
@@ -8241,11 +8241,14 @@ fn assert_has_op<'a>(op: &str, op_token: Option<&TokenAndSpan>, context: &mut Co
82418241
}
82428242
}
82438243

8244-
fn use_new_line_group_for_arrow_body(arrow_expr: &ArrowExpr) -> bool {
8244+
fn use_new_line_group_for_arrow_body(arrow_expr: &ArrowExpr, context: &Context) -> bool {
82458245
match &arrow_expr.body {
8246-
BlockStmtOrExpr::Expr(Expr::Paren(paren)) => match paren.expr {
8247-
Expr::Object(_) => false,
8248-
_ => true,
8246+
BlockStmtOrExpr::Expr(expr) => match expr {
8247+
Expr::Paren(paren) => match paren.expr {
8248+
Expr::Object(_) => false,
8249+
_ => !is_jsx_paren_expr_handled_node(&paren.expr.into(), context),
8250+
},
8251+
_ => !is_jsx_paren_expr_handled_node(&expr.into(), context),
82498252
},
82508253
_ => true,
82518254
}

tests/specs/jsx/JsxElement/JsxElement_MultiLineParens_False.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,19 @@ const t = <Test
106106
>
107107
<Test />
108108
</Test>;
109+
110+
== should handle jsx in arrow function expr with multiple parens ==
111+
const t = test.map((a, b) => <testing a={b} b={test}>
112+
{a[1]}
113+
</testing>);
114+
const u = test.map((a, b) => <testing a={b} b={test} />);
115+
116+
[expect]
117+
const t = test.map((a, b) =>
118+
<testing a={b} b={test}>
119+
{a[1]}
120+
</testing>
121+
);
122+
const u = test.map((a, b) =>
123+
<testing a={b} b={test} />
124+
);

tests/specs/jsx/JsxElement/JsxElement_MultiLineParens_True.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,19 @@ const t = (
178178
)}
179179
/>
180180
);
181+
182+
== should handle jsx in arrow function expr with multiple parens ==
183+
const t = test.map((a, b) => <testing a={b} b={test}>
184+
{a[1]}
185+
</testing>);
186+
const u = test.map((a, b) => <testing a={b} b={test} />);
187+
188+
[expect]
189+
const t = test.map((a, b) => (
190+
<testing a={b} b={test}>
191+
{a[1]}
192+
</testing>
193+
));
194+
const u = test.map((a, b) => (
195+
<testing a={b} b={test} />
196+
));

0 commit comments

Comments
 (0)