diff --git a/lib/rules/jsx-no-leaked-zero.js b/lib/rules/jsx-no-leaked-zero.js index b6537e7533..e957a23278 100644 --- a/lib/rules/jsx-no-leaked-zero.js +++ b/lib/rules/jsx-no-leaked-zero.js @@ -17,10 +17,9 @@ const messages = { noPotentialNumericEvaluation: 'Potential numeric evaluation resulting in an unintentionally rendered `0`', }; -const VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'ConditionalExpression', 'CallExpression']; -const CAST_FIX_STRATEGY = 'cast'; -const TERNARY_FIX_STRATEGY = 'ternary'; -const DEFAULT_VALID_FIX_STRATEGIES = [TERNARY_FIX_STRATEGY, CAST_FIX_STRATEGY]; +const CAST_STRATEGY = 'cast'; +const TERNARY_STRATEGY = 'ternary'; +const DEFAULT_VALID_FIX_STRATEGIES = [TERNARY_STRATEGY, CAST_STRATEGY]; /** * @type {import('eslint').Rule.RuleModule} @@ -45,8 +44,8 @@ module.exports = { type: 'array', items: { enum: [ - TERNARY_FIX_STRATEGY, - CAST_FIX_STRATEGY, + TERNARY_STRATEGY, + CAST_STRATEGY, ], }, uniqueItems: true, @@ -77,7 +76,7 @@ module.exports = { const sourceCode = context.getSourceCode(); const rightSideText = sourceCode.getText(rightNode); - if (fixStrategy === CAST_FIX_STRATEGY) { + if (fixStrategy === CAST_STRATEGY) { let leftSideText = sourceCode.getText(leftNode); if (isParenthesized(context, leftNode)) { leftSideText = `(${leftSideText})`; @@ -88,7 +87,7 @@ module.exports = { return fixer.replaceText(reportedNode, `${shouldPrefixDoubleNegation ? '!!' : ''}${leftSideText} && ${rightSideText}`); } - if (fixStrategy === TERNARY_FIX_STRATEGY) { + if (fixStrategy === TERNARY_STRATEGY) { let leftSideText = sourceCode.getText(trimLeftNode(leftNode)); if (isParenthesized(context, leftNode)) { leftSideText = `(${leftSideText})`; @@ -102,16 +101,10 @@ module.exports = { return { 'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) { const leftSide = node.left; - if (areBothStrategiesValid && VALID_LEFT_SIDE_EXPRESSIONS.includes(leftSide.type)) { - return; - } - - // TODO: check if can be removed - if (fixStrategy === TERNARY_FIX_STRATEGY && leftSide.type === 'ConditionalExpression') { - return; - } + const VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression']; + const isCastStrategyValid = areBothStrategiesValid || fixStrategy === CAST_STRATEGY; - if (fixStrategy === CAST_FIX_STRATEGY && ['UnaryExpression', 'BinaryExpression', 'CallExpression'].includes(leftSide.type)) { + if (isCastStrategyValid && VALID_LEFT_SIDE_EXPRESSIONS.includes(leftSide.type)) { return; } @@ -122,12 +115,10 @@ module.exports = { }, }); }, - 'JSXExpressionContainer > ConditionalExpression'(node) { - if (areBothStrategiesValid) { - return; - } - if (fixStrategy === TERNARY_FIX_STRATEGY) { + 'JSXExpressionContainer > ConditionalExpression'(node) { + const isTernaryStrategyValid = areBothStrategiesValid || fixStrategy === TERNARY_STRATEGY; + if (isTernaryStrategyValid) { return; } diff --git a/tests/lib/rules/jsx-no-leaked-zero.js b/tests/lib/rules/jsx-no-leaked-zero.js index b1e7cd57a5..e9f96875ab 100644 --- a/tests/lib/rules/jsx-no-leaked-zero.js +++ b/tests/lib/rules/jsx-no-leaked-zero.js @@ -230,6 +230,24 @@ ruleTester.run('jsx-no-leaked-zero', rule, { } `, }, + { + code: ` + const Component = ({ numberA, numberB }) => { + return
{(numberA || numberB) && {numberA+numberB}}
+ } + `, + options: [{ validFixStrategies: ['cast', 'ternary'] }], + errors: [{ + message: 'Potential numeric evaluation resulting in an unintentionally rendered `0`', + line: 3, + column: 24, + }], + output: ` + const Component = ({ numberA, numberB }) => { + return
{!!(numberA || numberB) && {numberA+numberB}}
+ } + `, + }, // Invalid tests with "ternary" fix strategy {