Skip to content

Commit 156e9f5

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

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ module.exports = {
238238

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

245+
return adjSiblings.some(x => x.type && ['JSXExpressionContainer', 'JSXElement'].includes(x.type));
246+
}
242247
function shouldCheckForUnnecessaryCurly(parent, node, config) {
243248
// Bail out if the parent is a JSXAttribute & its contents aren't
244249
// StringLiteral or TemplateLiteral since e.g
@@ -259,7 +264,9 @@ module.exports = {
259264
if (jsxUtil.isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) {
260265
return false;
261266
}
262-
267+
if (containsWhitespaceExpression(node) && hasAdjacentJsx(node, parent.children)) {
268+
return false;
269+
}
263270
if (
264271
parent.children &&
265272
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)