Skip to content

Commit 126c452

Browse files
sjarvaljharb
authored andcommitted
[Fix] jsx-no-leaked-render: coerce strategy now allows a ternary
1 parent bb4d1b3 commit 126c452

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

88
### Fixed
99
* [`jsx-key`]: fix detecting missing key in `Array.from`'s mapping function ([#3369][] @sjarva)
10+
* [`jsx-no-leaked-render`]: coerce strategy now allows a ternary ([#3370][], @sjarva)
1011

12+
[#3370]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3370
1113
[#3369]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3369
1214

1315
## [7.31.0] - 2022.08.24

docs/rules/jsx-no-leaked-render.md

+18
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ const Component = ({ elements }) => {
141141
}
142142
```
143143

144+
```jsx
145+
const Component = ({ elements }) => {
146+
return <div>{elements.length ? <List elements={elements}/> : <EmptyList />}</div>
147+
}
148+
```
149+
144150
## Rule Options
145151

146152
The supported options are:
@@ -183,6 +189,12 @@ const Component = ({ count, title }) => {
183189
}
184190
```
185191

192+
```jsx
193+
const Component = ({ count, title, empty }) => {
194+
return <div>{count ? title : empty}</div>
195+
}
196+
```
197+
186198
Assuming the following options: `{ "validStrategies": ["coerce"] }`
187199

188200
Examples of **incorrect** code for this rule, with the above configuration:
@@ -207,6 +219,12 @@ const Component = ({ count, title }) => {
207219
}
208220
```
209221

222+
```jsx
223+
const Component = ({ count, title, empty }) => {
224+
return <div>{count ? title : empty}</div>
225+
}
226+
```
227+
210228
## When Not To Use It
211229

212230
If you are working in a typed-codebase which encourages you to always use boolean conditions, this rule can be disabled.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ module.exports = {
144144
}
145145

146146
const isValidTernaryAlternate = TERNARY_INVALID_ALTERNATE_VALUES.indexOf(node.alternate.value) === -1;
147-
if (isValidTernaryAlternate) {
147+
const isJSXElementAlternate = node.alternate.type === 'JSXElement';
148+
if (isValidTernaryAlternate || isJSXElementAlternate) {
148149
return;
149150
}
150151

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

+18
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ ruleTester.run('jsx-no-leaked-render', rule, {
175175
`,
176176
options: [{ validStrategies: ['coerce'] }],
177177
},
178+
// Fixes for:
179+
// - https://github.com/jsx-eslint/eslint-plugin-react/issues/3354
180+
{
181+
code: `
182+
const Component = ({ elements, count }) => {
183+
return <div>{count ? <List elements={elements}/> : <EmptyList />}</div>
184+
}
185+
`,
186+
options: [{ validStrategies: ['coerce', 'ternary'] }],
187+
},
188+
{
189+
code: `
190+
const Component = ({ elements, count }) => {
191+
return <div>{count ? <List elements={elements}/> : <EmptyList />}</div>
192+
}
193+
`,
194+
options: [{ validStrategies: ['coerce'] }],
195+
},
178196
]),
179197

180198
invalid: parsers.all([

0 commit comments

Comments
 (0)