From a6cdede7d7b6ecbb3462567682922cb0046816b6 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 4 Nov 2026 17:28:06 -0500 Subject: [PATCH] fix: keep parens around jsx that starts a member, call or new expression --- src/generation/generate.rs | 29 ++++- .../JsxElement_ParensNeeded_Always.txt | 99 +++++++++++++++++ .../JsxElement_ParensNeeded_Never.txt | 101 ++++++++++++++++++ .../JsxElement_ParensNeeded_Prefer.txt | 99 +++++++++++++++++ 4 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Always.txt create mode 100644 tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Never.txt create mode 100644 tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Prefer.txt diff --git a/src/generation/generate.rs b/src/generation/generate.rs index faf872bf..8d45b890 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -4591,6 +4591,11 @@ fn handle_jsx_surrounding_parens<'a>(inner_items: PrintItems<'a>, context: &mut return surround_with_newlines_indented_if_multi_line(inner_items, context.config.indent_width); } + // ex. `(
).props` + let are_parens_required = match context.parent() { + Node::ParenExpr(paren_expr) => is_jsx_paren_expr_required(paren_expr.into(), paren_expr.parent()), + _ => false, + }; let start_ln = LineNumber::new("conditionalParenStart"); let end_ln = LineNumber::new("conditionalParenEnd"); let mut items = PrintItems::new(context.arena); @@ -4604,7 +4609,14 @@ fn handle_jsx_surrounding_parens<'a>(inner_items: PrintItems<'a>, context: &mut .arena .alloc_resolver(move |context| condition_helpers::is_multiple_lines(context, start_ln, end_ln)), surround_with_parens(surround_with_new_lines(with_indent(inner_items_rc.into_print_items(context.arena)))), - { + if are_parens_required { + let items = inner_items_rc.into_print_items(context.arena); + surround_with_parens(if context.config.paren_expression_space_around { + surround_with_spaces(items) + } else { + items + }) + } else { let mut items = PrintItems::new(context.arena); items.push_signal(Signal::PossibleNewLine); items.extend(inner_items_rc.into_print_items(context.arena)); @@ -4670,6 +4682,21 @@ fn is_jsx_paren_expr_handled_node<'a>(node: Node<'a>, context: &Context<'a>) -> ) } +/// Gets if the parens around a JSX element or fragment are required because +/// the JSX starts a member, call, or similar expression (ex. `(
).props`). +fn is_jsx_paren_expr_required(paren_expr: Node, parent: Node) -> bool { + let paren_range = paren_expr.range(); + match parent { + Node::MemberExpr(member_expr) => member_expr.obj.range() == paren_range, + Node::CallExpr(call_expr) => call_expr.callee.range() == paren_range, + Node::OptCall(call_expr) => call_expr.callee.range() == paren_range, + Node::NewExpr(new_expr) => new_expr.callee.range() == paren_range, + Node::TaggedTpl(tagged_tpl) => tagged_tpl.tag.range() == paren_range, + Node::TsNonNullExpr(_) | Node::TsInstantiation(_) => true, + _ => false, + } +} + fn gen_jsx_element<'a>(node: &JSXElement<'a>, context: &mut Context<'a>) -> PrintItems<'a> { let items = if let Some(closing) = node.closing { // pre element bodies should be formatted as-is diff --git a/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Always.txt b/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Always.txt new file mode 100644 index 00000000..544383ad --- /dev/null +++ b/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Always.txt @@ -0,0 +1,99 @@ +-- file.tsx -- +~~ jsx.multiLineParens: always ~~ +== should keep the parens when the jsx is called or constructed == +const a = (
)(); +const b = (
)?.(); +const c = new (
)(); +const d = (<>)(); +const e = ( +
+)(); + +[expect] +const a = (
)(); +const b = (
)?.(); +const c = new (
)(); +const d = (<>)(); +const e = (
)(); + +== should keep the parens when the jsx is a tag, non-null, or instantiation expression == +const a = (
)`a`; +const b = (
)!; +const c = (
); + +[expect] +const a = (
)`a`; +const b = (
)!; +const c = (
); + +== should keep the parens when the jsx is the object of a member expression == +const a = (
).props; +const b = (
)?.props; +const c = (
)[0]; +const d = (
).a.b(); + +[expect] +const a = (
).props; +const b = (
)?.props; +const c = (
)[0]; +const d = (
).a.b(); + +== should keep the parens when the jsx is multi-line == +const a = ( +
+ a +
+)(); +const b = new ( +
+ a +
+)(); +const c = ( +
+ a +
+)`a`; +const d = ( +
+ a +
+)!; +const e = ( +
+ a +
+).props; + +[expect] +const a = ( +
+ a +
+)(); +const b = new ( +
+ a +
+)(); +const c = ( +
+ a +
+)`a`; +const d = ( +
+ a +
+)!; +const e = ( +
+ a +
+).props; + +== should not double the parens when they're nested == +const a = ((
))(); + +[expect] +const a = (
)(); diff --git a/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Never.txt b/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Never.txt new file mode 100644 index 00000000..21d36158 --- /dev/null +++ b/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Never.txt @@ -0,0 +1,101 @@ +-- file.tsx -- +~~ jsx.multiLineParens: never ~~ +== should keep the parens when the jsx is called or constructed == +const a = (
)(); +const b = (
)?.(); +const c = new (
)(); +const d = (<>)(); +const e = ( +
+)(); + +[expect] +const a = (
)(); +const b = (
)?.(); +const c = new (
)(); +const d = (<>)(); +const e = ( +
+)(); + +== should keep the parens when the jsx is a tag, non-null, or instantiation expression == +const a = (
)`a`; +const b = (
)!; +const c = (
); + +[expect] +const a = (
)`a`; +const b = (
)!; +const c = (
); + +== should keep the parens when the jsx is the object of a member expression == +const a = (
).props; +const b = (
)?.props; +const c = (
)[0]; +const d = (
).a.b(); + +[expect] +const a = (
).props; +const b = (
)?.props; +const c = (
)[0]; +const d = (
).a.b(); + +== should keep the parens when the jsx is multi-line == +const a = ( +
+ a +
+)(); +const b = new ( +
+ a +
+)(); +const c = ( +
+ a +
+)`a`; +const d = ( +
+ a +
+)!; +const e = ( +
+ a +
+).props; + +[expect] +const a = ( +
+ a +
+)(); +const b = new ( +
+ a +
+)(); +const c = ( +
+ a +
+)`a`; +const d = ( +
+ a +
+)!; +const e = ( +
+ a +
+).props; + +== should not double the parens when they're nested == +const a = ((
))(); + +[expect] +const a = (
)(); diff --git a/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Prefer.txt b/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Prefer.txt new file mode 100644 index 00000000..fcddca38 --- /dev/null +++ b/tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Prefer.txt @@ -0,0 +1,99 @@ +-- file.tsx -- +~~ jsx.multiLineParens: prefer ~~ +== should keep the parens when the jsx is called or constructed == +const a = (
)(); +const b = (
)?.(); +const c = new (
)(); +const d = (<>)(); +const e = ( +
+)(); + +[expect] +const a = (
)(); +const b = (
)?.(); +const c = new (
)(); +const d = (<>)(); +const e = (
)(); + +== should keep the parens when the jsx is a tag, non-null, or instantiation expression == +const a = (
)`a`; +const b = (
)!; +const c = (
); + +[expect] +const a = (
)`a`; +const b = (
)!; +const c = (
); + +== should keep the parens when the jsx is the object of a member expression == +const a = (
).props; +const b = (
)?.props; +const c = (
)[0]; +const d = (
).a.b(); + +[expect] +const a = (
).props; +const b = (
)?.props; +const c = (
)[0]; +const d = (
).a.b(); + +== should keep the parens when the jsx is multi-line == +const a = ( +
+ a +
+)(); +const b = new ( +
+ a +
+)(); +const c = ( +
+ a +
+)`a`; +const d = ( +
+ a +
+)!; +const e = ( +
+ a +
+).props; + +[expect] +const a = ( +
+ a +
+)(); +const b = new ( +
+ a +
+)(); +const c = ( +
+ a +
+)`a`; +const d = ( +
+ a +
+)!; +const e = ( +
+ a +
+).props; + +== should not double the parens when they're nested == +const a = ((
))(); + +[expect] +const a = (
)();