1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const astUtils = require("../ast-utils");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const INDEX_OF_PATTERN = /^(?:i|lastI)ndexOf$/;
|
15 | const ALLOWABLE_OPERATORS = ["~", "!!", "+", "*"];
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function parseOptions(options) {
|
23 | return {
|
24 | boolean: "boolean" in options ? Boolean(options.boolean) : true,
|
25 | number: "number" in options ? Boolean(options.number) : true,
|
26 | string: "string" in options ? Boolean(options.string) : true,
|
27 | allow: options.allow || []
|
28 | };
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function isDoubleLogicalNegating(node) {
|
37 | return (
|
38 | node.operator === "!" &&
|
39 | node.argument.type === "UnaryExpression" &&
|
40 | node.argument.operator === "!"
|
41 | );
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function isBinaryNegatingOfIndexOf(node) {
|
50 | return (
|
51 | node.operator === "~" &&
|
52 | node.argument.type === "CallExpression" &&
|
53 | node.argument.callee.type === "MemberExpression" &&
|
54 | node.argument.callee.property.type === "Identifier" &&
|
55 | INDEX_OF_PATTERN.test(node.argument.callee.property.name)
|
56 | );
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function isMultiplyByOne(node) {
|
65 | return node.operator === "*" && (
|
66 | node.left.type === "Literal" && node.left.value === 1 ||
|
67 | node.right.type === "Literal" && node.right.value === 1
|
68 | );
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function isNumeric(node) {
|
77 | return (
|
78 | node.type === "Literal" && typeof node.value === "number" ||
|
79 | node.type === "CallExpression" && (
|
80 | node.callee.name === "Number" ||
|
81 | node.callee.name === "parseInt" ||
|
82 | node.callee.name === "parseFloat"
|
83 | )
|
84 | );
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function getNonNumericOperand(node) {
|
95 | const left = node.left,
|
96 | right = node.right;
|
97 |
|
98 | if (right.type !== "BinaryExpression" && !isNumeric(right)) {
|
99 | return right;
|
100 | }
|
101 |
|
102 | if (left.type !== "BinaryExpression" && !isNumeric(left)) {
|
103 | return left;
|
104 | }
|
105 |
|
106 | return null;
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function isEmptyString(node) {
|
116 | return astUtils.isStringLiteral(node) && (node.value === "" || (node.type === "TemplateLiteral" && node.quasis.length === 1 && node.quasis[0].value.cooked === ""));
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function isConcatWithEmptyString(node) {
|
125 | return node.operator === "+" && (
|
126 | (isEmptyString(node.left) && !astUtils.isStringLiteral(node.right)) ||
|
127 | (isEmptyString(node.right) && !astUtils.isStringLiteral(node.left))
|
128 | );
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | function isAppendEmptyString(node) {
|
137 | return node.operator === "+=" && isEmptyString(node.right);
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | function getNonEmptyOperand(node) {
|
146 | return isEmptyString(node.left) ? node.right : node.left;
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | module.exports = {
|
154 | meta: {
|
155 | docs: {
|
156 | description: "disallow shorthand type conversions",
|
157 | category: "Best Practices",
|
158 | recommended: false,
|
159 | url: "https://eslint.org/docs/rules/no-implicit-coercion"
|
160 | },
|
161 |
|
162 | fixable: "code",
|
163 | schema: [{
|
164 | type: "object",
|
165 | properties: {
|
166 | boolean: {
|
167 | type: "boolean"
|
168 | },
|
169 | number: {
|
170 | type: "boolean"
|
171 | },
|
172 | string: {
|
173 | type: "boolean"
|
174 | },
|
175 | allow: {
|
176 | type: "array",
|
177 | items: {
|
178 | enum: ALLOWABLE_OPERATORS
|
179 | },
|
180 | uniqueItems: true
|
181 | }
|
182 | },
|
183 | additionalProperties: false
|
184 | }]
|
185 | },
|
186 |
|
187 | create(context) {
|
188 | const options = parseOptions(context.options[0] || {});
|
189 | const sourceCode = context.getSourceCode();
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function report(node, recommendation, shouldFix) {
|
199 | context.report({
|
200 | node,
|
201 | message: "use `{{recommendation}}` instead.",
|
202 | data: {
|
203 | recommendation
|
204 | },
|
205 | fix(fixer) {
|
206 | if (!shouldFix) {
|
207 | return null;
|
208 | }
|
209 |
|
210 | const tokenBefore = sourceCode.getTokenBefore(node);
|
211 |
|
212 | if (
|
213 | tokenBefore &&
|
214 | tokenBefore.range[1] === node.range[0] &&
|
215 | !astUtils.canTokensBeAdjacent(tokenBefore, recommendation)
|
216 | ) {
|
217 | return fixer.replaceText(node, ` ${recommendation}`);
|
218 | }
|
219 | return fixer.replaceText(node, recommendation);
|
220 | }
|
221 | });
|
222 | }
|
223 |
|
224 | return {
|
225 | UnaryExpression(node) {
|
226 | let operatorAllowed;
|
227 |
|
228 |
|
229 | operatorAllowed = options.allow.indexOf("!!") >= 0;
|
230 | if (!operatorAllowed && options.boolean && isDoubleLogicalNegating(node)) {
|
231 | const recommendation = `Boolean(${sourceCode.getText(node.argument.argument)})`;
|
232 |
|
233 | report(node, recommendation, true);
|
234 | }
|
235 |
|
236 |
|
237 | operatorAllowed = options.allow.indexOf("~") >= 0;
|
238 | if (!operatorAllowed && options.boolean && isBinaryNegatingOfIndexOf(node)) {
|
239 | const recommendation = `${sourceCode.getText(node.argument)} !== -1`;
|
240 |
|
241 | report(node, recommendation, false);
|
242 | }
|
243 |
|
244 |
|
245 | operatorAllowed = options.allow.indexOf("+") >= 0;
|
246 | if (!operatorAllowed && options.number && node.operator === "+" && !isNumeric(node.argument)) {
|
247 | const recommendation = `Number(${sourceCode.getText(node.argument)})`;
|
248 |
|
249 | report(node, recommendation, true);
|
250 | }
|
251 | },
|
252 |
|
253 |
|
254 | "BinaryExpression:exit"(node) {
|
255 | let operatorAllowed;
|
256 |
|
257 |
|
258 | operatorAllowed = options.allow.indexOf("*") >= 0;
|
259 | const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && getNonNumericOperand(node);
|
260 |
|
261 | if (nonNumericOperand) {
|
262 | const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`;
|
263 |
|
264 | report(node, recommendation, true);
|
265 | }
|
266 |
|
267 |
|
268 | operatorAllowed = options.allow.indexOf("+") >= 0;
|
269 | if (!operatorAllowed && options.string && isConcatWithEmptyString(node)) {
|
270 | const recommendation = `String(${sourceCode.getText(getNonEmptyOperand(node))})`;
|
271 |
|
272 | report(node, recommendation, true);
|
273 | }
|
274 | },
|
275 |
|
276 | AssignmentExpression(node) {
|
277 |
|
278 |
|
279 | const operatorAllowed = options.allow.indexOf("+") >= 0;
|
280 |
|
281 | if (!operatorAllowed && options.string && isAppendEmptyString(node)) {
|
282 | const code = sourceCode.getText(getNonEmptyOperand(node));
|
283 | const recommendation = `${code} = String(${code})`;
|
284 |
|
285 | report(node, recommendation, true);
|
286 | }
|
287 | }
|
288 | };
|
289 | }
|
290 | };
|