Skip to content

Commit eda90f0

Browse files
committed
apply more suggestions from the PR
1 parent e04af42 commit eda90f0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

docs/rules/jsx-no-leaked-render.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const Example = () => {
3030
```
3131

3232
This can be avoided by:
33-
- casting the condition to bool: `{!!someValue && <Something />}`
33+
- coercing the conditional to a boolean: `{!!someValue && <Something />}`
3434
- transforming the binary expression into a ternary expression which returns `null` for falsy values: `{someValue ? <Something /> : null}`
3535

3636
This rule is autofixable, check the Options section to read more about the different strategies available.
@@ -142,7 +142,7 @@ const Component = ({ elements }) => {
142142
The supported options are:
143143

144144
### `validStrategies`
145-
An array containing `"coerce"`, `"ternary"`, or both (default: `["ternary", "coerce"]`) - Decide which strategies are considered valid to prevent leaked renders (at least 1 is required). The "coerce" option will cast to boolean the condition of the JSX expression. The "ternary" option transforms the binary expression into a ternary expression returning `null` for falsy values. The first option from the array will be used as autofix, so the order of the values matter.
145+
An array containing `"coerce"`, `"ternary"`, or both (default: `["ternary", "coerce"]`) - Decide which strategies are considered valid to prevent leaked renders (at least 1 is required). The "coerce" option will transform the conditional of the JSX expression to a boolean. The "ternary" option transforms the binary expression into a ternary expression returning `null` for falsy values. The first option from the array will be used as autofix, so the order of the values matter.
146146

147147
It can be set like:
148148
```json5

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const messages = {
2020
const COERCE_STRATEGY = 'coerce';
2121
const TERNARY_STRATEGY = 'ternary';
2222
const DEFAULT_VALID_STRATEGIES = [TERNARY_STRATEGY, COERCE_STRATEGY];
23+
const COERCE_VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression'];
2324

2425
/**
2526
* @type {import('eslint').Rule.RuleModule}
@@ -64,7 +65,7 @@ module.exports = {
6465
const areBothStrategiesValid = validStrategies.length === 2;
6566

6667
function trimLeftNode(node) {
67-
// Remove double unary expression (boolean cast), so we avoid trimming valid negations
68+
// Remove double unary expression (boolean coercion), so we avoid trimming valid negations
6869
if (node.type === 'UnaryExpression' && node.argument.type === 'UnaryExpression') {
6970
return trimLeftNode(node.argument.argument);
7071
}
@@ -95,19 +96,18 @@ module.exports = {
9596
return fixer.replaceText(reportedNode, `${leftSideText} ? ${rightSideText} : null`);
9697
}
9798

98-
throw new Error('Invalid value for "fixStrategy" option');
99+
throw new Error('Invalid value for "validStrategies" option');
99100
}
100101

101102
return {
102103
'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) {
103104
const leftSide = node.left;
104-
const CAST_VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression'];
105-
const isCastStrategyValid = areBothStrategiesValid || fixStrategy === COERCE_STRATEGY;
106-
const isCastValidLeftExpression = CAST_VALID_LEFT_SIDE_EXPRESSIONS.some(
105+
const isCoerceStrategyValid = areBothStrategiesValid || fixStrategy === COERCE_STRATEGY;
106+
const isCoerceValidLeftExpression = COERCE_VALID_LEFT_SIDE_EXPRESSIONS.some(
107107
(validExpression) => validExpression === leftSide.type
108108
);
109109

110-
if (isCastStrategyValid && isCastValidLeftExpression) {
110+
if (isCoerceStrategyValid && isCoerceValidLeftExpression) {
111111
return;
112112
}
113113

0 commit comments

Comments
 (0)