Skip to content

Commit

Permalink
[Fix] jsx-no-leaked-render: prevent wrongly adding parens
Browse files Browse the repository at this point in the history
  • Loading branch information
developer-bandi authored and ljharb committed Mar 4, 2024
1 parent b7780ce commit 730bac9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
* [`jsx-no-leaked-render`]: prevent wrongly adding parens ([#3700][] @developer-bandi)

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

## [7.34.0] - 2024.03.03

### Added
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function ruleFixer(context, fixStrategy, fixer, reportedNode, leftNode, rightNod
return fixer.replaceText(reportedNode, `${newText} && ${alternateVal}`);
}

if (rightNode.type === 'ConditionalExpression') {
if (rightNode.type === 'ConditionalExpression' || rightNode.type === 'LogicalExpression') {
return fixer.replaceText(reportedNode, `${newText} && (${rightSideText})`);
}
if (rightNode.type === 'JSXElement') {
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 @@ -733,6 +733,24 @@ ruleTester.run('jsx-no-leaked-render', rule, {
}
`,
},
{
code: `
const Component = ({ connection, hasError, hasErrorUpdate}) => {
return <div>{connection && (hasError || hasErrorUpdate)}</div>
}
`,
options: [{ validStrategies: ['coerce'] }],
errors: [{
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
line: 3,
column: 24,
}],
output: `
const Component = ({ connection, hasError, hasErrorUpdate}) => {
return <div>{!!connection && (hasError || hasErrorUpdate)}</div>
}
`,
},

// cases: ternary isn't valid if strategy is only "coerce"
{
Expand Down

0 comments on commit 730bac9

Please sign in to comment.