Skip to content

Commit a8d5a41

Browse files
committed
[fix]jsx-curly-brace-presence: report unnecessary curly braces with children on next line"
1 parent 781bfff commit a8d5a41

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,21 @@ module.exports = {
205205
);
206206
}
207207

208+
function isWhiteSpaceLiteral(node) {
209+
return node.type && node.type === 'Literal' && node.value && !(/\S/.test(node.value));
210+
}
211+
212+
function hasManyChildren(children) {
213+
if (children.length === 1) return false;
214+
215+
const childrenExcludingWhitespaceLiteral = children.filter(child => !isWhiteSpaceLiteral(child));
216+
return childrenExcludingWhitespaceLiteral.length !== 1;
217+
}
218+
208219
function shouldCheckForUnnecessaryCurly(parent, config) {
209-
// If there are more than one JSX child, there is no need to check for
210-
// unnecessary curly braces.
211-
if (jsxUtil.isJSX(parent) && parent.children.length !== 1) {
220+
// If there are more than one JSX child excluding white-space `Literal`,
221+
// there is no need to check for unnecessary curly braces.
222+
if (jsxUtil.isJSX(parent) && hasManyChildren(parent.children)) {
212223
return false;
213224
}
214225

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
284284
\`}</App>
285285
`,
286286
options: ['always']
287+
},
288+
{
289+
code: `
290+
<MyComponent>
291+
%
292+
</MyComponent>
293+
`,
294+
parser: parsers.BABEL_ESLINT,
295+
options: [{children: 'never'}]
287296
}
288297
],
289298

@@ -335,6 +344,21 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
335344
options: [{props: 'never'}],
336345
errors: [{message: unnecessaryCurlyMessage}]
337346
},
347+
{
348+
code: `
349+
<MyComponent>
350+
{'%'}
351+
</MyComponent>
352+
`,
353+
output: `
354+
<MyComponent>
355+
%
356+
</MyComponent>
357+
`,
358+
parser: parsers.BABEL_ESLINT,
359+
options: [{children: 'never'}],
360+
errors: [{message: unnecessaryCurlyMessage}]
361+
},
338362
{
339363
code: `<MyComponent prop='bar'>foo</MyComponent>`,
340364
output: '<MyComponent prop={"bar"}>foo</MyComponent>',

0 commit comments

Comments
 (0)