From eda90f092ba96df00c3b06eba4cd4b1ee73da8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltr=C3=A1n=20Alarc=C3=B3n?= Date: Tue, 26 Apr 2022 21:56:21 +0200 Subject: [PATCH] apply more suggestions from the PR --- docs/rules/jsx-no-leaked-render.md | 4 ++-- lib/rules/jsx-no-leaked-render.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/rules/jsx-no-leaked-render.md b/docs/rules/jsx-no-leaked-render.md index 8e1093e598..4e8e092700 100644 --- a/docs/rules/jsx-no-leaked-render.md +++ b/docs/rules/jsx-no-leaked-render.md @@ -30,7 +30,7 @@ const Example = () => { ``` This can be avoided by: -- casting the condition to bool: `{!!someValue && }` +- coercing the conditional to a boolean: `{!!someValue && }` - transforming the binary expression into a ternary expression which returns `null` for falsy values: `{someValue ? : null}` This rule is autofixable, check the Options section to read more about the different strategies available. @@ -142,7 +142,7 @@ const Component = ({ elements }) => { The supported options are: ### `validStrategies` -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. +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. It can be set like: ```json5 diff --git a/lib/rules/jsx-no-leaked-render.js b/lib/rules/jsx-no-leaked-render.js index 2e15f93280..de6fd71166 100644 --- a/lib/rules/jsx-no-leaked-render.js +++ b/lib/rules/jsx-no-leaked-render.js @@ -20,6 +20,7 @@ const messages = { const COERCE_STRATEGY = 'coerce'; const TERNARY_STRATEGY = 'ternary'; const DEFAULT_VALID_STRATEGIES = [TERNARY_STRATEGY, COERCE_STRATEGY]; +const COERCE_VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression']; /** * @type {import('eslint').Rule.RuleModule} @@ -64,7 +65,7 @@ module.exports = { const areBothStrategiesValid = validStrategies.length === 2; function trimLeftNode(node) { - // Remove double unary expression (boolean cast), so we avoid trimming valid negations + // Remove double unary expression (boolean coercion), so we avoid trimming valid negations if (node.type === 'UnaryExpression' && node.argument.type === 'UnaryExpression') { return trimLeftNode(node.argument.argument); } @@ -95,19 +96,18 @@ module.exports = { return fixer.replaceText(reportedNode, `${leftSideText} ? ${rightSideText} : null`); } - throw new Error('Invalid value for "fixStrategy" option'); + throw new Error('Invalid value for "validStrategies" option'); } return { 'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) { const leftSide = node.left; - const CAST_VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression']; - const isCastStrategyValid = areBothStrategiesValid || fixStrategy === COERCE_STRATEGY; - const isCastValidLeftExpression = CAST_VALID_LEFT_SIDE_EXPRESSIONS.some( + const isCoerceStrategyValid = areBothStrategiesValid || fixStrategy === COERCE_STRATEGY; + const isCoerceValidLeftExpression = COERCE_VALID_LEFT_SIDE_EXPRESSIONS.some( (validExpression) => validExpression === leftSide.type ); - if (isCastStrategyValid && isCastValidLeftExpression) { + if (isCoerceStrategyValid && isCoerceValidLeftExpression) { return; }