Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsx-curly-brace-presence): allow necessary white-space literal #2437

Merged
merged 2 commits into from Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -6,6 +6,8 @@

'use strict';

const arrayIncludes = require('array-includes');

const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');

Expand Down Expand Up @@ -70,7 +72,7 @@ module.exports = {
}

function containsBackslash(rawStringValue) {
return rawStringValue.includes('\\');
return arrayIncludes(rawStringValue, '\\');
}

function containsHTMLEntity(rawStringValue) {
Expand Down Expand Up @@ -238,7 +240,12 @@ module.exports = {

return adjSiblings.some(x => x.type && x.type === 'JSXExpressionContainer');
}
function hasAdjacentJsx(node, children) {
const childrenExcludingWhitespaceLiteral = children.filter(child => !isWhiteSpaceLiteral(child));
const adjSiblings = getAdjacentSiblings(node, childrenExcludingWhitespaceLiteral);

return adjSiblings.some(x => x.type && arrayIncludes(['JSXExpressionContainer', 'JSXElement'], x.type));
}
function shouldCheckForUnnecessaryCurly(parent, node, config) {
// Bail out if the parent is a JSXAttribute & its contents aren't
// StringLiteral or TemplateLiteral since e.g
Expand All @@ -259,7 +266,9 @@ module.exports = {
if (jsxUtil.isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) {
return false;
}

if (containsWhitespaceExpression(node) && hasAdjacentJsx(node, parent.children)) {
return false;
}
if (
parent.children &&
parent.children.length === 1 &&
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/jsx-no-useless-fragment.js
Expand Up @@ -4,6 +4,8 @@

'use strict';

const arrayIncludes = require('array-includes');

const pragmaUtil = require('../util/pragma');
const jsxUtil = require('../util/jsx');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -47,8 +49,8 @@ function trimLikeReact(text) {
const leadingSpaces = /^\s*/.exec(text)[0];
const trailingSpaces = /\s*$/.exec(text)[0];

const start = leadingSpaces.includes('\n') ? leadingSpaces.length : 0;
const end = trailingSpaces.includes('\n') ? text.length - trailingSpaces.length : text.length;
const start = arrayIncludes(leadingSpaces, '\n') ? leadingSpaces.length : 0;
const end = arrayIncludes(trailingSpaces, '\n') ? text.length - trailingSpaces.length : text.length;

return text.slice(start, end);
}
Expand Down Expand Up @@ -92,7 +94,7 @@ module.exports = {
function isPaddingSpaces(node) {
return isJSXText(node) &&
isOnlyWhitespace(node.raw) &&
node.raw.includes('\n');
arrayIncludes(node.raw, '\n');
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -85,6 +85,25 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
code: '<App>{`Hello ${word} World`}</App>',
options: [{children: 'never'}]
},
{
code: `
<React.Fragment>
foo{' '}
<span>bar</span>
</React.Fragment>
`,
options: [{children: 'never'}]
},
{
code: `
<>
uniqname marked this conversation as resolved.
Show resolved Hide resolved
foo{' '}
<span>bar</span>
</>
uniqname marked this conversation as resolved.
Show resolved Hide resolved
`,
parser: parsers.BABEL_ESLINT,
options: [{children: 'never'}]
},
{
code: '<App>{`Hello \\n World`}</App>',
options: [{children: 'never'}]
Expand Down