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