-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathprefer-logical-operator-over-ternary.js
160 lines (138 loc) · 3.7 KB
/
prefer-logical-operator-over-ternary.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const {isParenthesized, getParenthesizedText} = require('./utils/parentheses.js');
const isSameReference = require('./utils/is-same-reference.js');
const shouldAddParenthesesToLogicalExpressionChild = require('./utils/should-add-parentheses-to-logical-expression-child.js');
const needsSemicolon = require('./utils/needs-semicolon.js');
const MESSAGE_ID_ERROR = 'prefer-logical-operator-over-ternary/error';
const MESSAGE_ID_SUGGESTION = 'prefer-logical-operator-over-ternary/suggestion';
const messages = {
[MESSAGE_ID_ERROR]: 'Prefer using a logical operator over a ternary.',
[MESSAGE_ID_SUGGESTION]: 'Switch to `{{operator}}` operator.',
};
function isSameNode(left, right, sourceCode) {
if (isSameReference(left, right)) {
return true;
}
if (left.type !== right.type) {
return false;
}
switch (left.type) {
case 'AwaitExpression': {
return isSameNode(left.argument, right.argument, sourceCode);
}
case 'LogicalExpression': {
return (
left.operator === right.operator
&& isSameNode(left.left, right.left, sourceCode)
&& isSameNode(left.right, right.right, sourceCode)
);
}
case 'UnaryExpression': {
return (
left.operator === right.operator
&& left.prefix === right.prefix
&& isSameNode(left.argument, right.argument, sourceCode)
);
}
case 'UpdateExpression': {
return false;
}
// No default
}
return sourceCode.getText(left) === sourceCode.getText(right);
}
function fix({
fixer,
sourceCode,
conditionalExpression,
left,
right,
operator,
}) {
let text = [left, right].map((node, index) => {
const isNodeParenthesized = isParenthesized(node, sourceCode);
let text = isNodeParenthesized ? getParenthesizedText(node, sourceCode) : sourceCode.getText(node);
if (
!isNodeParenthesized
&& shouldAddParenthesesToLogicalExpressionChild(node, {operator, property: index === 0 ? 'left' : 'right'})
) {
text = `(${text})`;
}
return text;
}).join(` ${operator} `);
// According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table
// There should be no cases need add parentheses when switching ternary to logical expression
// ASI
if (needsSemicolon(sourceCode.getTokenBefore(conditionalExpression), sourceCode, text)) {
text = `;${text}`;
}
return fixer.replaceText(conditionalExpression, text);
}
function getProblem({
sourceCode,
conditionalExpression,
left,
right,
}) {
return {
node: conditionalExpression,
messageId: MESSAGE_ID_ERROR,
suggest: ['??', '||'].map(operator => ({
messageId: MESSAGE_ID_SUGGESTION,
data: {operator},
fix: fixer => fix({
fixer,
sourceCode,
conditionalExpression,
left,
right,
operator,
}),
})),
};
}
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {sourceCode} = context;
return {
ConditionalExpression(conditionalExpression) {
const {test, consequent, alternate} = conditionalExpression;
// `foo ? foo : bar`
if (isSameNode(test, consequent, sourceCode)) {
return getProblem({
sourceCode,
conditionalExpression,
left: test,
right: alternate,
});
}
// `!bar ? foo : bar`
if (
test.type === 'UnaryExpression'
&& test.operator === '!'
&& test.prefix
&& isSameNode(test.argument, alternate, sourceCode)
) {
return getProblem({
sourceCode,
conditionalExpression,
left: test.argument,
right: consequent,
});
}
},
};
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Prefer using a logical operator over a ternary.',
recommended: true,
},
hasSuggestions: true,
messages,
},
};