Skip to content

Commit

Permalink
[Fix] jsx-no-leaked-render: Don't report errors on empty strings if…
Browse files Browse the repository at this point in the history
… React >= v18
  • Loading branch information
himanshu007-creator authored and ljharb committed Nov 14, 2022
1 parent 12e9838 commit 5668f9d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,9 +16,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`sort-prop-types`]: restore autofixing ([#3452][] @ROSSROSALES)
* [`no-unknown-property`]: do not check `fbs` elements ([#3494][] @brianogilvie)
* [`jsx-newline`]: No newline between comments and jsx elements ([#3493][] @justmejulian)
* [`jsx-no-leaked-render`]: Don't report errors on empty strings if React >= v18 ([#3488][] @himanshu007-creator)

[#3494]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3494
[#3493]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3493
[#3488]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3488
[#3461]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3461
[#3452]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3452
[#3449]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3449
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/jsx-no-leaked-render.js
Expand Up @@ -7,6 +7,7 @@

const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const testReactVersion = require('../util/version').testReactVersion;
const isParenthesized = require('../util/ast').isParenthesized;

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -130,6 +131,9 @@ module.exports = {
}
}

if (testReactVersion(context, '>= 18.0.0') && node.left.value === '') {
return;
}
report(context, messages.noPotentialLeakedRender, 'noPotentialLeakedRender', {
node,
fix(fixer) {
Expand Down
39 changes: 37 additions & 2 deletions tests/lib/rules/jsx-no-leaked-render.js
Expand Up @@ -218,7 +218,41 @@ ruleTester.run('jsx-no-leaked-render', rule, {
},
{
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
line: 6,
line: 7,
column: 14,
},
],
output: `
const Example = () => {
return (
<>
{0 ? <Something/> : null}
{'' ? <Something/> : null}
{NaN ? <Something/> : null}
</>
)
}
`,
settings: { react: { version: '17.999.999' } },
},

{
code: `
const Example = () => {
return (
<>
{0 && <Something/>}
{'' && <Something/>}
{NaN && <Something/>}
</>
)
}
`,
features: ['fragment'],
errors: [
{
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
line: 5,
column: 14,
},
{
Expand All @@ -232,12 +266,13 @@ ruleTester.run('jsx-no-leaked-render', rule, {
return (
<>
{0 ? <Something/> : null}
{'' ? <Something/> : null}
{'' && <Something/>}
{NaN ? <Something/> : null}
</>
)
}
`,
settings: { react: { version: '18.0.0' } },
},

// Invalid tests with both strategies enabled (default)
Expand Down

0 comments on commit 5668f9d

Please sign in to comment.