Skip to content

Commit 0c932e0

Browse files
committed
[Fix]jsx-no-leaked-render: invalid fixes in coerce mode
1 parent 85ae820 commit 0c932e0

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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.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)