From 4083d92ad1462780febb8c9bda74dfc6cf72d842 Mon Sep 17 00:00:00 2001 From: Karl O'Keeffe Date: Mon, 22 May 2017 23:32:57 +0100 Subject: [PATCH] Fix fill wrapping when text starts with whitespace (#1666) --- src/printer.js | 5 +++++ .../__snapshots__/jsfmt.spec.js.snap | 21 +++++++++++++++++++ tests/jsx-text-wrap/test.js | 6 ++++++ 3 files changed, 32 insertions(+) diff --git a/src/printer.js b/src/printer.js index 8e10244a..d28ade53 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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) { diff --git a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap index b1aaaa14..99d5777d 100644 --- a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap @@ -96,6 +96,12 @@ x = Second Third + +leading_whitespace = +
First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth
+ +no_leading_whitespace = +
First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Wrapping text x = ( @@ -241,4 +247,19 @@ x = ( ); +leading_whitespace = ( +
+ {" "} + First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh + Twelfth Thirteenth Fourteenth +
+); + +no_leading_whitespace = ( +
+ First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh + Twelfth Thirteenth Fourteenth +
+); + `; diff --git a/tests/jsx-text-wrap/test.js b/tests/jsx-text-wrap/test.js index 5a526643..d29ca3b6 100644 --- a/tests/jsx-text-wrap/test.js +++ b/tests/jsx-text-wrap/test.js @@ -93,3 +93,9 @@ x = Second Third + +leading_whitespace = +
First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth
+ +no_leading_whitespace = +
First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth