Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsx): do not move trailing char to the next line as leading char #5354

Merged
merged 3 commits into from Nov 6, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/language-js/printer-estree.js
Expand Up @@ -5078,19 +5078,27 @@ function separatorNoWhitespace(
(childNode.type === "JSXElement" && !childNode.closingElement) ||
(nextNode && (nextNode.type === "JSXElement" && !nextNode.closingElement))
) {
return hardline;
return child.length === 1 ? softline : hardline;
}

return softline;
}

function separatorWithWhitespace(isFacebookTranslationTag, child) {
function separatorWithWhitespace(
isFacebookTranslationTag,
child,
childNode,
nextNode
) {
if (isFacebookTranslationTag) {
return hardline;
}

if (child.length === 1) {
return softline;
return (childNode.type === "JSXElement" && !childNode.closingElement) ||
(nextNode && nextNode.type === "JSXElement" && !nextNode.closingElement)
? hardline
: softline;
}

return hardline;
Expand Down Expand Up @@ -5133,8 +5141,14 @@ function printJSXChildren(
children.push("");
words.shift();
if (/\n/.test(words[0])) {
const next = n.children[i + 1];
children.push(
separatorWithWhitespace(isFacebookTranslationTag, words[1])
separatorWithWhitespace(
isFacebookTranslationTag,
words[1],
child,
next
)
);
} else {
children.push(jsxWhitespace);
Expand Down Expand Up @@ -5164,10 +5178,13 @@ function printJSXChildren(

if (endWhitespace !== undefined) {
if (/\n/.test(endWhitespace)) {
const next = n.children[i + 1];
children.push(
separatorWithWhitespace(
isFacebookTranslationTag,
getLast(children)
getLast(children),
child,
next
)
);
} else {
Expand Down
26 changes: 17 additions & 9 deletions tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -452,6 +452,13 @@ this_really_should_split_across_lines =
<div>
before{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after
</div>

let myDiv = ReactTestUtils.renderIntoDocument(
<div>
<div key="theDog" className="dog" />,
<div key="theBird" className="bird" />
</div>
);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Wrapping text
x = (
Expand Down Expand Up @@ -773,11 +780,7 @@ line_after_br = (

line_after_br_2 = (
<div>
A
<br />
B
<br />
C
A<br />B<br />C
</div>
);

Expand Down Expand Up @@ -967,8 +970,7 @@ x = (

x = (
<div>
<strong>text here</strong>.
<br />
<strong>text here</strong>.<br />
</div>
);

Expand All @@ -988,8 +990,7 @@ x = (
<span>
<strong>{name}</strong>’s{" "}
</span>
Hello <strong>world</strong>.
<br />
Hello <strong>world</strong>.<br />
<Text>You {type}ed this shipment to</Text>
</div>
);
Expand Down Expand Up @@ -1023,4 +1024,11 @@ this_really_should_split_across_lines = (
</div>
);

let myDiv = ReactTestUtils.renderIntoDocument(
<div>
<div key="theDog" className="dog" />,
<div key="theBird" className="bird" />
</div>
);

`;
7 changes: 7 additions & 0 deletions tests/jsx-text-wrap/test.js
Expand Up @@ -449,3 +449,10 @@ this_really_should_split_across_lines =
<div>
before{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after
</div>

let myDiv = ReactTestUtils.renderIntoDocument(
<div>
<div key="theDog" className="dog" />,
<div key="theBird" className="bird" />
</div>
);