Skip to content

Commit c02d4e3

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 c02d4e3

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
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') && leftSide.type === 'Literal' && leftSide.value === '') {
135+
return;
136+
}
133137
report(context, messages.noPotentialLeakedRender, 'noPotentialLeakedRender', {
134138
node,
135139
fix(fixer) {

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

+61-21
Original file line numberDiff line numberDiff line change
@@ -199,45 +199,85 @@ ruleTester.run('jsx-no-leaked-render', rule, {
199199
// Common invalid cases with default options
200200
{
201201
code: `
202-
const Example = () => {
203-
return (
204-
<>
205-
{0 && <Something/>}
206-
{'' && <Something/>}
207-
{NaN && <Something/>}
208-
</>
209-
)
210-
}
202+
const Example = () => {
203+
return (
204+
<>
205+
{0 && <Something/>}
206+
{'' && <Something/>}
207+
{NaN && <Something/>}
208+
</>
209+
)
210+
}
211211
`,
212212
features: ['fragment'],
213213
errors: [
214214
{
215215
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
216216
line: 5,
217-
column: 14,
217+
column: 16,
218218
},
219219
{
220220
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
221221
line: 6,
222-
column: 14,
222+
column: 16,
223+
},
224+
{
225+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
226+
line: 7,
227+
column: 16,
228+
},
229+
],
230+
output: `
231+
const Example = () => {
232+
return (
233+
<>
234+
{0 ? <Something/> : null}
235+
{'' ? <Something/> : null}
236+
{NaN ? <Something/> : null}
237+
</>
238+
)
239+
}
240+
`,
241+
settings: { react: { version: '17.999.999' } },
242+
},
243+
244+
{
245+
code: `
246+
const Example = () => {
247+
return (
248+
<>
249+
{0 && <Something/>}
250+
{'' && <Something/>}
251+
{NaN && <Something/>}
252+
</>
253+
)
254+
}
255+
`,
256+
features: ['fragment'],
257+
errors: [
258+
{
259+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
260+
line: 5,
261+
column: 16,
223262
},
224263
{
225264
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
226265
line: 7,
227-
column: 14,
266+
column: 16,
228267
},
229268
],
230269
output: `
231-
const Example = () => {
232-
return (
233-
<>
234-
{0 ? <Something/> : null}
235-
{'' ? <Something/> : null}
236-
{NaN ? <Something/> : null}
237-
</>
238-
)
239-
}
270+
const Example = () => {
271+
return (
272+
<>
273+
{0 ? <Something/> : null}
274+
{'' && <Something/>}
275+
{NaN ? <Something/> : null}
276+
</>
277+
)
278+
}
240279
`,
280+
settings: { react: { version: '18.0.0' } },
241281
},
242282

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

0 commit comments

Comments
 (0)