]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-implicit-coercion.js
import 7.12.1 upstream release
[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 "+42"
92 ],
93 invalid: [
94 {
95 code: "!!foo",
96 output: "Boolean(foo)",
97 errors: [{
98 messageId: "useRecommendation",
99 data: { recommendation: "Boolean(foo)" },
100 type: "UnaryExpression"
101 }]
102 },
103 {
104 code: "!!(foo + bar)",
105 output: "Boolean(foo + bar)",
106 errors: [{
107 messageId: "useRecommendation",
108 data: { recommendation: "Boolean(foo + bar)" },
109 type: "UnaryExpression"
110 }]
111 },
112 {
113 code: "~foo.indexOf(1)",
114 output: null,
115 errors: [{
116 messageId: "useRecommendation",
117 data: { recommendation: "foo.indexOf(1) !== -1" },
118 type: "UnaryExpression"
119 }]
120 },
121 {
122 code: "~foo.bar.indexOf(2)",
123 output: null,
124 errors: [{
125 messageId: "useRecommendation",
126 data: { recommendation: "foo.bar.indexOf(2) !== -1" },
127 type: "UnaryExpression"
128 }]
129 },
130 {
131 code: "+foo",
132 output: "Number(foo)",
133 errors: [{
134 messageId: "useRecommendation",
135 data: { recommendation: "Number(foo)" },
136 type: "UnaryExpression"
137 }]
138 },
139 {
140 code: "+foo.bar",
141 output: "Number(foo.bar)",
142 errors: [{
143 messageId: "useRecommendation",
144 data: { recommendation: "Number(foo.bar)" },
145 type: "UnaryExpression"
146 }]
147 },
148 {
149 code: "1*foo",
150 output: "Number(foo)",
151 errors: [{
152 messageId: "useRecommendation",
153 data: { recommendation: "Number(foo)" },
154 type: "BinaryExpression"
155 }]
156 },
157 {
158 code: "foo*1",
159 output: "Number(foo)",
160 errors: [{
161 messageId: "useRecommendation",
162 data: { recommendation: "Number(foo)" },
163 type: "BinaryExpression"
164 }]
165 },
166 {
167 code: "1*foo.bar",
168 output: "Number(foo.bar)",
169 errors: [{
170 messageId: "useRecommendation",
171 data: { recommendation: "Number(foo.bar)" },
172 type: "BinaryExpression"
173 }]
174 },
175 {
176 code: "\"\"+foo",
177 output: "String(foo)",
178 errors: [{
179 messageId: "useRecommendation",
180 data: { recommendation: "String(foo)" },
181 type: "BinaryExpression"
182 }]
183 },
184 {
185 code: "``+foo",
186 output: "String(foo)",
187 parserOptions: { ecmaVersion: 6 },
188 errors: [{
189 messageId: "useRecommendation",
190 data: { recommendation: "String(foo)" },
191 type: "BinaryExpression"
192 }]
193 },
194 {
195 code: "foo+\"\"",
196 output: "String(foo)",
197 errors: [{
198 messageId: "useRecommendation",
199 data: { recommendation: "String(foo)" },
200 type: "BinaryExpression"
201 }]
202 },
203 {
204 code: "foo+``",
205 output: "String(foo)",
206 parserOptions: { ecmaVersion: 6 },
207 errors: [{
208 messageId: "useRecommendation",
209 data: { recommendation: "String(foo)" },
210 type: "BinaryExpression"
211 }]
212 },
213 {
214 code: "\"\"+foo.bar",
215 output: "String(foo.bar)",
216 errors: [{
217 messageId: "useRecommendation",
218 data: { recommendation: "String(foo.bar)" },
219 type: "BinaryExpression"
220 }]
221 },
222 {
223 code: "``+foo.bar",
224 output: "String(foo.bar)",
225 parserOptions: { ecmaVersion: 6 },
226 errors: [{
227 messageId: "useRecommendation",
228 data: { recommendation: "String(foo.bar)" },
229 type: "BinaryExpression"
230 }]
231 },
232 {
233 code: "foo.bar+\"\"",
234 output: "String(foo.bar)",
235 errors: [{
236 messageId: "useRecommendation",
237 data: { recommendation: "String(foo.bar)" },
238 type: "BinaryExpression"
239 }]
240 },
241 {
242 code: "foo.bar+``",
243 output: "String(foo.bar)",
244 parserOptions: { ecmaVersion: 6 },
245 errors: [{
246 messageId: "useRecommendation",
247 data: { recommendation: "String(foo.bar)" },
248 type: "BinaryExpression"
249 }]
250 },
251 {
252 code: "foo += \"\"",
253 output: "foo = String(foo)",
254 errors: [{
255 messageId: "useRecommendation",
256 data: { recommendation: "foo = String(foo)" },
257 type: "AssignmentExpression"
258 }]
259 },
260 {
261 code: "foo += ``",
262 output: "foo = String(foo)",
263 parserOptions: { ecmaVersion: 6 },
264 errors: [{
265 messageId: "useRecommendation",
266 data: { recommendation: "foo = String(foo)" },
267 type: "AssignmentExpression"
268 }]
269 },
270 {
271 code: "var a = !!foo",
272 output: "var a = Boolean(foo)",
273 options: [{ boolean: true, allow: ["~"] }],
274 errors: [{
275 messageId: "useRecommendation",
276 data: { recommendation: "Boolean(foo)" },
277 type: "UnaryExpression"
278 }]
279 },
280 {
281 code: "var a = ~foo.indexOf(1)",
282 output: null,
283 options: [{ boolean: true, allow: ["!!"] }],
284 errors: [{
285 messageId: "useRecommendation",
286 data: { recommendation: "foo.indexOf(1) !== -1" },
287 type: "UnaryExpression"
288 }]
289 },
290 {
291 code: "var a = 1 * foo",
292 output: "var a = Number(foo)",
293 options: [{ boolean: true, allow: ["+"] }],
294 errors: [{
295 messageId: "useRecommendation",
296 data: { recommendation: "Number(foo)" },
297 type: "BinaryExpression"
298 }]
299 },
300 {
301 code: "var a = +foo",
302 output: "var a = Number(foo)",
303 options: [{ boolean: true, allow: ["*"] }],
304 errors: [{
305 messageId: "useRecommendation",
306 data: { recommendation: "Number(foo)" },
307 type: "UnaryExpression"
308 }]
309 },
310 {
311 code: "var a = \"\" + foo",
312 output: "var a = String(foo)",
313 options: [{ boolean: true, allow: ["*"] }],
314 errors: [{
315 messageId: "useRecommendation",
316 data: { recommendation: "String(foo)" },
317 type: "BinaryExpression"
318 }]
319 },
320 {
321 code: "var a = `` + foo",
322 output: "var a = String(foo)",
323 options: [{ boolean: true, allow: ["*"] }],
324 parserOptions: { ecmaVersion: 6 },
325 errors: [{
326 messageId: "useRecommendation",
327 data: { recommendation: "String(foo)" },
328 type: "BinaryExpression"
329 }]
330 },
331 {
332 code: "typeof+foo",
333 output: "typeof Number(foo)",
334 errors: [{
335 messageId: "useRecommendation",
336 data: { recommendation: "Number(foo)" },
337 type: "UnaryExpression"
338 }]
339 },
340 {
341 code: "typeof +foo",
342 output: "typeof Number(foo)",
343 errors: [{
344 messageId: "useRecommendation",
345 data: { recommendation: "Number(foo)" },
346 type: "UnaryExpression"
347 }]
348 },
349 {
350 code: "let x ='' + 1n;",
351 output: "let x =String(1n);",
352 parserOptions: { ecmaVersion: 2020 },
353 errors: [{
354 messageId: "useRecommendation",
355 data: { recommendation: "String(1n)" },
356 type: "BinaryExpression"
357 }]
358 },
359
360 // Optional chaining
361 {
362 code: "~foo?.indexOf(1)",
363 output: null,
364 parserOptions: { ecmaVersion: 2020 },
365 errors: [{
366 messageId: "useRecommendation",
367 data: { recommendation: "foo?.indexOf(1) >= 0" },
368 type: "UnaryExpression"
369 }]
370 },
371 {
372 code: "~(foo?.indexOf)(1)",
373 output: null,
374 parserOptions: { ecmaVersion: 2020 },
375 errors: [{
376 messageId: "useRecommendation",
377 data: { recommendation: "(foo?.indexOf)(1) !== -1" },
378 type: "UnaryExpression"
379 }]
380 }
381 ]
382 });