Skip to content

Commit ae3dd17

Browse files
authored
Refactor: Extract printEndOfOpeningTag (#13086)
* Refactor * Rename * Fix typo
1 parent 6bc6914 commit ae3dd17

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

src/language-js/print/jsx.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -574,27 +574,6 @@ function printJsxOpeningElement(path, options, print) {
574574
]);
575575
}
576576

577-
const lastAttrHasTrailingComments =
578-
node.attributes.length > 0 &&
579-
hasComment(getLast(node.attributes), CommentCheckFlags.Trailing);
580-
581-
const bracketSameLine =
582-
// Simple tags (no attributes and no comment in tag name) should be
583-
// kept unbroken regardless of `bracketSameLine`.
584-
// jsxBracketSameLine is deprecated in favour of bracketSameLine,
585-
// but is still needed for backwards compatibility.
586-
(node.attributes.length === 0 && !nameHasComments) ||
587-
((options.bracketSameLine || options.jsxBracketSameLine) &&
588-
// We should print the bracket in a new line for the following cases:
589-
// <div
590-
// // comment
591-
// >
592-
// <div
593-
// attr // comment
594-
// >
595-
(!nameHasComments || node.attributes.length > 0) &&
596-
!lastAttrHasTrailingComments);
597-
598577
// We should print the opening element expanded if any prop value is a
599578
// string literal with newlines
600579
const shouldBreak =
@@ -617,13 +596,50 @@ function printJsxOpeningElement(path, options, print) {
617596
print("name"),
618597
print("typeParameters"),
619598
indent(path.map(() => [attributeLine, print()], "attributes")),
620-
node.selfClosing ? line : bracketSameLine ? ">" : softline,
621-
node.selfClosing ? "/>" : bracketSameLine ? "" : ">",
599+
...printEndOfOpeningTag(node, options, nameHasComments),
622600
],
623601
{ shouldBreak }
624602
);
625603
}
626604

605+
function printEndOfOpeningTag(node, options, nameHasComments) {
606+
if (node.selfClosing) {
607+
return [line, "/>"];
608+
}
609+
const bracketSameLine = shouldPrintBracketSameLine(
610+
node,
611+
options,
612+
nameHasComments
613+
);
614+
if (bracketSameLine) {
615+
return [">"];
616+
}
617+
return [softline, ">"];
618+
}
619+
620+
function shouldPrintBracketSameLine(node, options, nameHasComments) {
621+
const lastAttrHasTrailingComments =
622+
node.attributes.length > 0 &&
623+
hasComment(getLast(node.attributes), CommentCheckFlags.Trailing);
624+
return (
625+
// Simple tags (no attributes and no comment in tag name) should be
626+
// kept unbroken regardless of `bracketSameLine`.
627+
// jsxBracketSameLine is deprecated in favour of bracketSameLine,
628+
// but is still needed for backwards compatibility.
629+
(node.attributes.length === 0 && !nameHasComments) ||
630+
((options.bracketSameLine || options.jsxBracketSameLine) &&
631+
// We should print the bracket in a new line for the following cases:
632+
// <div
633+
// // comment
634+
// >
635+
// <div
636+
// attr // comment
637+
// >
638+
(!nameHasComments || node.attributes.length > 0) &&
639+
!lastAttrHasTrailingComments)
640+
);
641+
}
642+
627643
function printJsxClosingElement(path, options, print) {
628644
const node = path.getValue();
629645
const parts = [];

0 commit comments

Comments
 (0)