Skip to content

Commit 0dac159

Browse files
akulsr0ljharb
authored andcommitted
[Fix] jsx-no-leaked-render: invalid fixes in coerce mode
1 parent 85ae820 commit 0dac159

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1919
* [`jsx-no-leaked-render`]: Don't report errors on empty strings if React >= v18 ([#3488][] @himanshu007-creator)
2020
* [`no-invalid-html-attribute`]: convert autofix to suggestion ([#3474][] @himanshu007-creator @ljharb)
2121
* [`jsx-no-leaked-render`]: fix removing parentheses for conditionals ([#3502][] @akulsr0)
22+
* [`jsx-no-leaked-render`]: invalid fixes in coerce mode ([#3511][] @akulsr0)
2223

2324
### Changed
2425
* [Docs] [`jsx-no-leaked-render`]: Remove mentions of empty strings for React 18 ([#3468][] @karlhorky)
@@ -28,6 +29,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2829
* [Docs] [`prefer-exact-props`]: fix example flow syntax ([#3510][] @smackfu)
2930
* [Perf] use `anyOf` instead of `oneOf` (@ljharb @remcohaszing)
3031

32+
[#3511]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3511
3133
[#3510]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3510
3234
[#3504]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3504
3335
[#3499]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3499

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

+12
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,21 @@ function ruleFixer(context, fixStrategy, fixer, reportedNode, leftNode, rightNod
6161
if (isParenthesized(context, node)) {
6262
nodeText = `(${nodeText})`;
6363
}
64+
if (node.parent && node.parent.type === 'ConditionalExpression' && node.parent.consequent.value === false) {
65+
return `${getIsCoerceValidNestedLogicalExpression(node) ? '' : '!'}${nodeText}`;
66+
}
6467
return `${getIsCoerceValidNestedLogicalExpression(node) ? '' : '!!'}${nodeText}`;
6568
}).join(' && ');
6669

70+
if (rightNode.parent.type === 'ConditionalExpression' && rightNode.parent.consequent.value === false) {
71+
const consequentVal = rightNode.parent.consequent.raw || rightNode.parent.consequent.name;
72+
const alternateVal = rightNode.parent.alternate.raw || rightNode.parent.alternate.name;
73+
if (rightNode.parent.test.type === 'LogicalExpression') {
74+
return fixer.replaceText(reportedNode, `${newText} ? ${consequentVal} : ${alternateVal}`);
75+
}
76+
return fixer.replaceText(reportedNode, `${newText} && ${rightNode.parent.alternate.name}`);
77+
}
78+
6779
if (rightNode.type === 'ConditionalExpression') {
6880
return fixer.replaceText(reportedNode, `${newText} && (${rightSideText})`);
6981
}

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

+36
Original file line numberDiff line numberDiff line change
@@ -847,5 +847,41 @@ ruleTester.run('jsx-no-leaked-render', rule, {
847847
column: 24,
848848
}],
849849
},
850+
{
851+
code: `
852+
const MyComponent = () => {
853+
return <Something checked={isIndeterminate ? false : isChecked} />
854+
}
855+
`,
856+
output: `
857+
const MyComponent = () => {
858+
return <Something checked={!isIndeterminate && isChecked} />
859+
}
860+
`,
861+
options: [{ validStrategies: ['coerce'] }],
862+
errors: [{
863+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
864+
line: 3,
865+
column: 38,
866+
}],
867+
},
868+
{
869+
code: `
870+
const MyComponent = () => {
871+
return <Something checked={cond && isIndeterminate ? false : isChecked} />
872+
}
873+
`,
874+
output: `
875+
const MyComponent = () => {
876+
return <Something checked={!!cond && !!isIndeterminate ? false : isChecked} />
877+
}
878+
`,
879+
options: [{ validStrategies: ['coerce'] }],
880+
errors: [{
881+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
882+
line: 3,
883+
column: 38,
884+
}],
885+
},
850886
]),
851887
});

0 commit comments

Comments
 (0)