Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] jsx-curly-brace-presence: ignore containers with comments #2900

Merged
merged 1 commit into from Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,7 +14,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`static-property-placement`]: do not report non-components ([#2893][] @golopot)
* [`no-array-index-key`]: support optional chaining ([#2897][] @SyMind)
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)
* [`jsx-curly-brace-presence`]: ignore containers with comments ([#2900][] @golopot)

[#2900]: https://github.com/yannickcr/eslint-plugin-react/pull/2900
[#2899]: https://github.com/yannickcr/eslint-plugin-react/issues/2899
[#2897]: https://github.com/yannickcr/eslint-plugin-react/pull/2897
[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
Expand Down
1 change: 1 addition & 0 deletions docs/rules/jsx-curly-brace-presence.md
Expand Up @@ -151,6 +151,7 @@ Examples of **correct** code for this rule, even when configured with `"never"`:
*/
<App>{' '}</App>
<App>{' '}</App>
<App>{/* comment */ <Bpp />}</App> // the comment makes the container necessary
```

## When Not To Use It
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -235,6 +235,11 @@ module.exports = {
const expression = JSXExpressionNode.expression;
const expressionType = expression.type;

// Curly braces containing comments are necessary
if (context.getSourceCode().getCommentsInside(JSXExpressionNode).length > 0) {
return;
}

if (
(expressionType === 'Literal' || expressionType === 'JSXText')
&& typeof expression.value === 'string'
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -417,6 +417,28 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
};
`,
options: [{props: 'never', children: 'never'}]
},
{
code: `<App>{/* comment */}</App>`
},
{
code: `<App>{/* comment */ <Foo />}</App>`
},
{
code: `<App>{/* comment */ 'foo'}</App>`
},
{
code: `<App prop={/* comment */ 'foo'} />`
},
{
code: `
<App>
{
// comment
<Foo />
}
</App>
`
}
],

Expand Down