Skip to content

Commit

Permalink
final tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Belco90 committed Mar 9, 2022
1 parent bf2c26e commit 59aee22
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
35 changes: 13 additions & 22 deletions lib/rules/jsx-no-leaked-zero.js
Expand Up @@ -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}
Expand All @@ -45,8 +44,8 @@ module.exports = {
type: 'array',
items: {
enum: [
TERNARY_FIX_STRATEGY,
CAST_FIX_STRATEGY,
TERNARY_STRATEGY,
CAST_STRATEGY,
],
},
uniqueItems: true,
Expand Down Expand Up @@ -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})`;
Expand All @@ -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})`;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/jsx-no-leaked-zero.js
Expand Up @@ -230,6 +230,24 @@ ruleTester.run('jsx-no-leaked-zero', rule, {
}
`,
},
{
code: `
const Component = ({ numberA, numberB }) => {
return <div>{(numberA || numberB) && <Results>{numberA+numberB}</Results>}</div>
}
`,
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 <div>{!!(numberA || numberB) && <Results>{numberA+numberB}</Results>}</div>
}
`,
},

// Invalid tests with "ternary" fix strategy
{
Expand Down

0 comments on commit 59aee22

Please sign in to comment.