]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-implicit-coercion.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / no-implicit-coercion.js
1 /**
2 * @fileoverview Tests for no-implicit-coercion rule.
3 * @author Toru Nagashima
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-implicit-coercion");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-implicit-coercion", rule, {
22 valid: [
23 "Boolean(foo)",
24 "foo.indexOf(1) !== -1",
25 "Number(foo)",
26 "parseInt(foo)",
27 "parseFloat(foo)",
28 "String(foo)",
29 "!foo",
30 "~foo",
31 "-foo",
32 "+1234",
33 "-1234",
34 "+Number(lol)",
35 "-parseFloat(lol)",
36 "2 * foo",
37 "1 * 1234",
38 "1 * Number(foo)",
39 "1 * parseInt(foo)",
40 "1 * parseFloat(foo)",
41 "Number(foo) * 1",
42 "parseInt(foo) * 1",
43 "parseFloat(foo) * 1",
44 "1 * 1234 * 678 * Number(foo)",
45 "1 * 1234 * 678 * parseInt(foo)",
46 "1234 * 1 * 678 * Number(foo)",
47 "1234 * 1 * Number(foo) * Number(bar)",
48 "1234 * 1 * Number(foo) * parseInt(bar)",
49 "1234 * 1 * Number(foo) * parseFloat(bar)",
50 "1234 * 1 * parseInt(foo) * parseFloat(bar)",
51 "1234 * 1 * parseInt(foo) * Number(bar)",
52 "1234 * 1 * parseFloat(foo) * Number(bar)",
53 "1234 * Number(foo) * 1 * Number(bar)",
54 "1234 * parseInt(foo) * 1 * Number(bar)",
55 "1234 * parseFloat(foo) * 1 * parseInt(bar)",
56 "1234 * parseFloat(foo) * 1 * Number(bar)",
57 "1234*foo*1",
58 "1234*1*foo",
59 "1234*bar*1*foo",
60 "1234*1*foo*bar",
61 "1234*1*foo*Number(bar)",
62 "1234*1*Number(foo)*bar",
63 "1234*1*parseInt(foo)*bar",
64 "0 + foo",
65 "~foo.bar()",
66 "foo + 'bar'",
67 { code: "foo + `${bar}`", parserOptions: { ecmaVersion: 6 } },
68
69 { code: "!!foo", options: [{ boolean: false }] },
70 { code: "~foo.indexOf(1)", options: [{ boolean: false }] },
71 { code: "+foo", options: [{ number: false }] },
72 { code: "1*foo", options: [{ number: false }] },
73 { code: "\"\"+foo", options: [{ string: false }] },
74 { code: "foo += \"\"", options: [{ string: false }] },
75 { code: "var a = !!foo", options: [{ boolean: true, allow: ["!!"] }] },
76 { code: "var a = ~foo.indexOf(1)", options: [{ boolean: true, allow: ["~"] }] },
77 { code: "var a = ~foo", options: [{ boolean: true }] },
78 { code: "var a = 1 * foo", options: [{ boolean: true, allow: ["*"] }] },
79 { code: "var a = +foo", options: [{ boolean: true, allow: ["+"] }] },
80 { code: "var a = \"\" + foo", options: [{ boolean: true, string: true, allow: ["+"] }] },
81
82 // https://github.com/eslint/eslint/issues/7057
83 "'' + 'foo'",
84 { code: "`` + 'foo'", parserOptions: { ecmaVersion: 6 } },
85 { code: "'' + `${foo}`", parserOptions: { ecmaVersion: 6 } },
86 "'foo' + ''",
87 { code: "'foo' + ``", parserOptions: { ecmaVersion: 6 } },
88 { code: "`${foo}` + ''", parserOptions: { ecmaVersion: 6 } },
89 "foo += 'bar'",
90 { code: "foo += `${bar}`", parserOptions: { ecmaVersion: 6 } },
91 { code: "`a${foo}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
92 { code: "`${foo}b`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
93 { code: "`${foo}${bar}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
94 { code: "tag`${foo}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
95 { code: "`${foo}`", parserOptions: { ecmaVersion: 6 } },
96 { code: "`${foo}`", options: [{ }], parserOptions: { ecmaVersion: 6 } },
97 { code: "`${foo}`", options: [{ disallowTemplateShorthand: false }], parserOptions: { ecmaVersion: 6 } },
98 "+42",
99
100 // https://github.com/eslint/eslint/issues/14623
101 "'' + String(foo)",
102 "String(foo) + ''",
103 { code: "`` + String(foo)", parserOptions: { ecmaVersion: 6 } },
104 { code: "String(foo) + ``", parserOptions: { ecmaVersion: 6 } },
105 { code: "`${'foo'}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
106 { code: "`${`foo`}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
107 { code: "`${String(foo)}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
108
109 // https://github.com/eslint/eslint/issues/16373
110 "console.log(Math.PI * 1/4)",
111 "a * 1 / 2",
112 "a * 1 / b"
113 ],
114 invalid: [
115 {
116 code: "!!foo",
117 output: "Boolean(foo)",
118 errors: [{
119 messageId: "useRecommendation",
120 data: { recommendation: "Boolean(foo)" },
121 type: "UnaryExpression"
122 }]
123 },
124 {
125 code: "!!(foo + bar)",
126 output: "Boolean(foo + bar)",
127 errors: [{
128 messageId: "useRecommendation",
129 data: { recommendation: "Boolean(foo + bar)" },
130 type: "UnaryExpression"
131 }]
132 },
133 {
134 code: "~foo.indexOf(1)",
135 output: null,
136 errors: [{
137 messageId: "useRecommendation",
138 data: { recommendation: "foo.indexOf(1) !== -1" },
139 type: "UnaryExpression"
140 }]
141 },
142 {
143 code: "~foo.bar.indexOf(2)",
144 output: null,
145 errors: [{
146 messageId: "useRecommendation",
147 data: { recommendation: "foo.bar.indexOf(2) !== -1" },
148 type: "UnaryExpression"
149 }]
150 },
151 {
152 code: "+foo",
153 output: "Number(foo)",
154 errors: [{
155 messageId: "useRecommendation",
156 data: { recommendation: "Number(foo)" },
157 type: "UnaryExpression"
158 }]
159 },
160 {
161 code: "+foo.bar",
162 output: "Number(foo.bar)",
163 errors: [{
164 messageId: "useRecommendation",
165 data: { recommendation: "Number(foo.bar)" },
166 type: "UnaryExpression"
167 }]
168 },
169 {
170 code: "1*foo",
171 output: "Number(foo)",
172 errors: [{
173 messageId: "useRecommendation",
174 data: { recommendation: "Number(foo)" },
175 type: "BinaryExpression"
176 }]
177 },
178 {
179 code: "foo*1",
180 output: "Number(foo)",
181 errors: [{
182 messageId: "useRecommendation",
183 data: { recommendation: "Number(foo)" },
184 type: "BinaryExpression"
185 }]
186 },
187 {
188 code: "1*foo.bar",
189 output: "Number(foo.bar)",
190 errors: [{
191 messageId: "useRecommendation",
192 data: { recommendation: "Number(foo.bar)" },
193 type: "BinaryExpression"
194 }]
195 },
196 {
197 code: "\"\"+foo",
198 output: "String(foo)",
199 errors: [{
200 messageId: "useRecommendation",
201 data: { recommendation: "String(foo)" },
202 type: "BinaryExpression"
203 }]
204 },
205 {
206 code: "``+foo",
207 output: "String(foo)",
208 parserOptions: { ecmaVersion: 6 },
209 errors: [{
210 messageId: "useRecommendation",
211 data: { recommendation: "String(foo)" },
212 type: "BinaryExpression"
213 }]
214 },
215 {
216 code: "foo+\"\"",
217 output: "String(foo)",
218 errors: [{
219 messageId: "useRecommendation",
220 data: { recommendation: "String(foo)" },
221 type: "BinaryExpression"
222 }]
223 },
224 {
225 code: "foo+``",
226 output: "String(foo)",
227 parserOptions: { ecmaVersion: 6 },
228 errors: [{
229 messageId: "useRecommendation",
230 data: { recommendation: "String(foo)" },
231 type: "BinaryExpression"
232 }]
233 },
234 {
235 code: "\"\"+foo.bar",
236 output: "String(foo.bar)",
237 errors: [{
238 messageId: "useRecommendation",
239 data: { recommendation: "String(foo.bar)" },
240 type: "BinaryExpression"
241 }]
242 },
243 {
244 code: "``+foo.bar",
245 output: "String(foo.bar)",
246 parserOptions: { ecmaVersion: 6 },
247 errors: [{
248 messageId: "useRecommendation",
249 data: { recommendation: "String(foo.bar)" },
250 type: "BinaryExpression"
251 }]
252 },
253 {
254 code: "foo.bar+\"\"",
255 output: "String(foo.bar)",
256 errors: [{
257 messageId: "useRecommendation",
258 data: { recommendation: "String(foo.bar)" },
259 type: "BinaryExpression"
260 }]
261 },
262 {
263 code: "foo.bar+``",
264 output: "String(foo.bar)",
265 parserOptions: { ecmaVersion: 6 },
266 errors: [{
267 messageId: "useRecommendation",
268 data: { recommendation: "String(foo.bar)" },
269 type: "BinaryExpression"
270 }]
271 },
272 {
273 code: "`${foo}`",
274 output: "String(foo)",
275 options: [{ disallowTemplateShorthand: true }],
276 parserOptions: { ecmaVersion: 6 },
277 errors: [{
278 messageId: "useRecommendation",
279 data: { recommendation: "String(foo)" },
280 type: "TemplateLiteral"
281 }]
282 },
283 {
284 code: "`\\\n${foo}`",
285 output: "String(foo)",
286 options: [{ disallowTemplateShorthand: true }],
287 parserOptions: { ecmaVersion: 6 },
288 errors: [{
289 messageId: "useRecommendation",
290 data: { recommendation: "String(foo)" },
291 type: "TemplateLiteral"
292 }]
293 },
294 {
295 code: "`${foo}\\\n`",
296 output: "String(foo)",
297 options: [{ disallowTemplateShorthand: true }],
298 parserOptions: { ecmaVersion: 6 },
299 errors: [{
300 messageId: "useRecommendation",
301 data: { recommendation: "String(foo)" },
302 type: "TemplateLiteral"
303 }]
304 },
305 {
306 code: "foo += \"\"",
307 output: "foo = String(foo)",
308 errors: [{
309 messageId: "useRecommendation",
310 data: { recommendation: "foo = String(foo)" },
311 type: "AssignmentExpression"
312 }]
313 },
314 {
315 code: "foo += ``",
316 output: "foo = String(foo)",
317 parserOptions: { ecmaVersion: 6 },
318 errors: [{
319 messageId: "useRecommendation",
320 data: { recommendation: "foo = String(foo)" },
321 type: "AssignmentExpression"
322 }]
323 },
324 {
325 code: "var a = !!foo",
326 output: "var a = Boolean(foo)",
327 options: [{ boolean: true, allow: ["~"] }],
328 errors: [{
329 messageId: "useRecommendation",
330 data: { recommendation: "Boolean(foo)" },
331 type: "UnaryExpression"
332 }]
333 },
334 {
335 code: "var a = ~foo.indexOf(1)",
336 output: null,
337 options: [{ boolean: true, allow: ["!!"] }],
338 errors: [{
339 messageId: "useRecommendation",
340 data: { recommendation: "foo.indexOf(1) !== -1" },
341 type: "UnaryExpression"
342 }]
343 },
344 {
345 code: "var a = 1 * foo",
346 output: "var a = Number(foo)",
347 options: [{ boolean: true, allow: ["+"] }],
348 errors: [{
349 messageId: "useRecommendation",
350 data: { recommendation: "Number(foo)" },
351 type: "BinaryExpression"
352 }]
353 },
354 {
355 code: "var a = +foo",
356 output: "var a = Number(foo)",
357 options: [{ boolean: true, allow: ["*"] }],
358 errors: [{
359 messageId: "useRecommendation",
360 data: { recommendation: "Number(foo)" },
361 type: "UnaryExpression"
362 }]
363 },
364 {
365 code: "var a = \"\" + foo",
366 output: "var a = String(foo)",
367 options: [{ boolean: true, allow: ["*"] }],
368 errors: [{
369 messageId: "useRecommendation",
370 data: { recommendation: "String(foo)" },
371 type: "BinaryExpression"
372 }]
373 },
374 {
375 code: "var a = `` + foo",
376 output: "var a = String(foo)",
377 options: [{ boolean: true, allow: ["*"] }],
378 parserOptions: { ecmaVersion: 6 },
379 errors: [{
380 messageId: "useRecommendation",
381 data: { recommendation: "String(foo)" },
382 type: "BinaryExpression"
383 }]
384 },
385 {
386 code: "typeof+foo",
387 output: "typeof Number(foo)",
388 errors: [{
389 messageId: "useRecommendation",
390 data: { recommendation: "Number(foo)" },
391 type: "UnaryExpression"
392 }]
393 },
394 {
395 code: "typeof +foo",
396 output: "typeof Number(foo)",
397 errors: [{
398 messageId: "useRecommendation",
399 data: { recommendation: "Number(foo)" },
400 type: "UnaryExpression"
401 }]
402 },
403 {
404 code: "let x ='' + 1n;",
405 output: "let x =String(1n);",
406 parserOptions: { ecmaVersion: 2020 },
407 errors: [{
408 messageId: "useRecommendation",
409 data: { recommendation: "String(1n)" },
410 type: "BinaryExpression"
411 }]
412 },
413
414 // Optional chaining
415 {
416 code: "~foo?.indexOf(1)",
417 output: null,
418 parserOptions: { ecmaVersion: 2020 },
419 errors: [{
420 messageId: "useRecommendation",
421 data: { recommendation: "foo?.indexOf(1) >= 0" },
422 type: "UnaryExpression"
423 }]
424 },
425 {
426 code: "~(foo?.indexOf)(1)",
427 output: null,
428 parserOptions: { ecmaVersion: 2020 },
429 errors: [{
430 messageId: "useRecommendation",
431 data: { recommendation: "(foo?.indexOf)(1) !== -1" },
432 type: "UnaryExpression"
433 }]
434 },
435
436 // https://github.com/eslint/eslint/issues/16373 regression tests
437 {
438 code: "1 * a / 2",
439 output: "Number(a) / 2",
440 errors: [{
441 messageId: "useRecommendation",
442 data: { recommendation: "Number(a)" },
443 type: "BinaryExpression"
444 }]
445 },
446 {
447 code: "(a * 1) / 2",
448 output: "(Number(a)) / 2",
449 errors: [{
450 messageId: "useRecommendation",
451 data: { recommendation: "Number(a)" },
452 type: "BinaryExpression"
453 }]
454 },
455 {
456 code: "a * 1 / (b * 1)",
457 output: "a * 1 / (Number(b))",
458 errors: [{
459 messageId: "useRecommendation",
460 data: { recommendation: "Number(b)" },
461 type: "BinaryExpression"
462 }]
463 },
464 {
465 code: "a * 1 + 2",
466 output: "Number(a) + 2",
467 errors: [{
468 messageId: "useRecommendation",
469 data: { recommendation: "Number(a)" },
470 type: "BinaryExpression"
471 }]
472 }
473 ]
474 });