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: prevent wrongly adding parens #3700

Merged
merged 1 commit into from
Mar 4, 2024
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
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
Loading