]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-implicit-coercion.js
import eslint 7.28.0
[pve-eslint.git] / eslint / tests / lib / rules / no-implicit-coercion.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for no-implicit-coercion rule.
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-implicit-coercion");
13const { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.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 } },
5422a9cc
TL
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 } }
eb39fafa
DC
108 ],
109 invalid: [
110 {
111 code: "!!foo",
112 output: "Boolean(foo)",
113 errors: [{
114 messageId: "useRecommendation",
115 data: { recommendation: "Boolean(foo)" },
116 type: "UnaryExpression"
117 }]
118 },
119 {
120 code: "!!(foo + bar)",
121 output: "Boolean(foo + bar)",
122 errors: [{
123 messageId: "useRecommendation",
124 data: { recommendation: "Boolean(foo + bar)" },
125 type: "UnaryExpression"
126 }]
127 },
128 {
129 code: "~foo.indexOf(1)",
130 output: null,
131 errors: [{
132 messageId: "useRecommendation",
133 data: { recommendation: "foo.indexOf(1) !== -1" },
134 type: "UnaryExpression"
135 }]
136 },
137 {
138 code: "~foo.bar.indexOf(2)",
139 output: null,
140 errors: [{
141 messageId: "useRecommendation",
142 data: { recommendation: "foo.bar.indexOf(2) !== -1" },
143 type: "UnaryExpression"
144 }]
145 },
146 {
147 code: "+foo",
148 output: "Number(foo)",
149 errors: [{
150 messageId: "useRecommendation",
151 data: { recommendation: "Number(foo)" },
152 type: "UnaryExpression"
153 }]
154 },
155 {
156 code: "+foo.bar",
157 output: "Number(foo.bar)",
158 errors: [{
159 messageId: "useRecommendation",
160 data: { recommendation: "Number(foo.bar)" },
161 type: "UnaryExpression"
162 }]
163 },
164 {
165 code: "1*foo",
166 output: "Number(foo)",
167 errors: [{
168 messageId: "useRecommendation",
169 data: { recommendation: "Number(foo)" },
170 type: "BinaryExpression"
171 }]
172 },
173 {
174 code: "foo*1",
175 output: "Number(foo)",
176 errors: [{
177 messageId: "useRecommendation",
178 data: { recommendation: "Number(foo)" },
179 type: "BinaryExpression"
180 }]
181 },
182 {
183 code: "1*foo.bar",
184 output: "Number(foo.bar)",
185 errors: [{
186 messageId: "useRecommendation",
187 data: { recommendation: "Number(foo.bar)" },
188 type: "BinaryExpression"
189 }]
190 },
191 {
192 code: "\"\"+foo",
193 output: "String(foo)",
194 errors: [{
195 messageId: "useRecommendation",
196 data: { recommendation: "String(foo)" },
197 type: "BinaryExpression"
198 }]
199 },
200 {
201 code: "``+foo",
202 output: "String(foo)",
203 parserOptions: { ecmaVersion: 6 },
204 errors: [{
205 messageId: "useRecommendation",
206 data: { recommendation: "String(foo)" },
207 type: "BinaryExpression"
208 }]
209 },
210 {
211 code: "foo+\"\"",
212 output: "String(foo)",
213 errors: [{
214 messageId: "useRecommendation",
215 data: { recommendation: "String(foo)" },
216 type: "BinaryExpression"
217 }]
218 },
219 {
220 code: "foo+``",
221 output: "String(foo)",
222 parserOptions: { ecmaVersion: 6 },
223 errors: [{
224 messageId: "useRecommendation",
225 data: { recommendation: "String(foo)" },
226 type: "BinaryExpression"
227 }]
228 },
229 {
230 code: "\"\"+foo.bar",
231 output: "String(foo.bar)",
232 errors: [{
233 messageId: "useRecommendation",
234 data: { recommendation: "String(foo.bar)" },
235 type: "BinaryExpression"
236 }]
237 },
238 {
239 code: "``+foo.bar",
240 output: "String(foo.bar)",
241 parserOptions: { ecmaVersion: 6 },
242 errors: [{
243 messageId: "useRecommendation",
244 data: { recommendation: "String(foo.bar)" },
245 type: "BinaryExpression"
246 }]
247 },
248 {
249 code: "foo.bar+\"\"",
250 output: "String(foo.bar)",
251 errors: [{
252 messageId: "useRecommendation",
253 data: { recommendation: "String(foo.bar)" },
254 type: "BinaryExpression"
255 }]
256 },
257 {
258 code: "foo.bar+``",
259 output: "String(foo.bar)",
260 parserOptions: { ecmaVersion: 6 },
261 errors: [{
262 messageId: "useRecommendation",
263 data: { recommendation: "String(foo.bar)" },
264 type: "BinaryExpression"
265 }]
266 },
5422a9cc
TL
267 {
268 code: "`${foo}`",
269 output: "String(foo)",
270 options: [{ disallowTemplateShorthand: true }],
271 parserOptions: { ecmaVersion: 6 },
272 errors: [{
273 messageId: "useRecommendation",
274 data: { recommendation: "String(foo)" },
275 type: "TemplateLiteral"
276 }]
277 },
278 {
279 code: "`\\\n${foo}`",
280 output: "String(foo)",
281 options: [{ disallowTemplateShorthand: true }],
282 parserOptions: { ecmaVersion: 6 },
283 errors: [{
284 messageId: "useRecommendation",
285 data: { recommendation: "String(foo)" },
286 type: "TemplateLiteral"
287 }]
288 },
289 {
290 code: "`${foo}\\\n`",
291 output: "String(foo)",
292 options: [{ disallowTemplateShorthand: true }],
293 parserOptions: { ecmaVersion: 6 },
294 errors: [{
295 messageId: "useRecommendation",
296 data: { recommendation: "String(foo)" },
297 type: "TemplateLiteral"
298 }]
299 },
eb39fafa
DC
300 {
301 code: "foo += \"\"",
302 output: "foo = String(foo)",
303 errors: [{
304 messageId: "useRecommendation",
305 data: { recommendation: "foo = String(foo)" },
306 type: "AssignmentExpression"
307 }]
308 },
309 {
310 code: "foo += ``",
311 output: "foo = String(foo)",
312 parserOptions: { ecmaVersion: 6 },
313 errors: [{
314 messageId: "useRecommendation",
315 data: { recommendation: "foo = String(foo)" },
316 type: "AssignmentExpression"
317 }]
318 },
319 {
320 code: "var a = !!foo",
321 output: "var a = Boolean(foo)",
322 options: [{ boolean: true, allow: ["~"] }],
323 errors: [{
324 messageId: "useRecommendation",
325 data: { recommendation: "Boolean(foo)" },
326 type: "UnaryExpression"
327 }]
328 },
329 {
330 code: "var a = ~foo.indexOf(1)",
331 output: null,
332 options: [{ boolean: true, allow: ["!!"] }],
333 errors: [{
334 messageId: "useRecommendation",
335 data: { recommendation: "foo.indexOf(1) !== -1" },
336 type: "UnaryExpression"
337 }]
338 },
339 {
340 code: "var a = 1 * foo",
341 output: "var a = Number(foo)",
342 options: [{ boolean: true, allow: ["+"] }],
343 errors: [{
344 messageId: "useRecommendation",
345 data: { recommendation: "Number(foo)" },
346 type: "BinaryExpression"
347 }]
348 },
349 {
350 code: "var a = +foo",
351 output: "var a = Number(foo)",
352 options: [{ boolean: true, allow: ["*"] }],
353 errors: [{
354 messageId: "useRecommendation",
355 data: { recommendation: "Number(foo)" },
356 type: "UnaryExpression"
357 }]
358 },
359 {
360 code: "var a = \"\" + foo",
361 output: "var a = String(foo)",
362 options: [{ boolean: true, allow: ["*"] }],
363 errors: [{
364 messageId: "useRecommendation",
365 data: { recommendation: "String(foo)" },
366 type: "BinaryExpression"
367 }]
368 },
369 {
370 code: "var a = `` + foo",
371 output: "var a = String(foo)",
372 options: [{ boolean: true, allow: ["*"] }],
373 parserOptions: { ecmaVersion: 6 },
374 errors: [{
375 messageId: "useRecommendation",
376 data: { recommendation: "String(foo)" },
377 type: "BinaryExpression"
378 }]
379 },
380 {
381 code: "typeof+foo",
382 output: "typeof Number(foo)",
383 errors: [{
384 messageId: "useRecommendation",
385 data: { recommendation: "Number(foo)" },
386 type: "UnaryExpression"
387 }]
388 },
389 {
390 code: "typeof +foo",
391 output: "typeof Number(foo)",
392 errors: [{
393 messageId: "useRecommendation",
394 data: { recommendation: "Number(foo)" },
395 type: "UnaryExpression"
396 }]
397 },
398 {
399 code: "let x ='' + 1n;",
400 output: "let x =String(1n);",
401 parserOptions: { ecmaVersion: 2020 },
402 errors: [{
403 messageId: "useRecommendation",
404 data: { recommendation: "String(1n)" },
405 type: "BinaryExpression"
406 }]
6f036462
TL
407 },
408
409 // Optional chaining
410 {
411 code: "~foo?.indexOf(1)",
412 output: null,
413 parserOptions: { ecmaVersion: 2020 },
414 errors: [{
415 messageId: "useRecommendation",
416 data: { recommendation: "foo?.indexOf(1) >= 0" },
417 type: "UnaryExpression"
418 }]
419 },
420 {
421 code: "~(foo?.indexOf)(1)",
422 output: null,
423 parserOptions: { ecmaVersion: 2020 },
424 errors: [{
425 messageId: "useRecommendation",
426 data: { recommendation: "(foo?.indexOf)(1) !== -1" },
427 type: "UnaryExpression"
428 }]
eb39fafa
DC
429 }
430 ]
431});