Skip to content

Commit 45500d2

Browse files
committed
rename "cast" option to "coerce"
1 parent 866f532 commit 45500d2

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ const Component = ({ elements }) => {
142142
The supported options are:
143143

144144
### `validStrategies`
145-
An array containing `"cast"`, `"ternary"` or both (default: `["ternary", "cast"]`) - Decide which strategies are considered valid to prevent leaked renders (at least 1 is required). The "cast" 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 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.
146146

147147
It can be set like:
148148
```json5
149149
{
150150
// ...
151-
"react/jsx-no-leaked-render": [<enabled>, { "validStrategies": ["ternary", "cast"] }]
151+
"react/jsx-no-leaked-render": [<enabled>, { "validStrategies": ["ternary", "coerce"] }]
152152
// ...
153153
}
154154
```
@@ -175,7 +175,7 @@ const Component = ({ count, title }) => {
175175
}
176176
```
177177

178-
Assuming the following options: `{ "validStrategies": ["cast"] }`
178+
Assuming the following options: `{ "validStrategies": ["coerce"] }`
179179

180180
Examples of **incorrect** code for this rule, with the above configuration:
181181
```jsx

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const messages = {
1717
noPotentialLeakedRender: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
1818
};
1919

20-
const CAST_STRATEGY = 'cast';
20+
const COERCE_STRATEGY = 'coerce';
2121
const TERNARY_STRATEGY = 'ternary';
22-
const DEFAULT_VALID_STRATEGIES = [TERNARY_STRATEGY, CAST_STRATEGY];
22+
const DEFAULT_VALID_STRATEGIES = [TERNARY_STRATEGY, COERCE_STRATEGY];
2323

2424
/**
2525
* @type {import('eslint').Rule.RuleModule}
@@ -45,7 +45,7 @@ module.exports = {
4545
items: {
4646
enum: [
4747
TERNARY_STRATEGY,
48-
CAST_STRATEGY,
48+
COERCE_STRATEGY,
4949
],
5050
},
5151
uniqueItems: true,
@@ -76,7 +76,7 @@ module.exports = {
7676
const sourceCode = context.getSourceCode();
7777
const rightSideText = sourceCode.getText(rightNode);
7878

79-
if (fixStrategy === CAST_STRATEGY) {
79+
if (fixStrategy === COERCE_STRATEGY) {
8080
let leftSideText = sourceCode.getText(leftNode);
8181
if (isParenthesized(context, leftNode)) {
8282
leftSideText = `(${leftSideText})`;
@@ -102,7 +102,7 @@ module.exports = {
102102
'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) {
103103
const leftSide = node.left;
104104
const CAST_VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression'];
105-
const isCastStrategyValid = areBothStrategiesValid || fixStrategy === CAST_STRATEGY;
105+
const isCastStrategyValid = areBothStrategiesValid || fixStrategy === COERCE_STRATEGY;
106106
const isCastValidLeftExpression = CAST_VALID_LEFT_SIDE_EXPRESSIONS.some(
107107
(validExpression) => validExpression === leftSide.type
108108
);

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,23 @@ ruleTester.run('jsx-no-leaked-render', rule, {
101101
`,
102102
},
103103
{
104-
options: [{ validStrategies: ['cast'] }],
104+
options: [{ validStrategies: ['coerce'] }],
105105
code: `
106106
const Component = ({ elements, count }) => {
107107
return <div>{!!count && <List elements={elements}/>}</div>
108108
}
109109
`,
110110
},
111111
{
112-
options: [{ validStrategies: ['cast', 'ternary'] }],
112+
options: [{ validStrategies: ['coerce', 'ternary'] }],
113113
code: `
114114
const Component = ({ elements, count }) => {
115115
return <div>{count ? <List elements={elements}/> : null}</div>
116116
}
117117
`,
118118
},
119119
{
120-
options: [{ validStrategies: ['cast', 'ternary'] }],
120+
options: [{ validStrategies: ['coerce', 'ternary'] }],
121121
code: `
122122
const Component = ({ elements, count }) => {
123123
return <div>{!!count && <List elements={elements}/>}</div>
@@ -132,11 +132,11 @@ ruleTester.run('jsx-no-leaked-render', rule, {
132132
code: `
133133
const Example = () => {
134134
return (
135-
<>
135+
<div>
136136
{0 && <Something/>}
137137
{'' && <Something/>}
138138
{NaN && <Something/>}
139-
</>
139+
</div>
140140
)
141141
}
142142
`,
@@ -279,7 +279,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
279279
return <div>{(numberA || numberB) && <Results>{numberA+numberB}</Results>}</div>
280280
}
281281
`,
282-
options: [{ validStrategies: ['cast', 'ternary'] }],
282+
options: [{ validStrategies: ['coerce', 'ternary'] }],
283283
errors: [{
284284
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
285285
line: 3,
@@ -402,7 +402,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
402402
`,
403403
},
404404

405-
// cases: boolean cast isn't valid if strategy is only "ternary"
405+
// cases: boolean coerce isn't valid if strategy is only "ternary"
406406
{
407407
code: `
408408
const Component = ({ someCondition, title }) => {
@@ -512,14 +512,14 @@ ruleTester.run('jsx-no-leaked-render', rule, {
512512
`,
513513
},
514514

515-
// Invalid tests only with "cast" strategy enabled
515+
// Invalid tests only with "coerce" strategy enabled
516516
{
517517
code: `
518518
const Component = ({ count, title }) => {
519519
return <div>{count && title}</div>
520520
}
521521
`,
522-
options: [{ validStrategies: ['cast'] }],
522+
options: [{ validStrategies: ['coerce'] }],
523523
errors: [{
524524
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
525525
line: 3,
@@ -537,7 +537,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
537537
return <div>{count && <span>There are {count} results</span>}</div>
538538
}
539539
`,
540-
options: [{ validStrategies: ['cast'] }],
540+
options: [{ validStrategies: ['coerce'] }],
541541
errors: [{
542542
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
543543
line: 3,
@@ -555,7 +555,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
555555
return <div>{elements.length && <List elements={elements}/>}</div>
556556
}
557557
`,
558-
options: [{ validStrategies: ['cast'] }],
558+
options: [{ validStrategies: ['coerce'] }],
559559
errors: [{
560560
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
561561
line: 3,
@@ -573,7 +573,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
573573
return <div>{nestedCollection.elements.length && <List elements={nestedCollection.elements}/>}</div>
574574
}
575575
`,
576-
options: [{ validStrategies: ['cast'] }],
576+
options: [{ validStrategies: ['coerce'] }],
577577
errors: [{
578578
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
579579
line: 3,
@@ -591,7 +591,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
591591
return <div>{elements[0] && <List elements={elements}/>}</div>
592592
}
593593
`,
594-
options: [{ validStrategies: ['cast'] }],
594+
options: [{ validStrategies: ['coerce'] }],
595595
errors: [{
596596
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
597597
line: 3,
@@ -609,7 +609,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
609609
return <div>{(numberA || numberB) && <Results>{numberA+numberB}</Results>}</div>
610610
}
611611
`,
612-
options: [{ validStrategies: ['cast'] }],
612+
options: [{ validStrategies: ['coerce'] }],
613613
errors: [{
614614
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
615615
line: 3,
@@ -622,14 +622,14 @@ ruleTester.run('jsx-no-leaked-render', rule, {
622622
`,
623623
},
624624

625-
// cases: ternary isn't valid if strategy is only "cast"
625+
// cases: ternary isn't valid if strategy is only "coerce"
626626
{
627627
code: `
628628
const Component = ({ count, title }) => {
629629
return <div>{count ? title : null}</div>
630630
}
631631
`,
632-
options: [{ validStrategies: ['cast'] }],
632+
options: [{ validStrategies: ['coerce'] }],
633633
errors: [{
634634
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
635635
line: 3,
@@ -647,7 +647,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
647647
return <div>{!count ? title : null}</div>
648648
}
649649
`,
650-
options: [{ validStrategies: ['cast'] }],
650+
options: [{ validStrategies: ['coerce'] }],
651651
errors: [{
652652
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
653653
line: 3,
@@ -665,7 +665,7 @@ ruleTester.run('jsx-no-leaked-render', rule, {
665665
return <div>{count && somethingElse ? title : null}</div>
666666
}
667667
`,
668-
options: [{ validStrategies: ['cast'] }],
668+
options: [{ validStrategies: ['coerce'] }],
669669
errors: [{
670670
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
671671
line: 3,

0 commit comments

Comments
 (0)