diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index cb307e43eb6a..fe18414a95cf 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -574,27 +574,6 @@ function printJsxOpeningElement(path, options, print) { ]); } - const lastAttrHasTrailingComments = - node.attributes.length > 0 && - hasComment(getLast(node.attributes), CommentCheckFlags.Trailing); - - const bracketSameLine = - // Simple tags (no attributes and no comment in tag name) should be - // kept unbroken regardless of `bracketSameLine`. - // jsxBracketSameLine is deprecated in favour of bracketSameLine, - // but is still needed for backwards compatibility. - (node.attributes.length === 0 && !nameHasComments) || - ((options.bracketSameLine || options.jsxBracketSameLine) && - // We should print the bracket in a new line for the following cases: - //
- //
- (!nameHasComments || node.attributes.length > 0) && - !lastAttrHasTrailingComments); - // We should print the opening element expanded if any prop value is a // string literal with newlines const shouldBreak = @@ -617,13 +596,50 @@ function printJsxOpeningElement(path, options, print) { print("name"), print("typeParameters"), indent(path.map(() => [attributeLine, print()], "attributes")), - node.selfClosing ? line : bracketSameLine ? ">" : softline, - node.selfClosing ? "/>" : bracketSameLine ? "" : ">", + ...printEndOfOpeningTag(node, options, nameHasComments), ], { shouldBreak } ); } +function printEndOfOpeningTag(node, options, nameHasComments) { + if (node.selfClosing) { + return [line, "/>"]; + } + const bracketSameLine = shouldPrintBracketSameLine( + node, + options, + nameHasComments + ); + if (bracketSameLine) { + return [">"]; + } + return [softline, ">"]; +} + +function shouldPrintBracketSameLine(node, options, nameHasComments) { + const lastAttrHasTrailingComments = + node.attributes.length > 0 && + hasComment(getLast(node.attributes), CommentCheckFlags.Trailing); + return ( + // Simple tags (no attributes and no comment in tag name) should be + // kept unbroken regardless of `bracketSameLine`. + // jsxBracketSameLine is deprecated in favour of bracketSameLine, + // but is still needed for backwards compatibility. + (node.attributes.length === 0 && !nameHasComments) || + ((options.bracketSameLine || options.jsxBracketSameLine) && + // We should print the bracket in a new line for the following cases: + //
+ //
+ (!nameHasComments || node.attributes.length > 0) && + !lastAttrHasTrailingComments) + ); +} + function printJsxClosingElement(path, options, print) { const node = path.getValue(); const parts = [];