Skip to content

Commit

Permalink
Fix fill wrapping when text starts with whitespace (#1666)
Browse files Browse the repository at this point in the history
  • Loading branch information
karl authored and vjeux committed May 22, 2017
1 parent d608b7b commit 4083d92
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3585,6 +3585,11 @@ function printJSXElement(path, options, print) {
multilineChildren.push(rawJsxWhitespace);
return;
} else if (i === 0) {
// Fill expects alternating content & whitespace parts
// always starting with content.
// So we add a dummy content element if we would otherwise start
// with whitespace.
multilineChildren.push("");
multilineChildren.push(concat([rawJsxWhitespace, hardline]));
return;
} else if (i === children.length - 1) {
Expand Down
21 changes: 21 additions & 0 deletions tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ x =
Second
</div> Third
</div>
leading_whitespace =
<div> First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth</div>
no_leading_whitespace =
<div>First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth</div>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Wrapping text
x = (
Expand Down Expand Up @@ -241,4 +247,19 @@ x = (
</div>
);
leading_whitespace = (
<div>
{" "}
First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh
Twelfth Thirteenth Fourteenth
</div>
);
no_leading_whitespace = (
<div>
First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh
Twelfth Thirteenth Fourteenth
</div>
);
`;
6 changes: 6 additions & 0 deletions tests/jsx-text-wrap/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,9 @@ x =
Second
</div> Third
</div>

leading_whitespace =
<div> First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth</div>

no_leading_whitespace =
<div>First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth</div>

0 comments on commit 4083d92

Please sign in to comment.