/
jsx-no-leaked-zero.js
134 lines (112 loc) · 4.11 KB
/
jsx-no-leaked-zero.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* @fileoverview Prevent zero from being rendered on numerical condition
* @author Mario Beltrán
*/
'use strict';
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const isParenthesized = require('../util/ast').isParenthesized;
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const messages = {
noPotentialNumericEvaluation: 'Potential numeric evaluation resulting in an unintentionally rendered `0`',
};
const CAST_STRATEGY = 'cast';
const TERNARY_STRATEGY = 'ternary';
const DEFAULT_VALID_FIX_STRATEGIES = [TERNARY_STRATEGY, CAST_STRATEGY];
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
docs: {
description: 'Prevent zero from being rendered on numerical condition',
category: 'Possible Errors',
recommended: false,
url: docsUrl('jsx-no-leaked-zero'),
},
messages,
fixable: 'code',
schema: [
{
type: 'object',
properties: {
validFixStrategies: {
type: 'array',
items: {
enum: [
TERNARY_STRATEGY,
CAST_STRATEGY,
],
},
uniqueItems: true,
default: DEFAULT_VALID_FIX_STRATEGIES,
},
},
additionalProperties: false,
},
],
},
create(context) {
const config = context.options[0] || {};
const validFixStrategies = config.validFixStrategies || DEFAULT_VALID_FIX_STRATEGIES;
const fixStrategy = validFixStrategies[0];
const areBothStrategiesValid = validFixStrategies.length === 2;
function trimLeftNode(node) {
// Remove double unary expression (boolean cast), so we avoid trimming valid negations
if (node.type === 'UnaryExpression' && node.argument.type === 'UnaryExpression') {
return trimLeftNode(node.argument.argument);
}
return node;
}
function ruleFixer(fixer, reportedNode, leftNode, rightNode) {
const sourceCode = context.getSourceCode();
const rightSideText = sourceCode.getText(rightNode);
if (fixStrategy === CAST_STRATEGY) {
let leftSideText = sourceCode.getText(leftNode);
if (isParenthesized(context, leftNode)) {
leftSideText = `(${leftSideText})`;
}
const shouldPrefixDoubleNegation = leftNode.type !== 'UnaryExpression';
return fixer.replaceText(reportedNode, `${shouldPrefixDoubleNegation ? '!!' : ''}${leftSideText} && ${rightSideText}`);
}
if (fixStrategy === TERNARY_STRATEGY) {
let leftSideText = sourceCode.getText(trimLeftNode(leftNode));
if (isParenthesized(context, leftNode)) {
leftSideText = `(${leftSideText})`;
}
return fixer.replaceText(reportedNode, `${leftSideText} ? ${rightSideText} : null`);
}
throw new Error('Invalid value for "fixStrategy" option');
}
return {
'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) {
const leftSide = node.left;
const VALID_LEFT_SIDE_EXPRESSIONS = ['UnaryExpression', 'BinaryExpression', 'CallExpression'];
const isCastStrategyValid = areBothStrategiesValid || fixStrategy === CAST_STRATEGY;
if (isCastStrategyValid && VALID_LEFT_SIDE_EXPRESSIONS.includes(leftSide.type)) {
return;
}
report(context, messages.noPotentialNumericEvaluation, 'noPotentialNumericEvaluation', {
node,
fix(fixer) {
return ruleFixer(fixer, node, leftSide, node.right);
},
});
},
'JSXExpressionContainer > ConditionalExpression'(node) {
const isTernaryStrategyValid = areBothStrategiesValid || fixStrategy === TERNARY_STRATEGY;
if (isTernaryStrategyValid) {
return;
}
report(context, messages.noPotentialNumericEvaluation, 'noPotentialNumericEvaluation', {
node,
fix(fixer) {
return ruleFixer(fixer, node, node.test, node.consequent);
},
});
},
};
},
};