2 * @fileoverview Prefer destructuring from arrays and objects
3 * @author Alex LaFroscia
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const rule
= require("../../../lib/rules/prefer-destructuring"),
12 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 2020 } });
21 ruleTester
.run("prefer-destructuring", rule
, {
24 "var { foo } = object;",
28 // Ensure that the default behavior does not require destructuring when renaming
29 code
: "var foo = object.bar;",
30 options
: [{ VariableDeclarator
: { object
: true } }]
34 // Ensure that the default behavior does not require destructuring when renaming
35 code
: "var foo = object.bar;",
36 options
: [{ object
: true }]
39 code
: "var foo = object.bar;",
40 options
: [{ VariableDeclarator
: { object
: true } }, { enforceForRenamedProperties
: false }]
43 code
: "var foo = object.bar;",
44 options
: [{ object
: true }, { enforceForRenamedProperties
: false }]
47 code
: "var foo = object['bar'];",
48 options
: [{ VariableDeclarator
: { object
: true } }, { enforceForRenamedProperties
: false }]
51 code
: "var foo = object[bar];",
52 options
: [{ object
: true }, { enforceForRenamedProperties
: false }]
55 code
: "var { bar: foo } = object;",
56 options
: [{ VariableDeclarator
: { object
: true } }, { enforceForRenamedProperties
: true }]
59 code
: "var { bar: foo } = object;",
60 options
: [{ object
: true }, { enforceForRenamedProperties
: true }]
63 code
: "var { [bar]: foo } = object;",
64 options
: [{ VariableDeclarator
: { object
: true } }, { enforceForRenamedProperties
: true }]
67 code
: "var { [bar]: foo } = object;",
68 options
: [{ object
: true }, { enforceForRenamedProperties
: true }]
71 code
: "var foo = array[0];",
72 options
: [{ VariableDeclarator
: { array
: false } }]
75 code
: "var foo = array[0];",
76 options
: [{ array
: false }]
79 code
: "var foo = object.foo;",
80 options
: [{ VariableDeclarator
: { object
: false } }]
83 code
: "var foo = object['foo'];",
84 options
: [{ VariableDeclarator
: { object
: false } }]
86 "({ foo } = object);",
90 code
: "var foo = array[0];",
91 options
: [{ VariableDeclarator
: { array
: false } }, { enforceForRenamedProperties
: true }]
96 code
: "var foo = array[0];",
97 options
: [{ array
: false }, { enforceForRenamedProperties
: true }]
102 code
: "foo &&= array[0]",
103 parserOptions
: { ecmaVersion
: 2021 }
107 code
: "foo ||= bar.foo",
108 parserOptions
: { ecmaVersion
: 2021 }
111 code
: "foo ??= bar['foo']",
112 parserOptions
: { ecmaVersion
: 2021 }
115 code
: "foo = object.foo;",
116 options
: [{ AssignmentExpression
: { object
: false } }, { enforceForRenamedProperties
: true }]
119 code
: "foo = object.foo;",
120 options
: [{ AssignmentExpression
: { object
: false } }, { enforceForRenamedProperties
: false }]
123 code
: "foo = array[0];",
124 options
: [{ AssignmentExpression
: { array
: false } }, { enforceForRenamedProperties
: true }]
127 code
: "foo = array[0];",
128 options
: [{ AssignmentExpression
: { array
: false } }, { enforceForRenamedProperties
: false }]
131 code
: "foo = array[0];",
132 options
: [{ VariableDeclarator
: { array
: true }, AssignmentExpression
: { array
: false } }, { enforceForRenamedProperties
: false }]
135 code
: "var foo = array[0];",
136 options
: [{ VariableDeclarator
: { array
: false }, AssignmentExpression
: { array
: true } }, { enforceForRenamedProperties
: false }]
139 code
: "foo = object.foo;",
140 options
: [{ VariableDeclarator
: { object
: true }, AssignmentExpression
: { object
: false } }]
143 code
: "var foo = object.foo;",
144 options
: [{ VariableDeclarator
: { object
: false }, AssignmentExpression
: { object
: true } }]
146 "class Foo extends Bar { static foo() {var foo = super.foo} }",
148 "var foo = bar[foo];",
150 code
: "var {foo: {bar}} = object;",
151 options
: [{ object
: true }]
154 code
: "var {bar} = object.foo;",
155 options
: [{ object
: true }]
159 "var foo = array?.[0];", // because the fixed code can throw TypeError.
160 "var foo = object?.foo;"
165 code
: "var foo = array[0];",
168 messageId
: "preferDestructuring",
169 data
: { type
: "array" },
170 type
: "VariableDeclarator"
174 code
: "foo = array[0];",
177 messageId
: "preferDestructuring",
178 data
: { type
: "array" },
179 type
: "AssignmentExpression"
183 code
: "var foo = object.foo;",
184 output
: "var {foo} = object;",
186 messageId
: "preferDestructuring",
187 data
: { type
: "object" },
188 type
: "VariableDeclarator"
192 code
: "var foo = (a, b).foo;",
193 output
: "var {foo} = (a, b);",
195 messageId
: "preferDestructuring",
196 data
: { type
: "object" },
197 type
: "VariableDeclarator"
201 code
: "var length = (() => {}).length;",
202 output
: "var {length} = () => {};",
204 messageId
: "preferDestructuring",
205 data
: { type
: "object" },
206 type
: "VariableDeclarator"
210 code
: "var foo = (a = b).foo;",
211 output
: "var {foo} = a = b;",
213 messageId
: "preferDestructuring",
214 data
: { type
: "object" },
215 type
: "VariableDeclarator"
219 code
: "var foo = (a || b).foo;",
220 output
: "var {foo} = a || b;",
222 messageId
: "preferDestructuring",
223 data
: { type
: "object" },
224 type
: "VariableDeclarator"
228 code
: "var foo = (f()).foo;",
229 output
: "var {foo} = f();",
231 messageId
: "preferDestructuring",
232 data
: { type
: "object" },
233 type
: "VariableDeclarator"
237 code
: "var foo = object.bar.foo;",
238 output
: "var {foo} = object.bar;",
240 messageId
: "preferDestructuring",
241 data
: { type
: "object" },
242 type
: "VariableDeclarator"
246 code
: "var foobar = object.bar;",
248 options
: [{ VariableDeclarator
: { object
: true } }, { enforceForRenamedProperties
: true }],
250 messageId
: "preferDestructuring",
251 data
: { type
: "object" },
252 type
: "VariableDeclarator"
256 code
: "var foobar = object.bar;",
258 options
: [{ object
: true }, { enforceForRenamedProperties
: true }],
260 messageId
: "preferDestructuring",
261 data
: { type
: "object" },
262 type
: "VariableDeclarator"
266 code
: "var foo = object[bar];",
268 options
: [{ VariableDeclarator
: { object
: true } }, { enforceForRenamedProperties
: true }],
270 messageId
: "preferDestructuring",
271 data
: { type
: "object" },
272 type
: "VariableDeclarator"
276 code
: "var foo = object[bar];",
278 options
: [{ object
: true }, { enforceForRenamedProperties
: true }],
280 messageId
: "preferDestructuring",
281 data
: { type
: "object" },
282 type
: "VariableDeclarator"
286 code
: "var foo = object[foo];",
288 options
: [{ object
: true }, { enforceForRenamedProperties
: true }],
290 messageId
: "preferDestructuring",
291 data
: { type
: "object" },
292 type
: "VariableDeclarator"
296 code
: "var foo = object['foo'];",
299 messageId
: "preferDestructuring",
300 data
: { type
: "object" },
301 type
: "VariableDeclarator"
305 code
: "foo = object.foo;",
308 messageId
: "preferDestructuring",
309 data
: { type
: "object" },
310 type
: "AssignmentExpression"
314 code
: "foo = object['foo'];",
317 messageId
: "preferDestructuring",
318 data
: { type
: "object" },
319 type
: "AssignmentExpression"
323 code
: "var foo = array[0];",
325 options
: [{ VariableDeclarator
: { array
: true } }, { enforceForRenamedProperties
: true }],
327 messageId
: "preferDestructuring",
328 data
: { type
: "array" },
329 type
: "VariableDeclarator"
333 code
: "foo = array[0];",
335 options
: [{ AssignmentExpression
: { array
: true } }],
337 messageId
: "preferDestructuring",
338 data
: { type
: "array" },
339 type
: "AssignmentExpression"
343 code
: "var foo = array[0];",
347 VariableDeclarator
: { array
: true },
348 AssignmentExpression
: { array
: false }
350 { enforceForRenamedProperties
: true }
353 messageId
: "preferDestructuring",
354 data
: { type
: "array" },
355 type
: "VariableDeclarator"
359 code
: "var foo = array[0];",
363 VariableDeclarator
: { array
: true },
364 AssignmentExpression
: { array
: false }
368 messageId
: "preferDestructuring",
369 data
: { type
: "array" },
370 type
: "VariableDeclarator"
374 code
: "foo = array[0];",
378 VariableDeclarator
: { array
: false },
379 AssignmentExpression
: { array
: true }
383 messageId
: "preferDestructuring",
384 data
: { type
: "array" },
385 type
: "AssignmentExpression"
389 code
: "foo = object.foo;",
393 VariableDeclarator
: { array
: true, object
: false },
394 AssignmentExpression
: { object
: true }
398 messageId
: "preferDestructuring",
399 data
: { type
: "object" },
400 type
: "AssignmentExpression"
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} }",
407 messageId
: "preferDestructuring",
408 data
: { type
: "object" },
409 type
: "VariableDeclarator"
415 code
: "var /* comment */ foo = object.foo;",
416 output
: "var /* comment */ {foo} = object;",
418 messageId
: "preferDestructuring",
419 data
: { type
: "object" },
420 type
: "VariableDeclarator"
424 code
: "var a, /* comment */foo = object.foo;",
425 output
: "var a, /* comment */{foo} = object;",
427 messageId
: "preferDestructuring",
428 data
: { type
: "object" },
429 type
: "VariableDeclarator"
433 code
: "var foo /* comment */ = object.foo;",
436 messageId
: "preferDestructuring",
437 data
: { type
: "object" },
438 type
: "VariableDeclarator"
442 code
: "var a, foo /* comment */ = object.foo;",
445 messageId
: "preferDestructuring",
446 data
: { type
: "object" },
447 type
: "VariableDeclarator"
451 code
: "var foo /* comment */ = object.foo, a;",
454 messageId
: "preferDestructuring",
455 data
: { type
: "object" },
456 type
: "VariableDeclarator"
460 code
: "var foo // comment\n = object.foo;",
463 messageId
: "preferDestructuring",
464 data
: { type
: "object" },
465 type
: "VariableDeclarator"
469 code
: "var foo = /* comment */ object.foo;",
472 messageId
: "preferDestructuring",
473 data
: { type
: "object" },
474 type
: "VariableDeclarator"
478 code
: "var foo = // comment\n object.foo;",
481 messageId
: "preferDestructuring",
482 data
: { type
: "object" },
483 type
: "VariableDeclarator"
487 code
: "var foo = (/* comment */ object).foo;",
490 messageId
: "preferDestructuring",
491 data
: { type
: "object" },
492 type
: "VariableDeclarator"
496 code
: "var foo = (object /* comment */).foo;",
499 messageId
: "preferDestructuring",
500 data
: { type
: "object" },
501 type
: "VariableDeclarator"
505 code
: "var foo = bar(/* comment */).foo;",
506 output
: "var {foo} = bar(/* comment */);",
508 messageId
: "preferDestructuring",
509 data
: { type
: "object" },
510 type
: "VariableDeclarator"
514 code
: "var foo = bar/* comment */.baz.foo;",
515 output
: "var {foo} = bar/* comment */.baz;",
517 messageId
: "preferDestructuring",
518 data
: { type
: "object" },
519 type
: "VariableDeclarator"
523 code
: "var foo = bar[// comment\nbaz].foo;",
524 output
: "var {foo} = bar[// comment\nbaz];",
526 messageId
: "preferDestructuring",
527 data
: { type
: "object" },
528 type
: "VariableDeclarator"
532 code
: "var foo // comment\n = bar(/* comment */).foo;",
535 messageId
: "preferDestructuring",
536 data
: { type
: "object" },
537 type
: "VariableDeclarator"
541 code
: "var foo = bar/* comment */.baz/* comment */.foo;",
544 messageId
: "preferDestructuring",
545 data
: { type
: "object" },
546 type
: "VariableDeclarator"
550 code
: "var foo = object// comment\n.foo;",
553 messageId
: "preferDestructuring",
554 data
: { type
: "object" },
555 type
: "VariableDeclarator"
559 code
: "var foo = object./* comment */foo;",
562 messageId
: "preferDestructuring",
563 data
: { type
: "object" },
564 type
: "VariableDeclarator"
568 code
: "var foo = (/* comment */ object.foo);",
571 messageId
: "preferDestructuring",
572 data
: { type
: "object" },
573 type
: "VariableDeclarator"
577 code
: "var foo = (object.foo /* comment */);",
580 messageId
: "preferDestructuring",
581 data
: { type
: "object" },
582 type
: "VariableDeclarator"
586 code
: "var foo = object.foo/* comment */;",
587 output
: "var {foo} = object/* comment */;",
589 messageId
: "preferDestructuring",
590 data
: { type
: "object" },
591 type
: "VariableDeclarator"
595 code
: "var foo = object.foo// comment",
596 output
: "var {foo} = object// comment",
598 messageId
: "preferDestructuring",
599 data
: { type
: "object" },
600 type
: "VariableDeclarator"
604 code
: "var foo = object.foo/* comment */, a;",
605 output
: "var {foo} = object/* comment */, a;",
607 messageId
: "preferDestructuring",
608 data
: { type
: "object" },
609 type
: "VariableDeclarator"
613 code
: "var foo = object.foo// comment\n, a;",
614 output
: "var {foo} = object// comment\n, a;",
616 messageId
: "preferDestructuring",
617 data
: { type
: "object" },
618 type
: "VariableDeclarator"
622 code
: "var foo = object.foo, /* comment */ a;",
623 output
: "var {foo} = object, /* comment */ a;",
625 messageId
: "preferDestructuring",
626 data
: { type
: "object" },
627 type
: "VariableDeclarator"