Skip to content

Commit 52e2a13

Browse files
committed
fix: prefer keeping jsx paren exprs inline in binary expressions
1 parent 187727c commit 52e2a13

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/parsing/parser.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,6 @@ fn parse_await_expr<'a>(node: &'a AwaitExpr, context: &mut Context<'a>) -> Print
16411641
}
16421642

16431643
fn parse_binary_expr<'a>(node: &'a BinExpr, context: &mut Context<'a>) -> PrintItems {
1644-
// todo: use a simplified version for nodes that don't need the complexity (for performance reasons)
16451644
let mut items = PrintItems::new();
16461645
let flattened_binary_expr = get_flattened_bin_expr(node, context);
16471646
// println!("Bin expr: {:?}", flattened_binary_expr.iter().map(|x| x.expr.text(context)).collect::<Vec<_>>());
@@ -1795,8 +1794,14 @@ fn parse_binary_expr<'a>(node: &'a BinExpr, context: &mut Context<'a>) -> PrintI
17951794
items
17961795
}));
17971796

1797+
let items = if should_newline_group_bin_item_expr(&bin_expr_item.expr, context) {
1798+
parser_helpers::new_line_group(items)
1799+
} else {
1800+
items
1801+
};
1802+
17981803
parsed_nodes.push(parser_helpers::ParsedValue {
1799-
items: parser_helpers::new_line_group(items),
1804+
items,
18001805
lines_span,
18011806
allow_inline_multi_line: true,
18021807
allow_inline_single_line: true,
@@ -1872,13 +1877,26 @@ fn parse_binary_expr<'a>(node: &'a BinExpr, context: &mut Context<'a>) -> PrintI
18721877
items
18731878
}
18741879

1875-
fn get_use_space_surrounding_operator(op: &BinaryOp, context: &mut Context) -> bool {
1880+
fn get_use_space_surrounding_operator(op: &BinaryOp, context: &Context) -> bool {
18761881
if op.is_bitwise_or_arithmetic() {
18771882
context.config.binary_expression_space_surrounding_bitwise_and_arithmetic_operator
18781883
} else {
18791884
true
18801885
}
18811886
}
1887+
1888+
fn should_newline_group_bin_item_expr(node: &Node, context: &Context) -> bool {
1889+
if let Some(node) = node.to::<ParenExpr>() {
1890+
return should_newline_group_bin_item_expr(&node.expr.into(), context);
1891+
}
1892+
1893+
if is_jsx_paren_expr_handled_node(node, context) {
1894+
// prefer using the possible newline at the start of the element
1895+
return false;
1896+
}
1897+
1898+
true
1899+
}
18821900
}
18831901

18841902
fn parse_call_expr<'a>(node: &'a CallExpr, context: &mut Context<'a>) -> PrintItems {

tests/specs/jsx/JsxElement/JsxElement_All.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,36 @@ const t = (
256256
Test
257257
</Element>
258258
);
259+
260+
== should try to keep jsx parened expr inlined within binary expressions ==
261+
const t = <div>
262+
{someCondition && (
263+
<td className="some-class-name">
264+
{someValue}
265+
</td>
266+
)}
267+
</div>;
268+
// this has the && right at the line width max so the newline will happen before
269+
const testing = someConditionLongEnoughTestTest && (
270+
<td className="some-class-name">
271+
{someValue}
272+
</td>
273+
);
274+
275+
[expect]
276+
const t = (
277+
<div>
278+
{someCondition && (
279+
<td className="some-class-name">
280+
{someValue}
281+
</td>
282+
)}
283+
</div>
284+
);
285+
// this has the && right at the line width max so the newline will happen before
286+
const testing = someConditionLongEnoughTestTest
287+
&& (
288+
<td className="some-class-name">
289+
{someValue}
290+
</td>
291+
);

0 commit comments

Comments
 (0)