Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4598,6 +4598,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. `(<div />).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);
Expand All @@ -4611,7 +4616,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));
Expand Down Expand Up @@ -4714,11 +4726,22 @@ fn is_jsx_paren_expr_removed<'a>(node: &'a ParenExpr<'a>, context: &Context<'a>)

match parent {
Node::ExprOrSpread(expr_or_spread) => expr_or_spread.spread().is_none(),
// keep when the jsx starts a member, call, or similar expression (ex. `(<div />).props`)
Node::MemberExpr(_) | Node::OptChainExpr(_) | Node::NewExpr(_) | Node::TaggedTpl(_) | Node::TsNonNullExpr(_) | Node::TsInstantiation(_) => false,
Node::CallExpr(call_expr) => !call_expr.callee.range().contains(&child.range()),
Node::OptCall(call_expr) => !call_expr.callee.range().contains(&child.range()),
_ => true,
_ => !is_jsx_paren_expr_required(child, parent),
}
}

/// Gets if the parens around a JSX element or fragment are required because
/// the JSX starts a member, call, or similar expression (ex. `(<div />).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,
}
}

Expand Down
99 changes: 99 additions & 0 deletions tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Always.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-- file.tsx --
~~ jsx.multiLineParens: always ~~
== should keep the parens when the jsx is called or constructed ==
const a = (<div />)();
const b = (<div />)?.();
const c = new (<div />)();
const d = (<></>)();
const e = (
<div />
)();

[expect]
const a = (<div />)();
const b = (<div />)?.();
const c = new (<div />)();
const d = (<></>)();
const e = (<div />)();

== should keep the parens when the jsx is a tag, non-null, or instantiation expression ==
const a = (<div />)`a`;
const b = (<div />)!;
const c = (<div />)<T>;

[expect]
const a = (<div />)`a`;
const b = (<div />)!;
const c = (<div />)<T>;

== should keep the parens when the jsx is the object of a member expression ==
const a = (<div />).props;
const b = (<div />)?.props;
const c = (<div />)[0];
const d = (<div />).a.b();

[expect]
const a = (<div />).props;
const b = (<div />)?.props;
const c = (<div />)[0];
const d = (<div />).a.b();

== should keep the parens when the jsx is multi-line ==
const a = (
<div>
<span>a</span>
</div>
)();
const b = new (
<div>
<span>a</span>
</div>
)();
const c = (
<div>
<span>a</span>
</div>
)`a`;
const d = (
<div>
<span>a</span>
</div>
)!;
const e = (
<div>
<span>a</span>
</div>
).props;

[expect]
const a = (
<div>
<span>a</span>
</div>
)();
const b = new (
<div>
<span>a</span>
</div>
)();
const c = (
<div>
<span>a</span>
</div>
)`a`;
const d = (
<div>
<span>a</span>
</div>
)!;
const e = (
<div>
<span>a</span>
</div>
).props;

== should not double the parens when they're nested ==
const a = ((<div />))();

[expect]
const a = (<div />)();
101 changes: 101 additions & 0 deletions tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Never.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
-- file.tsx --
~~ jsx.multiLineParens: never ~~
== should keep the parens when the jsx is called or constructed ==
const a = (<div />)();
const b = (<div />)?.();
const c = new (<div />)();
const d = (<></>)();
const e = (
<div />
)();

[expect]
const a = (<div />)();
const b = (<div />)?.();
const c = new (<div />)();
const d = (<></>)();
const e = (
<div />
)();

== should keep the parens when the jsx is a tag, non-null, or instantiation expression ==
const a = (<div />)`a`;
const b = (<div />)!;
const c = (<div />)<T>;

[expect]
const a = (<div />)`a`;
const b = (<div />)!;
const c = (<div />)<T>;

== should keep the parens when the jsx is the object of a member expression ==
const a = (<div />).props;
const b = (<div />)?.props;
const c = (<div />)[0];
const d = (<div />).a.b();

[expect]
const a = (<div />).props;
const b = (<div />)?.props;
const c = (<div />)[0];
const d = (<div />).a.b();

== should keep the parens when the jsx is multi-line ==
const a = (
<div>
<span>a</span>
</div>
)();
const b = new (
<div>
<span>a</span>
</div>
)();
const c = (
<div>
<span>a</span>
</div>
)`a`;
const d = (
<div>
<span>a</span>
</div>
)!;
const e = (
<div>
<span>a</span>
</div>
).props;

[expect]
const a = (
<div>
<span>a</span>
</div>
)();
const b = new (
<div>
<span>a</span>
</div>
)();
const c = (
<div>
<span>a</span>
</div>
)`a`;
const d = (
<div>
<span>a</span>
</div>
)!;
const e = (
<div>
<span>a</span>
</div>
).props;

== should not double the parens when they're nested ==
const a = ((<div />))();

[expect]
const a = (<div />)();
99 changes: 99 additions & 0 deletions tests/specs/jsx/JsxElement/JsxElement_ParensNeeded_Prefer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-- file.tsx --
~~ jsx.multiLineParens: prefer ~~
== should keep the parens when the jsx is called or constructed ==
const a = (<div />)();
const b = (<div />)?.();
const c = new (<div />)();
const d = (<></>)();
const e = (
<div />
)();

[expect]
const a = (<div />)();
const b = (<div />)?.();
const c = new (<div />)();
const d = (<></>)();
const e = (<div />)();

== should keep the parens when the jsx is a tag, non-null, or instantiation expression ==
const a = (<div />)`a`;
const b = (<div />)!;
const c = (<div />)<T>;

[expect]
const a = (<div />)`a`;
const b = (<div />)!;
const c = (<div />)<T>;

== should keep the parens when the jsx is the object of a member expression ==
const a = (<div />).props;
const b = (<div />)?.props;
const c = (<div />)[0];
const d = (<div />).a.b();

[expect]
const a = (<div />).props;
const b = (<div />)?.props;
const c = (<div />)[0];
const d = (<div />).a.b();

== should keep the parens when the jsx is multi-line ==
const a = (
<div>
<span>a</span>
</div>
)();
const b = new (
<div>
<span>a</span>
</div>
)();
const c = (
<div>
<span>a</span>
</div>
)`a`;
const d = (
<div>
<span>a</span>
</div>
)!;
const e = (
<div>
<span>a</span>
</div>
).props;

[expect]
const a = (
<div>
<span>a</span>
</div>
)();
const b = new (
<div>
<span>a</span>
</div>
)();
const c = (
<div>
<span>a</span>
</div>
)`a`;
const d = (
<div>
<span>a</span>
</div>
)!;
const e = (
<div>
<span>a</span>
</div>
).props;

== should not double the parens when they're nested ==
const a = ((<div />))();

[expect]
const a = (<div />)();
Loading