]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-destructuring.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / prefer-destructuring.js
1 /**
2 * @fileoverview Prefer destructuring from arrays and objects
3 * @author Alex LaFroscia
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/prefer-destructuring"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020 } });
20
21 ruleTester.run("prefer-destructuring", rule, {
22 valid: [
23 "var [foo] = array;",
24 "var { foo } = object;",
25 "var foo;",
26 {
27
28 // Ensure that the default behavior does not require destructuring when renaming
29 code: "var foo = object.bar;",
30 options: [{ VariableDeclarator: { object: true } }]
31 },
32 {
33
34 // Ensure that the default behavior does not require destructuring when renaming
35 code: "var foo = object.bar;",
36 options: [{ object: true }]
37 },
38 {
39 code: "var foo = object.bar;",
40 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: false }]
41 },
42 {
43 code: "var foo = object.bar;",
44 options: [{ object: true }, { enforceForRenamedProperties: false }]
45 },
46 {
47 code: "var foo = object['bar'];",
48 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: false }]
49 },
50 {
51 code: "var foo = object[bar];",
52 options: [{ object: true }, { enforceForRenamedProperties: false }]
53 },
54 {
55 code: "var { bar: foo } = object;",
56 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }]
57 },
58 {
59 code: "var { bar: foo } = object;",
60 options: [{ object: true }, { enforceForRenamedProperties: true }]
61 },
62 {
63 code: "var { [bar]: foo } = object;",
64 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }]
65 },
66 {
67 code: "var { [bar]: foo } = object;",
68 options: [{ object: true }, { enforceForRenamedProperties: true }]
69 },
70 {
71 code: "var foo = array[0];",
72 options: [{ VariableDeclarator: { array: false } }]
73 },
74 {
75 code: "var foo = array[0];",
76 options: [{ array: false }]
77 },
78 {
79 code: "var foo = object.foo;",
80 options: [{ VariableDeclarator: { object: false } }]
81 },
82 {
83 code: "var foo = object['foo'];",
84 options: [{ VariableDeclarator: { object: false } }]
85 },
86 "({ foo } = object);",
87 {
88
89 // Fix #8654
90 code: "var foo = array[0];",
91 options: [{ VariableDeclarator: { array: false } }, { enforceForRenamedProperties: true }]
92 },
93 {
94
95 // Fix #8654
96 code: "var foo = array[0];",
97 options: [{ array: false }, { enforceForRenamedProperties: true }]
98 },
99 "[foo] = array;",
100 "foo += array[0]",
101 {
102 code: "foo &&= array[0]",
103 parserOptions: { ecmaVersion: 2021 }
104 },
105 "foo += bar.foo",
106 {
107 code: "foo ||= bar.foo",
108 parserOptions: { ecmaVersion: 2021 }
109 },
110 {
111 code: "foo ??= bar['foo']",
112 parserOptions: { ecmaVersion: 2021 }
113 },
114 {
115 code: "foo = object.foo;",
116 options: [{ AssignmentExpression: { object: false } }, { enforceForRenamedProperties: true }]
117 },
118 {
119 code: "foo = object.foo;",
120 options: [{ AssignmentExpression: { object: false } }, { enforceForRenamedProperties: false }]
121 },
122 {
123 code: "foo = array[0];",
124 options: [{ AssignmentExpression: { array: false } }, { enforceForRenamedProperties: true }]
125 },
126 {
127 code: "foo = array[0];",
128 options: [{ AssignmentExpression: { array: false } }, { enforceForRenamedProperties: false }]
129 },
130 {
131 code: "foo = array[0];",
132 options: [{ VariableDeclarator: { array: true }, AssignmentExpression: { array: false } }, { enforceForRenamedProperties: false }]
133 },
134 {
135 code: "var foo = array[0];",
136 options: [{ VariableDeclarator: { array: false }, AssignmentExpression: { array: true } }, { enforceForRenamedProperties: false }]
137 },
138 {
139 code: "foo = object.foo;",
140 options: [{ VariableDeclarator: { object: true }, AssignmentExpression: { object: false } }]
141 },
142 {
143 code: "var foo = object.foo;",
144 options: [{ VariableDeclarator: { object: false }, AssignmentExpression: { object: true } }]
145 },
146 "class Foo extends Bar { static foo() {var foo = super.foo} }",
147 "foo = bar[foo];",
148 "var foo = bar[foo];",
149 {
150 code: "var {foo: {bar}} = object;",
151 options: [{ object: true }]
152 },
153 {
154 code: "var {bar} = object.foo;",
155 options: [{ object: true }]
156 },
157
158 // Optional chaining
159 "var foo = array?.[0];", // because the fixed code can throw TypeError.
160 "var foo = object?.foo;"
161 ],
162
163 invalid: [
164 {
165 code: "var foo = array[0];",
166 output: null,
167 errors: [{
168 messageId: "preferDestructuring",
169 data: { type: "array" },
170 type: "VariableDeclarator"
171 }]
172 },
173 {
174 code: "foo = array[0];",
175 output: null,
176 errors: [{
177 messageId: "preferDestructuring",
178 data: { type: "array" },
179 type: "AssignmentExpression"
180 }]
181 },
182 {
183 code: "var foo = object.foo;",
184 output: "var {foo} = object;",
185 errors: [{
186 messageId: "preferDestructuring",
187 data: { type: "object" },
188 type: "VariableDeclarator"
189 }]
190 },
191 {
192 code: "var foo = (a, b).foo;",
193 output: "var {foo} = (a, b);",
194 errors: [{
195 messageId: "preferDestructuring",
196 data: { type: "object" },
197 type: "VariableDeclarator"
198 }]
199 },
200 {
201 code: "var length = (() => {}).length;",
202 output: "var {length} = () => {};",
203 errors: [{
204 messageId: "preferDestructuring",
205 data: { type: "object" },
206 type: "VariableDeclarator"
207 }]
208 },
209 {
210 code: "var foo = (a = b).foo;",
211 output: "var {foo} = a = b;",
212 errors: [{
213 messageId: "preferDestructuring",
214 data: { type: "object" },
215 type: "VariableDeclarator"
216 }]
217 },
218 {
219 code: "var foo = (a || b).foo;",
220 output: "var {foo} = a || b;",
221 errors: [{
222 messageId: "preferDestructuring",
223 data: { type: "object" },
224 type: "VariableDeclarator"
225 }]
226 },
227 {
228 code: "var foo = (f()).foo;",
229 output: "var {foo} = f();",
230 errors: [{
231 messageId: "preferDestructuring",
232 data: { type: "object" },
233 type: "VariableDeclarator"
234 }]
235 },
236 {
237 code: "var foo = object.bar.foo;",
238 output: "var {foo} = object.bar;",
239 errors: [{
240 messageId: "preferDestructuring",
241 data: { type: "object" },
242 type: "VariableDeclarator"
243 }]
244 },
245 {
246 code: "var foobar = object.bar;",
247 output: null,
248 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }],
249 errors: [{
250 messageId: "preferDestructuring",
251 data: { type: "object" },
252 type: "VariableDeclarator"
253 }]
254 },
255 {
256 code: "var foobar = object.bar;",
257 output: null,
258 options: [{ object: true }, { enforceForRenamedProperties: true }],
259 errors: [{
260 messageId: "preferDestructuring",
261 data: { type: "object" },
262 type: "VariableDeclarator"
263 }]
264 },
265 {
266 code: "var foo = object[bar];",
267 output: null,
268 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }],
269 errors: [{
270 messageId: "preferDestructuring",
271 data: { type: "object" },
272 type: "VariableDeclarator"
273 }]
274 },
275 {
276 code: "var foo = object[bar];",
277 output: null,
278 options: [{ object: true }, { enforceForRenamedProperties: true }],
279 errors: [{
280 messageId: "preferDestructuring",
281 data: { type: "object" },
282 type: "VariableDeclarator"
283 }]
284 },
285 {
286 code: "var foo = object[foo];",
287 output: null,
288 options: [{ object: true }, { enforceForRenamedProperties: true }],
289 errors: [{
290 messageId: "preferDestructuring",
291 data: { type: "object" },
292 type: "VariableDeclarator"
293 }]
294 },
295 {
296 code: "var foo = object['foo'];",
297 output: null,
298 errors: [{
299 messageId: "preferDestructuring",
300 data: { type: "object" },
301 type: "VariableDeclarator"
302 }]
303 },
304 {
305 code: "foo = object.foo;",
306 output: null,
307 errors: [{
308 messageId: "preferDestructuring",
309 data: { type: "object" },
310 type: "AssignmentExpression"
311 }]
312 },
313 {
314 code: "foo = object['foo'];",
315 output: null,
316 errors: [{
317 messageId: "preferDestructuring",
318 data: { type: "object" },
319 type: "AssignmentExpression"
320 }]
321 },
322 {
323 code: "var foo = array[0];",
324 output: null,
325 options: [{ VariableDeclarator: { array: true } }, { enforceForRenamedProperties: true }],
326 errors: [{
327 messageId: "preferDestructuring",
328 data: { type: "array" },
329 type: "VariableDeclarator"
330 }]
331 },
332 {
333 code: "foo = array[0];",
334 output: null,
335 options: [{ AssignmentExpression: { array: true } }],
336 errors: [{
337 messageId: "preferDestructuring",
338 data: { type: "array" },
339 type: "AssignmentExpression"
340 }]
341 },
342 {
343 code: "var foo = array[0];",
344 output: null,
345 options: [
346 {
347 VariableDeclarator: { array: true },
348 AssignmentExpression: { array: false }
349 },
350 { enforceForRenamedProperties: true }
351 ],
352 errors: [{
353 messageId: "preferDestructuring",
354 data: { type: "array" },
355 type: "VariableDeclarator"
356 }]
357 },
358 {
359 code: "var foo = array[0];",
360 output: null,
361 options: [
362 {
363 VariableDeclarator: { array: true },
364 AssignmentExpression: { array: false }
365 }
366 ],
367 errors: [{
368 messageId: "preferDestructuring",
369 data: { type: "array" },
370 type: "VariableDeclarator"
371 }]
372 },
373 {
374 code: "foo = array[0];",
375 output: null,
376 options: [
377 {
378 VariableDeclarator: { array: false },
379 AssignmentExpression: { array: true }
380 }
381 ],
382 errors: [{
383 messageId: "preferDestructuring",
384 data: { type: "array" },
385 type: "AssignmentExpression"
386 }]
387 },
388 {
389 code: "foo = object.foo;",
390 output: null,
391 options: [
392 {
393 VariableDeclarator: { array: true, object: false },
394 AssignmentExpression: { object: true }
395 }
396 ],
397 errors: [{
398 messageId: "preferDestructuring",
399 data: { type: "object" },
400 type: "AssignmentExpression"
401 }]
402 },
403 {
404 code: "class Foo extends Bar { static foo() {var bar = super.foo.bar} }",
405 output: "class Foo extends Bar { static foo() {var {bar} = super.foo} }",
406 errors: [{
407 messageId: "preferDestructuring",
408 data: { type: "object" },
409 type: "VariableDeclarator"
410 }]
411 },
412
413 // comments
414 {
415 code: "var /* comment */ foo = object.foo;",
416 output: "var /* comment */ {foo} = object;",
417 errors: [{
418 messageId: "preferDestructuring",
419 data: { type: "object" },
420 type: "VariableDeclarator"
421 }]
422 },
423 {
424 code: "var a, /* comment */foo = object.foo;",
425 output: "var a, /* comment */{foo} = object;",
426 errors: [{
427 messageId: "preferDestructuring",
428 data: { type: "object" },
429 type: "VariableDeclarator"
430 }]
431 },
432 {
433 code: "var foo /* comment */ = object.foo;",
434 output: null,
435 errors: [{
436 messageId: "preferDestructuring",
437 data: { type: "object" },
438 type: "VariableDeclarator"
439 }]
440 },
441 {
442 code: "var a, foo /* comment */ = object.foo;",
443 output: null,
444 errors: [{
445 messageId: "preferDestructuring",
446 data: { type: "object" },
447 type: "VariableDeclarator"
448 }]
449 },
450 {
451 code: "var foo /* comment */ = object.foo, a;",
452 output: null,
453 errors: [{
454 messageId: "preferDestructuring",
455 data: { type: "object" },
456 type: "VariableDeclarator"
457 }]
458 },
459 {
460 code: "var foo // comment\n = object.foo;",
461 output: null,
462 errors: [{
463 messageId: "preferDestructuring",
464 data: { type: "object" },
465 type: "VariableDeclarator"
466 }]
467 },
468 {
469 code: "var foo = /* comment */ object.foo;",
470 output: null,
471 errors: [{
472 messageId: "preferDestructuring",
473 data: { type: "object" },
474 type: "VariableDeclarator"
475 }]
476 },
477 {
478 code: "var foo = // comment\n object.foo;",
479 output: null,
480 errors: [{
481 messageId: "preferDestructuring",
482 data: { type: "object" },
483 type: "VariableDeclarator"
484 }]
485 },
486 {
487 code: "var foo = (/* comment */ object).foo;",
488 output: null,
489 errors: [{
490 messageId: "preferDestructuring",
491 data: { type: "object" },
492 type: "VariableDeclarator"
493 }]
494 },
495 {
496 code: "var foo = (object /* comment */).foo;",
497 output: null,
498 errors: [{
499 messageId: "preferDestructuring",
500 data: { type: "object" },
501 type: "VariableDeclarator"
502 }]
503 },
504 {
505 code: "var foo = bar(/* comment */).foo;",
506 output: "var {foo} = bar(/* comment */);",
507 errors: [{
508 messageId: "preferDestructuring",
509 data: { type: "object" },
510 type: "VariableDeclarator"
511 }]
512 },
513 {
514 code: "var foo = bar/* comment */.baz.foo;",
515 output: "var {foo} = bar/* comment */.baz;",
516 errors: [{
517 messageId: "preferDestructuring",
518 data: { type: "object" },
519 type: "VariableDeclarator"
520 }]
521 },
522 {
523 code: "var foo = bar[// comment\nbaz].foo;",
524 output: "var {foo} = bar[// comment\nbaz];",
525 errors: [{
526 messageId: "preferDestructuring",
527 data: { type: "object" },
528 type: "VariableDeclarator"
529 }]
530 },
531 {
532 code: "var foo // comment\n = bar(/* comment */).foo;",
533 output: null,
534 errors: [{
535 messageId: "preferDestructuring",
536 data: { type: "object" },
537 type: "VariableDeclarator"
538 }]
539 },
540 {
541 code: "var foo = bar/* comment */.baz/* comment */.foo;",
542 output: null,
543 errors: [{
544 messageId: "preferDestructuring",
545 data: { type: "object" },
546 type: "VariableDeclarator"
547 }]
548 },
549 {
550 code: "var foo = object// comment\n.foo;",
551 output: null,
552 errors: [{
553 messageId: "preferDestructuring",
554 data: { type: "object" },
555 type: "VariableDeclarator"
556 }]
557 },
558 {
559 code: "var foo = object./* comment */foo;",
560 output: null,
561 errors: [{
562 messageId: "preferDestructuring",
563 data: { type: "object" },
564 type: "VariableDeclarator"
565 }]
566 },
567 {
568 code: "var foo = (/* comment */ object.foo);",
569 output: null,
570 errors: [{
571 messageId: "preferDestructuring",
572 data: { type: "object" },
573 type: "VariableDeclarator"
574 }]
575 },
576 {
577 code: "var foo = (object.foo /* comment */);",
578 output: null,
579 errors: [{
580 messageId: "preferDestructuring",
581 data: { type: "object" },
582 type: "VariableDeclarator"
583 }]
584 },
585 {
586 code: "var foo = object.foo/* comment */;",
587 output: "var {foo} = object/* comment */;",
588 errors: [{
589 messageId: "preferDestructuring",
590 data: { type: "object" },
591 type: "VariableDeclarator"
592 }]
593 },
594 {
595 code: "var foo = object.foo// comment",
596 output: "var {foo} = object// comment",
597 errors: [{
598 messageId: "preferDestructuring",
599 data: { type: "object" },
600 type: "VariableDeclarator"
601 }]
602 },
603 {
604 code: "var foo = object.foo/* comment */, a;",
605 output: "var {foo} = object/* comment */, a;",
606 errors: [{
607 messageId: "preferDestructuring",
608 data: { type: "object" },
609 type: "VariableDeclarator"
610 }]
611 },
612 {
613 code: "var foo = object.foo// comment\n, a;",
614 output: "var {foo} = object// comment\n, a;",
615 errors: [{
616 messageId: "preferDestructuring",
617 data: { type: "object" },
618 type: "VariableDeclarator"
619 }]
620 },
621 {
622 code: "var foo = object.foo, /* comment */ a;",
623 output: "var {foo} = object, /* comment */ a;",
624 errors: [{
625 messageId: "preferDestructuring",
626 data: { type: "object" },
627 type: "VariableDeclarator"
628 }]
629 }
630 ]
631 });