Skip to content

Commit 0020161

Browse files
committed
fix(jsx-no-leaked-render): avoid reporting ternary with valid alternate
1 parent c42b624 commit 0020161

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const COERCE_STRATEGY = 'coerce';
2121
const TERNARY_STRATEGY = 'ternary';
2222
const DEFAULT_VALID_STRATEGIES = [TERNARY_STRATEGY, COERCE_STRATEGY];
2323
const COERCE_VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression'];
24+
const TERNARY_INVALID_ALTERNATE_VALUES = [undefined, null, false];
2425

2526
function trimLeftNode(node) {
2627
// Remove double unary expression (boolean coercion), so we avoid trimming valid negations
@@ -122,6 +123,11 @@ module.exports = {
122123
return;
123124
}
124125

126+
const isValidTernaryAlternate = TERNARY_INVALID_ALTERNATE_VALUES.indexOf(node.alternate.value) === -1;
127+
if (isValidTernaryAlternate) {
128+
return;
129+
}
130+
125131
report(context, messages.noPotentialLeakedRender, 'noPotentialLeakedRender', {
126132
node,
127133
fix(fixer) {

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

+29
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ ruleTester.run('jsx-no-leaked-render', rule, {
124124
}
125125
`,
126126
},
127+
128+
// Fixes for // https://github.com/jsx-eslint/eslint-plugin-react/issues/3297
129+
{
130+
// It shouldn't delete valid alternate from ternary expressions when "coerce" is the only valid strategy - 1
131+
options: [{ validStrategies: ['coerce'] }],
132+
code: `
133+
const Component = ({ elements, count }) => {
134+
return <div>{direction ? (direction === "down" ? "▼" : "▲") : ""}</div>
135+
}
136+
`,
137+
},
138+
{
139+
// It shouldn't delete valid alternate from ternary expressions when "coerce" is the only valid strategy - 2
140+
options: [{ validStrategies: ['coerce'] }],
141+
code: `
142+
const Component = ({ containerName }) => {
143+
return <div>{ containerName.length > 0 ? "Loading several stuff" : "Loading" }</div>
144+
}
145+
`,
146+
},
147+
{
148+
// It shouldn't delete valid branches from ternary expressions when ["coerce", "ternary"] are only valid strategies
149+
options: [{ validStrategies: ['coerce', 'ternary'] }],
150+
code: `
151+
const Component = ({ elements, count }) => {
152+
return <div>{direction ? (direction === "down" ? "▼" : "▲") : ""}</div>
153+
}
154+
`,
155+
},
127156
]),
128157

129158
invalid: parsers.all([

0 commit comments

Comments
 (0)