Skip to content

Commit a1dee7c

Browse files
Cory Brownljharb
Cory Brown
authored andcommitted
[fix] jsx-curly-brace-presence: allow necessary white-space literal
1 parent d7d2a01 commit a1dee7c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/rules/jsx-curly-brace-presence.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'use strict';
88

9+
const arrayIncludes = require('array-includes');
10+
911
const docsUrl = require('../util/docsUrl');
1012
const jsxUtil = require('../util/jsx');
1113

@@ -70,7 +72,7 @@ module.exports = {
7072
}
7173

7274
function containsBackslash(rawStringValue) {
73-
return rawStringValue.includes('\\');
75+
return arrayIncludes(rawStringValue, '\\');
7476
}
7577

7678
function containsHTMLEntity(rawStringValue) {
@@ -238,7 +240,12 @@ module.exports = {
238240

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

247+
return adjSiblings.some(x => x.type && arrayIncludes(['JSXExpressionContainer', 'JSXElement'], x.type));
248+
}
242249
function shouldCheckForUnnecessaryCurly(parent, node, config) {
243250
// Bail out if the parent is a JSXAttribute & its contents aren't
244251
// StringLiteral or TemplateLiteral since e.g
@@ -259,7 +266,9 @@ module.exports = {
259266
if (jsxUtil.isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) {
260267
return false;
261268
}
262-
269+
if (containsWhitespaceExpression(node) && hasAdjacentJsx(node, parent.children)) {
270+
return false;
271+
}
263272
if (
264273
parent.children &&
265274
parent.children.length === 1 &&

tests/lib/rules/jsx-curly-brace-presence.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
8585
code: '<App>{`Hello ${word} World`}</App>',
8686
options: [{children: 'never'}]
8787
},
88+
{
89+
code: `
90+
<React.Fragment>
91+
foo{' '}
92+
<span>bar</span>
93+
</React.Fragment>
94+
`,
95+
options: [{children: 'never'}]
96+
},
97+
{
98+
code: `
99+
<>
100+
foo{' '}
101+
<span>bar</span>
102+
</>
103+
`,
104+
parser: parsers.BABEL_ESLINT,
105+
options: [{children: 'never'}]
106+
},
88107
{
89108
code: '<App>{`Hello \\n World`}</App>',
90109
options: [{children: 'never'}]

0 commit comments

Comments
 (0)