Skip to content

Commit 465ea48

Browse files
committed
[fix]jsx-curly-braces-presence: add check for adjacent JsxExpressionContainers instead of multiple children
1 parent 1e38e43 commit 465ea48

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,46 @@ module.exports = {
209209
return node.type && node.type === 'Literal' && node.value && !(/\S/.test(node.value));
210210
}
211211

212-
function hasManyChildren(children) {
212+
function getAdjacentSiblings(node, children) {
213+
for (let i = 1; i < children.length - 1; i++) {
214+
const child = children[i];
215+
if (node === child) {
216+
return [children[i - 1], children[i + 1]];
217+
}
218+
}
219+
if (node === children[0] && children[1]) {
220+
return [children[1]];
221+
}
222+
if (node === children[children.length - 1] && children[children.length - 2]) {
223+
return [children[children.length - 2]];
224+
}
225+
return [];
226+
}
227+
228+
function hasAdjacentJsxExpressionContainers(node, children) {
213229
const childrenExcludingWhitespaceLiteral = children.filter(child => !isWhiteSpaceLiteral(child));
214-
return childrenExcludingWhitespaceLiteral.length > 1;
230+
const adjSiblings = getAdjacentSiblings(node, childrenExcludingWhitespaceLiteral);
231+
232+
for (let i = 0; i < adjSiblings.length; i++) {
233+
const currentChild = adjSiblings[i];
234+
if (currentChild.type && currentChild.type === 'JSXExpressionContainer') {
235+
return true;
236+
}
237+
}
238+
return false;
215239
}
216240

217-
function shouldCheckForUnnecessaryCurly(parent, config) {
218-
// If there are more than one JSX child excluding white-space `Literal`,
219-
// there is no need to check for unnecessary curly braces.
220-
if (jsxUtil.isJSX(parent) && hasManyChildren(parent.children)) {
241+
function shouldCheckForUnnecessaryCurly(parent, node, config) {
242+
// If there are adjacent `JsxExpressionContainer` then there is no need,
243+
// to check for unnecessary curly braces.
244+
if (jsxUtil.isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) {
221245
return false;
222246
}
223247

224248
if (
225249
parent.children &&
226250
parent.children.length === 1 &&
227-
containsWhitespaceExpression(parent.children[0])
251+
containsWhitespaceExpression(node)
228252
) {
229253
return false;
230254
}
@@ -250,7 +274,7 @@ module.exports = {
250274

251275
return {
252276
JSXExpressionContainer: (node) => {
253-
if (shouldCheckForUnnecessaryCurly(node.parent, userConfig)) {
277+
if (shouldCheckForUnnecessaryCurly(node.parent, node, userConfig)) {
254278
lintUnnecessaryCurly(node);
255279
}
256280
},

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
293293
`,
294294
parser: parsers.BABEL_ESLINT,
295295
options: [{children: 'never'}]
296+
},
297+
{
298+
code: `
299+
<MyComponent>
300+
foo
301+
<div>bar</div>
302+
</MyComponent>
303+
`,
304+
parser: parsers.BABEL_ESLINT,
305+
options: [{children: 'never'}]
296306
}
297307
],
298308

@@ -366,19 +376,46 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
366376
<div>
367377
{'bar'}
368378
</div>
379+
{'baz'}
369380
</MyComponent>
370381
`,
371382
output: `
383+
<MyComponent>
384+
foo
385+
<div>
386+
bar
387+
</div>
388+
baz
389+
</MyComponent>
390+
`,
391+
parser: parsers.BABEL_ESLINT,
392+
options: [{children: 'never'}],
393+
errors: 3
394+
},
395+
{
396+
code: `
372397
<MyComponent>
373398
{'foo'}
399+
<div>
400+
{'bar'}
401+
</div>
402+
{'baz'}
403+
{'some-complicated-exp'}
404+
</MyComponent>
405+
`,
406+
output: `
407+
<MyComponent>
408+
foo
374409
<div>
375410
bar
376411
</div>
412+
{'baz'}
413+
{'some-complicated-exp'}
377414
</MyComponent>
378415
`,
379416
parser: parsers.BABEL_ESLINT,
380417
options: [{children: 'never'}],
381-
errors: [{message: unnecessaryCurlyMessage}]
418+
errors: 2
382419
},
383420
{
384421
code: `<MyComponent prop='bar'>foo</MyComponent>`,

0 commit comments

Comments
 (0)