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-no-leaked-render autofixed JSXElements away in coerce strategy #3370

Merged
merged 1 commit into from
Aug 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

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

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

## [7.31.0] - 2022.08.24
Expand Down
18 changes: 18 additions & 0 deletions docs/rules/jsx-no-leaked-render.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ const Component = ({ elements }) => {
}
```

```jsx
const Component = ({ elements }) => {
return <div>{elements.length ? <List elements={elements}/> : <EmptyList />}</div>
}
```

## Rule Options

The supported options are:
Expand Down Expand Up @@ -183,6 +189,12 @@ const Component = ({ count, title }) => {
}
```

```jsx
const Component = ({ count, title, empty }) => {
return <div>{count ? title : empty}</div>
}
```

Assuming the following options: `{ "validStrategies": ["coerce"] }`

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

```jsx
const Component = ({ count, title, empty }) => {
return <div>{count ? title : empty}</div>
}
```

## When Not To Use It

If you are working in a typed-codebase which encourages you to always use boolean conditions, this rule can be disabled.
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/jsx-no-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ module.exports = {
}

const isValidTernaryAlternate = TERNARY_INVALID_ALTERNATE_VALUES.indexOf(node.alternate.value) === -1;
if (isValidTernaryAlternate) {
const isJSXElementAlternate = node.alternate.type === 'JSXElement';
if (isValidTernaryAlternate || isJSXElementAlternate) {
return;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/jsx-no-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ ruleTester.run('jsx-no-leaked-render', rule, {
`,
options: [{ validStrategies: ['coerce'] }],
},
// Fixes for:
// - https://github.com/jsx-eslint/eslint-plugin-react/issues/3354
{
code: `
const Component = ({ elements, count }) => {
return <div>{count ? <List elements={elements}/> : <EmptyList />}</div>
}
`,
options: [{ validStrategies: ['coerce', 'ternary'] }],
},
{
code: `
const Component = ({ elements, count }) => {
return <div>{count ? <List elements={elements}/> : <EmptyList />}</div>
}
`,
options: [{ validStrategies: ['coerce'] }],
},
]),

invalid: parsers.all([
Expand Down