Skip to content

Commit

Permalink
Small tidy up of JSX whitespace declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
karl committed Apr 23, 2017
1 parent eecf423 commit 2f22630
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/printer.js
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 2f22630

Please sign in to comment.