Skip to content

Commit cead83e

Browse files
committed
fix(jsx-no-leaked-render): avoid reporting nested logical expressions
1 parent 0020161 commit cead83e

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ function trimLeftNode(node) {
3232
return node;
3333
}
3434

35+
function getIsCoerceValidNestedLogicalExpression(node) {
36+
if (node.type === 'LogicalExpression') {
37+
return getIsCoerceValidNestedLogicalExpression(node.left) && getIsCoerceValidNestedLogicalExpression(node.right);
38+
}
39+
40+
return COERCE_VALID_LEFT_SIDE_EXPRESSIONS.some((validExpression) => validExpression === node.type);
41+
}
42+
3543
function ruleFixer(context, fixStrategy, fixer, reportedNode, leftNode, rightNode) {
3644
const sourceCode = context.getSourceCode();
3745
const rightSideText = sourceCode.getText(rightNode);
@@ -103,11 +111,12 @@ module.exports = {
103111
'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) {
104112
const leftSide = node.left;
105113

106-
if (
107-
validStrategies.has(COERCE_STRATEGY)
108-
&& COERCE_VALID_LEFT_SIDE_EXPRESSIONS.some((validExpression) => validExpression === leftSide.type)
109-
) {
110-
return;
114+
const isCoerceValidLeftSide = COERCE_VALID_LEFT_SIDE_EXPRESSIONS
115+
.some((validExpression) => validExpression === leftSide.type);
116+
if (validStrategies.has(COERCE_STRATEGY)) {
117+
if (isCoerceValidLeftSide || getIsCoerceValidNestedLogicalExpression(leftSide)) {
118+
return;
119+
}
111120
}
112121

113122
report(context, messages.noPotentialLeakedRender, 'noPotentialLeakedRender', {

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

+26-7
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,27 @@ ruleTester.run('jsx-no-leaked-render', rule, {
124124
}
125125
`,
126126
},
127-
128-
// Fixes for // https://github.com/jsx-eslint/eslint-plugin-react/issues/3297
129127
{
130-
// It shouldn't delete valid alternate from ternary expressions when "coerce" is the only valid strategy - 1
131128
options: [{ validStrategies: ['coerce'] }],
132129
code: `
133130
const Component = ({ elements, count }) => {
134-
return <div>{direction ? (direction === "down" ? "▼" : "▲") : ""}</div>
131+
return <div>{!!count && <List elements={elements}/>}</div>
135132
}
136133
`,
137134
},
135+
136+
// Fixes for // https://github.com/jsx-eslint/eslint-plugin-react/issues/3297
138137
{
139-
// It shouldn't delete valid alternate from ternary expressions when "coerce" is the only valid strategy - 2
138+
// It shouldn't delete valid alternate from ternary expressions when "coerce" is the only valid strategy
140139
options: [{ validStrategies: ['coerce'] }],
141140
code: `
142-
const Component = ({ containerName }) => {
143-
return <div>{ containerName.length > 0 ? "Loading several stuff" : "Loading" }</div>
141+
const Component = ({ elements, count }) => {
142+
return (
143+
<div>
144+
<div> {direction ? (direction === "down" ? "▼" : "▲") : ""} </div>
145+
<div>{ containerName.length > 0 ? "Loading several stuff" : "Loading" }</div>
146+
</div>
147+
)
144148
}
145149
`,
146150
},
@@ -153,6 +157,21 @@ ruleTester.run('jsx-no-leaked-render', rule, {
153157
}
154158
`,
155159
},
160+
{
161+
// It shouldn't report nested logical expressions when "coerce" is the only valid strategy
162+
options: [{ validStrategies: ['coerce'] }],
163+
code: `
164+
const Component = ({ direction }) => {
165+
return (
166+
<div>
167+
<div>{!!direction && direction === "down" && "▼"}</div>
168+
<div>{direction === "down" && !!direction && "▼"}</div>
169+
<div>{direction === "down" || !!direction && "▼"}</div>
170+
</div>
171+
)
172+
}
173+
`,
174+
},
156175
]),
157176

158177
invalid: parsers.all([

0 commit comments

Comments
 (0)