Skip to content

Commit 85e5d6b

Browse files
committed
fix(prettier): stop stacking parentheses around a commented element
A line-breaking comment already opens one pair around an element. Wrapping that result again printed a second pair and, for a trailing comment, the comment twice. A return or throw that already opened parentheses no longer gets another pair from the element wrap.
1 parent f58b3df commit 85e5d6b

1 file changed

Lines changed: 16 additions & 18 deletions

File tree

‎packages/prettier-plugin/src/index.js‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,23 +3719,6 @@ function printTsrxNode(node, path, options, print, args) {
37193719
nodeContent = [...printDecorators(decorated, path, options, print), nodeContent];
37203720
}
37213721

3722-
// Like Prettier's `printJsxElement`, an element's comments print inside
3723-
// the parentheses around a multi-line element, so a comment on its own
3724-
// line opens them
3725-
let suppressTrailingComments = args?.suppressTrailingComments;
3726-
if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
3727-
const trailingParts = suppressTrailingComments ? [] : printTrailingComments(node, options);
3728-
nodeContent = maybeWrapJSXElementInParens(
3729-
path,
3730-
parts.length > 0 || trailingParts.length > 0
3731-
? [...parts, nodeContent, ...trailingParts]
3732-
: nodeContent,
3733-
options,
3734-
);
3735-
parts.length = 0;
3736-
suppressTrailingComments = true;
3737-
}
3738-
37393722
// A cast's parens belong to the cast, so they print even where a parent
37403723
// lays out the node's other parens (`suppressOwnParens`)
37413724
if (typeCastParens) {
@@ -3747,6 +3730,10 @@ function printTsrxNode(node, path, options, print, args) {
37473730
args,
37483731
);
37493732
} else if (elementPrintsCommentsInParens(path, options, args)) {
3733+
// Comments that break the line print inside this one pair. Wrapping
3734+
// again in `maybeWrapJSXElementInParens` would stack a second pair
3735+
// around a group that already breaks, and reprint a trailing comment
3736+
// already inside the first pair.
37503737
return printElementInParens(
37513738
path,
37523739
options,
@@ -3758,6 +3745,17 @@ function printTsrxNode(node, path, options, print, args) {
37583745
args?.suppressTrailingComments,
37593746
),
37603747
);
3748+
} else if (
3749+
(node.type === 'JSXElement' || node.type === 'JSXFragment') &&
3750+
!args?.suppressOwnParens
3751+
) {
3752+
// A multi-line element in an expression position. A parent that already
3753+
// opened parentheses (`suppressOwnParens`, as `return` and `throw` do
3754+
// for an own-line comment) must not get another pair here.
3755+
nodeContent = maybeWrapJSXElementInParens(path, nodeContent, options);
3756+
if (needsParens(path, options)) {
3757+
nodeContent = ['(', nodeContent, ')'];
3758+
}
37613759
} else if (!args?.suppressOwnParens && needsParens(path, options)) {
37623760
nodeContent = ['(', nodeContent, ')'];
37633761
}
@@ -3767,7 +3765,7 @@ function printTsrxNode(node, path, options, print, args) {
37673765
parts,
37683766
nodeContent,
37693767
options,
3770-
suppressTrailingComments,
3768+
args?.suppressTrailingComments,
37713769
);
37723770
}
37733771

0 commit comments

Comments
 (0)