Skip to content

Commit 0e9a193

Browse files
chiawendtljharb
authored andcommitted
[Fix] jsx-curly-brace-presence: ignore containers with comments
Fixes #2885
1 parent f34ee91 commit 0e9a193

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1414
* [`static-property-placement`]: do not report non-components ([#2893][] @golopot)
1515
* [`no-array-index-key`]: support optional chaining ([#2897][] @SyMind)
1616
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)
17+
* [`jsx-curly-brace-presence`]: ignore containers with comments ([#2900][] @golopot)
1718

19+
[#2900]: https://github.com/yannickcr/eslint-plugin-react/pull/2900
1820
[#2899]: https://github.com/yannickcr/eslint-plugin-react/issues/2899
1921
[#2897]: https://github.com/yannickcr/eslint-plugin-react/pull/2897
2022
[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895

docs/rules/jsx-curly-brace-presence.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Examples of **correct** code for this rule, even when configured with `"never"`:
151151
*/
152152
<App>{' '}</App>
153153
<App>{' '}</App>
154+
<App>{/* comment */ <Bpp />}</App> // the comment makes the container necessary
154155
```
155156

156157
## When Not To Use It

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ module.exports = {
235235
const expression = JSXExpressionNode.expression;
236236
const expressionType = expression.type;
237237

238+
// Curly braces containing comments are necessary
239+
if (context.getSourceCode().getCommentsInside(JSXExpressionNode).length > 0) {
240+
return;
241+
}
242+
238243
if (
239244
(expressionType === 'Literal' || expressionType === 'JSXText')
240245
&& typeof expression.value === 'string'

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,28 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
417417
};
418418
`,
419419
options: [{props: 'never', children: 'never'}]
420+
},
421+
{
422+
code: `<App>{/* comment */}</App>`
423+
},
424+
{
425+
code: `<App>{/* comment */ <Foo />}</App>`
426+
},
427+
{
428+
code: `<App>{/* comment */ 'foo'}</App>`
429+
},
430+
{
431+
code: `<App prop={/* comment */ 'foo'} />`
432+
},
433+
{
434+
code: `
435+
<App>
436+
{
437+
// comment
438+
<Foo />
439+
}
440+
</App>
441+
`
420442
}
421443
],
422444

0 commit comments

Comments
 (0)