Skip to content

Commit a4e8aaf

Browse files
authored
fix(jsx): do not move trailing char to the next line as leading char (#5354)
1 parent f9bce75 commit a4e8aaf

3 files changed

Lines changed: 46 additions & 14 deletions

File tree

src/language-js/printer-estree.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5078,19 +5078,27 @@ function separatorNoWhitespace(
50785078
(childNode.type === "JSXElement" && !childNode.closingElement) ||
50795079
(nextNode && (nextNode.type === "JSXElement" && !nextNode.closingElement))
50805080
) {
5081-
return hardline;
5081+
return child.length === 1 ? softline : hardline;
50825082
}
50835083

50845084
return softline;
50855085
}
50865086

5087-
function separatorWithWhitespace(isFacebookTranslationTag, child) {
5087+
function separatorWithWhitespace(
5088+
isFacebookTranslationTag,
5089+
child,
5090+
childNode,
5091+
nextNode
5092+
) {
50885093
if (isFacebookTranslationTag) {
50895094
return hardline;
50905095
}
50915096

50925097
if (child.length === 1) {
5093-
return softline;
5098+
return (childNode.type === "JSXElement" && !childNode.closingElement) ||
5099+
(nextNode && nextNode.type === "JSXElement" && !nextNode.closingElement)
5100+
? hardline
5101+
: softline;
50945102
}
50955103

50965104
return hardline;
@@ -5133,8 +5141,14 @@ function printJSXChildren(
51335141
children.push("");
51345142
words.shift();
51355143
if (/\n/.test(words[0])) {
5144+
const next = n.children[i + 1];
51365145
children.push(
5137-
separatorWithWhitespace(isFacebookTranslationTag, words[1])
5146+
separatorWithWhitespace(
5147+
isFacebookTranslationTag,
5148+
words[1],
5149+
child,
5150+
next
5151+
)
51385152
);
51395153
} else {
51405154
children.push(jsxWhitespace);
@@ -5164,10 +5178,13 @@ function printJSXChildren(
51645178

51655179
if (endWhitespace !== undefined) {
51665180
if (/\n/.test(endWhitespace)) {
5181+
const next = n.children[i + 1];
51675182
children.push(
51685183
separatorWithWhitespace(
51695184
isFacebookTranslationTag,
5170-
getLast(children)
5185+
getLast(children),
5186+
child,
5187+
next
51715188
)
51725189
);
51735190
} else {

tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ this_really_should_split_across_lines =
452452
<div>
453453
before{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after
454454
</div>
455+
456+
let myDiv = ReactTestUtils.renderIntoDocument(
457+
<div>
458+
<div key="theDog" className="dog" />,
459+
<div key="theBird" className="bird" />
460+
</div>
461+
);
455462
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
456463
// Wrapping text
457464
x = (
@@ -773,11 +780,7 @@ line_after_br = (
773780
774781
line_after_br_2 = (
775782
<div>
776-
A
777-
<br />
778-
B
779-
<br />
780-
C
783+
A<br />B<br />C
781784
</div>
782785
);
783786
@@ -967,8 +970,7 @@ x = (
967970
968971
x = (
969972
<div>
970-
<strong>text here</strong>.
971-
<br />
973+
<strong>text here</strong>.<br />
972974
</div>
973975
);
974976
@@ -988,8 +990,7 @@ x = (
988990
<span>
989991
<strong>{name}</strong>’s{" "}
990992
</span>
991-
Hello <strong>world</strong>.
992-
<br />
993+
Hello <strong>world</strong>.<br />
993994
<Text>You {type}ed this shipment to</Text>
994995
</div>
995996
);
@@ -1023,4 +1024,11 @@ this_really_should_split_across_lines = (
10231024
</div>
10241025
);
10251026
1027+
let myDiv = ReactTestUtils.renderIntoDocument(
1028+
<div>
1029+
<div key="theDog" className="dog" />,
1030+
<div key="theBird" className="bird" />
1031+
</div>
1032+
);
1033+
10261034
`;

tests/jsx-text-wrap/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,10 @@ this_really_should_split_across_lines =
449449
<div>
450450
before{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after
451451
</div>
452+
453+
let myDiv = ReactTestUtils.renderIntoDocument(
454+
<div>
455+
<div key="theDog" className="dog" />,
456+
<div key="theBird" className="bird" />
457+
</div>
458+
);

0 commit comments

Comments
 (0)