Skip to content

Commit 5668f9d

Browse files
himanshu007-creatorljharb
authored andcommitted
[Fix] jsx-no-leaked-render: Don't report errors on empty strings if React >= v18
1 parent 12e9838 commit 5668f9d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1616
* [`sort-prop-types`]: restore autofixing ([#3452][] @ROSSROSALES)
1717
* [`no-unknown-property`]: do not check `fbs` elements ([#3494][] @brianogilvie)
1818
* [`jsx-newline`]: No newline between comments and jsx elements ([#3493][] @justmejulian)
19+
* [`jsx-no-leaked-render`]: Don't report errors on empty strings if React >= v18 ([#3488][] @himanshu007-creator)
1920

2021
[#3494]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3494
2122
[#3493]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3493
23+
[#3488]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3488
2224
[#3461]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3461
2325
[#3452]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3452
2426
[#3449]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3449

lib/rules/jsx-no-leaked-render.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const docsUrl = require('../util/docsUrl');
99
const report = require('../util/report');
10+
const testReactVersion = require('../util/version').testReactVersion;
1011
const isParenthesized = require('../util/ast').isParenthesized;
1112

1213
//------------------------------------------------------------------------------
@@ -130,6 +131,9 @@ module.exports = {
130131
}
131132
}
132133

134+
if (testReactVersion(context, '>= 18.0.0') && node.left.value === '') {
135+
return;
136+
}
133137
report(context, messages.noPotentialLeakedRender, 'noPotentialLeakedRender', {
134138
node,
135139
fix(fixer) {

tests/lib/rules/jsx-no-leaked-render.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,41 @@ ruleTester.run('jsx-no-leaked-render', rule, {
218218
},
219219
{
220220
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
221-
line: 6,
221+
line: 7,
222+
column: 14,
223+
},
224+
],
225+
output: `
226+
const Example = () => {
227+
return (
228+
<>
229+
{0 ? <Something/> : null}
230+
{'' ? <Something/> : null}
231+
{NaN ? <Something/> : null}
232+
</>
233+
)
234+
}
235+
`,
236+
settings: { react: { version: '17.999.999' } },
237+
},
238+
239+
{
240+
code: `
241+
const Example = () => {
242+
return (
243+
<>
244+
{0 && <Something/>}
245+
{'' && <Something/>}
246+
{NaN && <Something/>}
247+
</>
248+
)
249+
}
250+
`,
251+
features: ['fragment'],
252+
errors: [
253+
{
254+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
255+
line: 5,
222256
column: 14,
223257
},
224258
{
@@ -232,12 +266,13 @@ ruleTester.run('jsx-no-leaked-render', rule, {
232266
return (
233267
<>
234268
{0 ? <Something/> : null}
235-
{'' ? <Something/> : null}
269+
{'' && <Something/>}
236270
{NaN ? <Something/> : null}
237271
</>
238272
)
239273
}
240274
`,
275+
settings: { react: { version: '18.0.0' } },
241276
},
242277

243278
// Invalid tests with both strategies enabled (default)

0 commit comments

Comments
 (0)