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: invalid report if left side is boolean #3746

Merged
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
* [`boolean-prop-naming`]: avoid a crash with a non-TSTypeReference type ([#3718][] @developer-bandi)
* [`jsx-no-leaked-render`]: invalid report if left side is boolean ([#3746][] @akulsr0)

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

## [7.34.1] - 2024.03.15
Expand Down
12 changes: 12 additions & 0 deletions lib/rules/jsx-no-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const from = require('es-iterator-helpers/Iterator.from');

const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const variableUtil = require('../util/variable');
const testReactVersion = require('../util/version').testReactVersion;
const isParenthesized = require('../util/ast').isParenthesized;

Expand Down Expand Up @@ -160,6 +161,17 @@ module.exports = {
if (isCoerceValidLeftSide || getIsCoerceValidNestedLogicalExpression(leftSide)) {
return;
}
const variables = variableUtil.variablesInScope(context);
const leftSideVar = variableUtil.getVariable(variables, leftSide.name);
if (leftSideVar) {
const leftSideValue = leftSideVar.defs
&& leftSideVar.defs.length
&& leftSideVar.defs[0].node.init
&& leftSideVar.defs[0].node.init.value;
if (typeof leftSideValue === 'boolean') {
return;
}
}
}

if (testReactVersion(context, '>= 18') && leftSide.type === 'Literal' && leftSide.value === '') {
Expand Down
40 changes: 39 additions & 1 deletion tests/lib/rules/jsx-no-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ ruleTester.run('jsx-no-leaked-render', rule, {
`,
options: [{ validStrategies: ['coerce'] }],
},
{
code: `
const isOpen = true;
const Component = () => {
return <Popover open={isOpen && items.length > 0} />
}
`,
options: [{ validStrategies: ['coerce'] }],
},
{
code: `
const isOpen = false;
const Component = () => {
return <Popover open={isOpen && items.length > 0} />
}
`,
options: [{ validStrategies: ['coerce'] }],
},
]) || [],

invalid: parsers.all([].concat(
Expand Down Expand Up @@ -972,6 +990,26 @@ ruleTester.run('jsx-no-leaked-render', rule, {
line: 5,
column: 16,
}],
} : []
} : [],
{
code: `
const isOpen = 0;
const Component = () => {
return <Popover open={isOpen && items.length > 0} />
}
`,
output: `
const isOpen = 0;
const Component = () => {
return <Popover open={!!isOpen && items.length > 0} />
}
`,
options: [{ validStrategies: ['coerce'] }],
errors: [{
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
line: 4,
column: 33,
}],
}
)),
});
Loading