Skip to content

Commit

Permalink
Refactor: Extract printEndOfOpeningTag (prettier#13086)
Browse files Browse the repository at this point in the history
* Refactor

* Rename

* Fix typo
  • Loading branch information
sosukesuzuki authored and medikoo committed Jan 4, 2024
1 parent 4b82672 commit 6d4007a
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions src/language-js/print/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// <div
// // comment
// >
// <div
// attr // comment
// >
(!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 =
Expand All @@ -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:
// <div
// // comment
// >
// <div
// attr // comment
// >
(!nameHasComments || node.attributes.length > 0) &&
!lastAttrHasTrailingComments)
);
}

function printJsxClosingElement(path, options, print) {
const node = path.getValue();
const parts = [];
Expand Down

0 comments on commit 6d4007a

Please sign in to comment.