From e9b82074a695bbb2df9885cd5731b5994b746cc8 Mon Sep 17 00:00:00 2001 From: Karl O'Keeffe Date: Tue, 3 Oct 2017 20:51:18 +0100 Subject: [PATCH] Collapse multiple JSX whitespaces --- src/printer.js | 7 ++++++- .../__snapshots__/jsfmt.spec.js.snap | 19 +++++++++++++++++++ tests/jsx-text-wrap/test.js | 9 +++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index 717886dd..db964092 100644 --- a/src/printer.js +++ b/src/printer.js @@ -4148,11 +4148,16 @@ function printJSXElement(path, options, print) { children[i] === jsxWhitespace && children[i + 1] === "" && (children[i + 2] === softline || children[i + 2] === hardline); + const isDoubleJSXWhitespace = + children[i] === jsxWhitespace && + children[i + 1] === "" && + children[i + 2] === jsxWhitespace; if ( (isPairOfHardlines && containsText) || isPairOfEmptyStrings || - isLineFollowedByJSXWhitespace + isLineFollowedByJSXWhitespace || + isDoubleJSXWhitespace ) { children.splice(i, 2); } else if (isJSXWhitespaceFollowedByLine) { diff --git a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap index 715f1524..39e66786 100644 --- a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap @@ -298,7 +298,16 @@ x = +x = +
+ {" "}
text
+
+// NOTE: Multiple JSX whitespaces are collapsed into a single space. +x = +
+ {" "}{" "}{" "} +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Wrapping text x = ( @@ -679,4 +688,14 @@ x = ( ); +x = ( +
+ {" "} +
text
+
+); + +// NOTE: Multiple JSX whitespaces are collapsed into a single space. +x =
; + `; diff --git a/tests/jsx-text-wrap/test.js b/tests/jsx-text-wrap/test.js index 5c5dc434..17f53818 100644 --- a/tests/jsx-text-wrap/test.js +++ b/tests/jsx-text-wrap/test.js @@ -295,4 +295,13 @@ x = +x = +
+ {" "}
text
+
+// NOTE: Multiple JSX whitespaces are collapsed into a single space. +x = +
+ {" "}{" "}{" "} +