Skip to content

Commit

Permalink
[fix] jsx-curly-brace-presence: allow necessary white-space literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Brown authored and ljharb committed Oct 2, 2019
1 parent d7d2a01 commit a1dee7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
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
19 changes: 19 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
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: `
<>
foo{' '}
<span>bar</span>
</>
`,
parser: parsers.BABEL_ESLINT,
options: [{children: 'never'}]
},
{
code: '<App>{`Hello \\n World`}</App>',
options: [{children: 'never'}]
Expand Down

0 comments on commit a1dee7c

Please sign in to comment.