From 5b715eac1905703503b089686ccda989e33a5c7a Mon Sep 17 00:00:00 2001 From: Ika Date: Tue, 6 Nov 2018 21:51:58 +0800 Subject: [PATCH 1/3] test: add tests --- .../jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/jsx-text-wrap/test.js | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap index 254ad4e43f38..524c50df4d8b 100644 --- a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap @@ -452,6 +452,13 @@ this_really_should_split_across_lines =
before{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after
+ +let myDiv = ReactTestUtils.renderIntoDocument( +
+
, +
+
+); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Wrapping text x = ( @@ -1023,4 +1030,11 @@ this_really_should_split_across_lines = (
); +let myDiv = ReactTestUtils.renderIntoDocument( +
+
+ ,
+
+); + `; diff --git a/tests/jsx-text-wrap/test.js b/tests/jsx-text-wrap/test.js index 82be1c422e8b..f8cb62554435 100644 --- a/tests/jsx-text-wrap/test.js +++ b/tests/jsx-text-wrap/test.js @@ -449,3 +449,10 @@ this_really_should_split_across_lines =
before{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after{stuff}after
+ +let myDiv = ReactTestUtils.renderIntoDocument( +
+
, +
+
+); From 7427ec605f3955861201f00a03faaee4d401084e Mon Sep 17 00:00:00 2001 From: Ika Date: Tue, 6 Nov 2018 21:52:18 +0800 Subject: [PATCH 2/3] fix(jsx): do not move trailing char to the next line as leading char --- src/language-js/printer-estree.js | 28 +++++++++++++++---- .../__snapshots__/jsfmt.spec.js.snap | 16 ++++------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 998d9dd7b39c..7dcaafc5e9ee 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -5078,19 +5078,28 @@ 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; @@ -5133,8 +5142,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); @@ -5164,10 +5179,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 { diff --git a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap index 524c50df4d8b..e4e839bcdd00 100644 --- a/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx-text-wrap/__snapshots__/jsfmt.spec.js.snap @@ -780,11 +780,7 @@ line_after_br = ( line_after_br_2 = (
- A -
- B -
- C + A
B
C
); @@ -974,8 +970,7 @@ x = ( x = (
- text here. -
+ text here.
); @@ -995,8 +990,7 @@ x = ( {name}’s{" "} - Hello world. -
+ Hello world.
You {type}ed this shipment to
); @@ -1032,8 +1026,8 @@ this_really_should_split_across_lines = ( let myDiv = ReactTestUtils.renderIntoDocument(
-
- ,
+
, +
); From 78b8a01c386d573718288c0469749476e01552bb Mon Sep 17 00:00:00 2001 From: Ika Date: Tue, 6 Nov 2018 22:27:59 +0800 Subject: [PATCH 3/3] style: fix linting --- src/language-js/printer-estree.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 7dcaafc5e9ee..b3b2ea4ee52c 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -5096,8 +5096,7 @@ function separatorWithWhitespace( if (child.length === 1) { return (childNode.type === "JSXElement" && !childNode.closingElement) || - (nextNode && - (nextNode.type === "JSXElement" && !nextNode.closingElement)) + (nextNode && nextNode.type === "JSXElement" && !nextNode.closingElement) ? hardline : softline; }