From 2f226303d883637b4fedb0f70cc8081483cc9c9f Mon Sep 17 00:00:00 2001 From: Karl O'Keeffe Date: Sun, 23 Apr 2017 21:43:22 +0100 Subject: [PATCH] Small tidy up of JSX whitespace declarations --- src/printer.js | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/printer.js b/src/printer.js index d4ad45d4b005..02b9d038fc25 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2894,7 +2894,7 @@ function isEmptyJSXElement(node) { // // For another, leading, trailing, and lone whitespace all need to // turn themselves into the rather ugly `{' '}` when breaking. -function printJSXChildren(path, options, print, jsxWhitespace) { +function printJSXChildren(path, options, print, innerJsxWhitespace) { const n = path.getValue(); const children = []; @@ -2934,7 +2934,7 @@ function printJSXChildren(path, options, print, jsxWhitespace) { const beginSpace = /^\s+/.test(textLine); if (beginSpace) { - children.push(jsxWhitespace); + children.push(innerJsxWhitespace); } else { children.push(softline); } @@ -2955,7 +2955,7 @@ function printJSXChildren(path, options, print, jsxWhitespace) { const endSpace = /\s+$/.test(textLine); if (endSpace) { - children.push(jsxWhitespace); + children.push(innerJsxWhitespace); } else { children.push(softline); } @@ -2970,7 +2970,7 @@ function printJSXChildren(path, options, print, jsxWhitespace) { } else if (/\s/.test(value)) { // whitespace-only without newlines, // eg; a single space separating two elements - children.push(jsxWhitespace); + children.push(innerJsxWhitespace); } } else { children.push(print(childPath)); @@ -3036,23 +3036,13 @@ function printJSXElement(path, options, print) { // Record any breaks. Should never go from true to false, only false to true. let forcedBreak = willBreak(openingLines); - const jsxWhitespace = options.singleQuote - ? ifBreak(concat([softline, "{' '}", softline]), " ") - : ifBreak(concat([softline, '{" "}', softline]), " "); + const jsxWhitespace = options.singleQuote ? "{' '}" : '{" "}'; + const innerJsxWhitespace = ifBreak(concat([softline, jsxWhitespace, softline]), " ") + const leadingJsxWhitespace = ifBreak(concat([jsxWhitespace, softline]), " "); + const trailingJsxWhitespace = ifBreak(concat([softline, jsxWhitespace]), " "); + const solitaryJsxWhitespace = ifBreak(jsxWhitespace, " "); - const leadingJsxWhitespace = options.singleQuote - ? ifBreak(concat(["{' '}", softline]), " ") - : ifBreak(concat(['{" "}', softline]), " "); - - const trailingJsxWhitespace = options.singleQuote - ? ifBreak(concat([softline, "{' '}"]), " ") - : ifBreak(concat([softline, '{" "}']), " "); - - const solitaryJsxWhitespace = options.singleQuote - ? ifBreak(concat(["{' '}"]), " ") - : ifBreak(concat(['{" "}']), " "); - - const children = printJSXChildren(path, options, print, jsxWhitespace); + const children = printJSXChildren(path, options, print, innerJsxWhitespace); // Trim trailing lines, recording if there was a hardline let numTrailingHard = 0; @@ -3086,7 +3076,7 @@ function printJSXElement(path, options, print) { let groups = [[]]; // Initialize the first line's group children.forEach((child, i) => { // leading and trailing JSX whitespace don't go into a group - if (child === jsxWhitespace) { + if (child === innerJsxWhitespace) { if (i === 0) { if (children.length === 1) { groups.push(solitaryJsxWhitespace); @@ -3117,13 +3107,13 @@ function printJSXElement(path, options, print) { } }); - if (children[0] === jsxWhitespace) { + if (children[0] === innerJsxWhitespace) { children[0] = leadingJsxWhitespace; if (children.length === 1) { children[0] = solitaryJsxWhitespace; } } - if (children[children.length - 1] === jsxWhitespace) { + if (children[children.length - 1] === innerJsxWhitespace) { children[children.length - 1] = trailingJsxWhitespace; }