]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/logical-assignment-operators.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / logical-assignment-operators.js
1 /**
2 * @fileoverview Tests for logical-assignment-operators.
3 * @author Daniel Martens
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/logical-assignment-operators"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2021 } });
19
20 ruleTester.run("logical-assignment-operators", rule, {
21 valid: [
22
23 // Unrelated
24 "a || b",
25 "a && b",
26 "a ?? b",
27 "a || a || b",
28 "var a = a || b",
29 "a === undefined ? a : b",
30 "while (a) a = b",
31
32 // Preferred
33 "a ||= b",
34 "a &&= b",
35 "a ??= b",
36
37 // > Operator
38 "a += a || b",
39 "a *= a || b",
40 "a ||= a || b",
41 "a &&= a || b",
42
43 // > Right
44 "a = a",
45 "a = b",
46 "a = a === b",
47 "a = a + b",
48 "a = a / b",
49 "a = fn(a) || b",
50
51 // > Reference
52 "a = false || c",
53 "a = f() || g()",
54 "a = b || c",
55 "a = b || a",
56 "object.a = object.b || c",
57 "[a] = a || b",
58 "({ a } = a || b)",
59
60 // Logical
61 "(a = b) || a",
62 "a + (a = b)",
63 "a || (b ||= c)",
64 "a || (b &&= c)",
65 "a || b === 0",
66 "a || fn()",
67 "a || (b && c)",
68 "a || (b ?? c)",
69
70 // > Reference
71 "a || (b = c)",
72 "a || (a ||= b)",
73 "fn() || (a = b)",
74 "a.b || (a = b)",
75 "a?.b || (a.b = b)",
76 {
77 code: "class Class { #prop; constructor() { this.#prop || (this.prop = value) } }",
78 parserOptions: { ecmaVersion: 2022 }
79 }, {
80 code: "class Class { #prop; constructor() { this.prop || (this.#prop = value) } }",
81 parserOptions: { ecmaVersion: 2022 }
82 },
83
84 // If
85 "if (a) a = b",
86 {
87 code: "if (a) a = b",
88 options: ["always", { enforceForIfStatements: false }]
89 }, {
90 code: "if (a) { a = b } else {}",
91 options: ["always", { enforceForIfStatements: true }]
92 }, {
93 code: "if (a) { a = b } else if (a) {}",
94 options: ["always", { enforceForIfStatements: true }]
95 }, {
96 code: "if (unrelated) {} else if (a) a = b; else {}",
97 options: ["always", { enforceForIfStatements: true }]
98 }, {
99 code: "if (unrelated) {} else if (a) a = b; else if (unrelated) {}",
100 options: ["always", { enforceForIfStatements: true }]
101 },
102
103 // > Body
104 {
105 code: "if (a) {}",
106 options: ["always", { enforceForIfStatements: true }]
107 }, {
108 code: "if (a) { before; a = b }",
109 options: ["always", { enforceForIfStatements: true }]
110 }, {
111 code: "if (a) { a = b; after }",
112 options: ["always", { enforceForIfStatements: true }]
113 }, {
114 code: "if (a) throw new Error()",
115 options: ["always", { enforceForIfStatements: true }]
116 }, {
117 code: "if (a) a",
118 options: ["always", { enforceForIfStatements: true }]
119 }, {
120 code: "if (a) a ||= b",
121 options: ["always", { enforceForIfStatements: true }]
122 }, {
123 code: "if (a) b = a",
124 options: ["always", { enforceForIfStatements: true }]
125 }, {
126 code: "if (a) { a() }",
127 options: ["always", { enforceForIfStatements: true }]
128 }, {
129 code: "if (a) { a += a || b }",
130 options: ["always", { enforceForIfStatements: true }]
131 },
132
133 // > Test
134 {
135 code: "if (true) a = b",
136 options: ["always", { enforceForIfStatements: true }]
137 }, {
138 code: "if (predicate(a)) a = b",
139 options: ["always", { enforceForIfStatements: true }]
140 }, {
141 code: "if (a?.b) a.b = c",
142 options: ["always", { enforceForIfStatements: true }]
143 }, {
144 code: "if (!a?.b) a.b = c",
145 options: ["always", { enforceForIfStatements: true }]
146 }, {
147 code: "if (a === b) a = b",
148 options: ["always", { enforceForIfStatements: true }]
149 }, {
150 code: "if (a === undefined) a = b",
151 options: ["always", { enforceForIfStatements: true }]
152 }, {
153 code: "if (a === null) a = b",
154 options: ["always", { enforceForIfStatements: true }]
155 }, {
156 code: "if (a != null) a = b",
157 options: ["always", { enforceForIfStatements: true }]
158 }, {
159 code: "if (a === null && a === undefined) a = b",
160 options: ["always", { enforceForIfStatements: true }]
161 }, {
162 code: "if (a === 0 || a === undefined) a = b",
163 options: ["always", { enforceForIfStatements: true }]
164 }, {
165 code: "if (a === null || a === 1) a = b",
166 options: ["always", { enforceForIfStatements: true }]
167 }, {
168 code: "if (a == null || a == undefined) a = b",
169 options: ["always", { enforceForIfStatements: true }]
170 }, {
171 code: "if (a === null || a === !0) a = b",
172 options: ["always", { enforceForIfStatements: true }]
173 }, {
174 code: "if (a === null || a === +0) a = b",
175 options: ["always", { enforceForIfStatements: true }]
176 }, {
177 code: "if (a === null || a === null) a = b",
178 options: ["always", { enforceForIfStatements: true }]
179 }, {
180 code: "if (a === undefined || a === void 0) a = b",
181 options: ["always", { enforceForIfStatements: true }]
182 }, {
183 code: "if (a === null || a === void void 0) a = b",
184 options: ["always", { enforceForIfStatements: true }]
185 }, {
186 code: "if (a === null || a === void 'string') a = b",
187 options: ["always", { enforceForIfStatements: true }]
188 }, {
189 code: "if (a === null || a === void fn()) a = b",
190 options: ["always", { enforceForIfStatements: true }]
191 },
192
193 // > Test > Yoda
194 {
195 code: "if (a == a) a = b",
196 options: ["always", { enforceForIfStatements: true }]
197 }, {
198 code: "if (a == b) a = b",
199 options: ["always", { enforceForIfStatements: true }]
200 }, {
201 code: "if (null == null) a = b",
202 options: ["always", { enforceForIfStatements: true }]
203 }, {
204 code: "if (undefined == undefined) undefined = b",
205 options: ["always", { enforceForIfStatements: true }]
206 }, {
207 code: "if (null == x) a = b",
208 options: ["always", { enforceForIfStatements: true }]
209 }, {
210 code: "if (null == fn()) a = b",
211 options: ["always", { enforceForIfStatements: true }]
212 }, {
213 code: "if (null === a || a === 0) a = b",
214 options: ["always", { enforceForIfStatements: true }]
215 }, {
216 code: "if (0 === a || null === a) a = b",
217 options: ["always", { enforceForIfStatements: true }]
218 }, {
219 code: "if (1 === a || a === undefined) a = b",
220 options: ["always", { enforceForIfStatements: true }]
221 }, {
222 code: "if (undefined === a || 1 === a) a = b",
223 options: ["always", { enforceForIfStatements: true }]
224 }, {
225 code: "if (a === null || a === b) a = b",
226 options: ["always", { enforceForIfStatements: true }]
227 }, {
228 code: "if (b === undefined || a === null) a = b",
229 options: ["always", { enforceForIfStatements: true }]
230 }, {
231 code: "if (null === a || b === a) a = b",
232 options: ["always", { enforceForIfStatements: true }]
233 }, {
234 code: "if (null === null || undefined === undefined) a = b",
235 options: ["always", { enforceForIfStatements: true }]
236 }, {
237 code: "if (null === null || a === a) a = b",
238 options: ["always", { enforceForIfStatements: true }]
239 }, {
240 code: "if (undefined === undefined || a === a) a = b",
241 options: ["always", { enforceForIfStatements: true }]
242 }, {
243 code: "if (null === undefined || a === a) a = b",
244 options: ["always", { enforceForIfStatements: true }]
245 },
246
247 // > Test > Undefined
248 {
249 code: [
250 "{",
251 " const undefined = 0;",
252 " if (a == undefined) a = b",
253 "}"
254 ].join("\n"),
255 options: ["always", { enforceForIfStatements: true }]
256 }, {
257 code: [
258 "(() => {",
259 " const undefined = 0;",
260 " if (condition) {",
261 " if (a == undefined) a = b",
262 " }",
263 "})()"
264 ].join("\n"),
265 options: ["always", { enforceForIfStatements: true }]
266 }, {
267 code: [
268 "{",
269 " if (a == undefined) a = b",
270 "}",
271 "var undefined = 0;"
272 ].join("\n"),
273 options: ["always", { enforceForIfStatements: true }]
274 }, {
275 code: [
276 "{",
277 " const undefined = 0;",
278 " if (undefined == null) undefined = b",
279 "}"
280 ].join("\n"),
281 options: ["always", { enforceForIfStatements: true }]
282 }, {
283 code: [
284 "{",
285 " const undefined = 0;",
286 " if (a === undefined || a === null) a = b",
287 "}"
288 ].join("\n"),
289 options: ["always", { enforceForIfStatements: true }]
290 }, {
291 code: [
292 "{",
293 " const undefined = 0;",
294 " if (undefined === a || null === a) a = b",
295 "}"
296 ].join("\n"),
297 options: ["always", { enforceForIfStatements: true }]
298 },
299
300 // > Reference
301 {
302 code: "if (a) b = c",
303 options: ["always", { enforceForIfStatements: true }]
304 }, {
305 code: "if (!a) b = c",
306 options: ["always", { enforceForIfStatements: true }]
307 }, {
308 code: "if (!!a) b = c",
309 options: ["always", { enforceForIfStatements: true }]
310 }, {
311 code: "if (a == null) b = c",
312 options: ["always", { enforceForIfStatements: true }]
313 }, {
314 code: "if (a === null || a === undefined) b = c",
315 options: ["always", { enforceForIfStatements: true }]
316 }, {
317 code: "if (a === null || b === undefined) a = b",
318 options: ["always", { enforceForIfStatements: true }]
319 }, {
320 code: "if (a === null || b === undefined) b = c",
321 options: ["always", { enforceForIfStatements: true }]
322 }, {
323 code: "if (Boolean(a)) b = c",
324 options: ["always", { enforceForIfStatements: true }]
325 }, {
326 code: [
327 "function fn(Boolean) {",
328 " if (Boolean(a)) a = b",
329 "}"
330 ].join("\n"),
331 options: ["always", { enforceForIfStatements: true }]
332 },
333
334 // Never
335 {
336 code: "a = a || b",
337 options: ["never"]
338 }, {
339 code: "a = a && b",
340 options: ["never"]
341 }, {
342 code: "a = a ?? b",
343 options: ["never"]
344 }, {
345 code: "a = b",
346 options: ["never"]
347 }, {
348 code: "a += b",
349 options: ["never"]
350 }, {
351 code: "a -= b",
352 options: ["never"]
353 }, {
354 code: "a.b = a.b || c",
355 options: ["never"]
356 }
357 ],
358 invalid: [
359
360 // Assignment
361 {
362 code: "a = a || b",
363 output: "a ||= b",
364 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
365 }, {
366 code: "a = a && b",
367 output: "a &&= b",
368 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "&&=" }, suggestions: [] }]
369 }, {
370 code: "a = a ?? b",
371 output: "a ??= b",
372 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "??=" }, suggestions: [] }]
373 }, {
374 code: "foo = foo || bar",
375 output: "foo ||= bar",
376 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
377 },
378
379 // > Right
380 {
381 code: "a = a || fn()",
382 output: "a ||= fn()",
383 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
384 }, {
385 code: "a = a || b && c",
386 output: "a ||= b && c",
387 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
388 }, {
389 code: "a = a || (b || c)",
390 output: "a ||= (b || c)",
391 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
392 }, {
393 code: "a = a || (b ? c : d)",
394 output: "a ||= (b ? c : d)",
395 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
396 },
397
398 // > Comments
399 {
400 code: "/* before */ a = a || b",
401 output: "/* before */ a ||= b",
402 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
403 }, {
404 code: "a = a || b // after",
405 output: "a ||= b // after",
406 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
407 }, {
408 code: "a /* between */ = a || b",
409 output: null,
410 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
411 }, {
412 code: "a = /** @type */ a || b",
413 output: null,
414 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
415 }, {
416 code: "a = a || /* between */ b",
417 output: null,
418 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
419 },
420
421 // > Parenthesis
422 {
423 code: "(a) = a || b",
424 output: "(a) ||= b",
425 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
426 }, {
427 code: "a = (a) || b",
428 output: "a ||= b",
429 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
430 }, {
431 code: "a = a || (b)",
432 output: "a ||= (b)",
433 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
434 }, {
435 code: "a = a || ((b))",
436 output: "a ||= ((b))",
437 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
438 }, {
439 code: "(a = a || b)",
440 output: "(a ||= b)",
441 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
442 }, {
443 code: "a = a || (f(), b)",
444 output: "a ||= (f(), b)",
445 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
446 },
447
448 // > Suggestions
449 {
450 code: "a.b = a.b ?? c",
451 output: null,
452 errors: [{
453 messageId: "assignment",
454 type: "AssignmentExpression",
455 data: { operator: "??=" },
456 suggestions: [{
457 messageId: "useLogicalOperator",
458 data: { operator: "??=" },
459 output: "a.b ??= c"
460 }]
461 }]
462 }, {
463 code: "a.b.c = a.b.c ?? d",
464 output: null,
465 errors: [{
466 messageId: "assignment",
467 type: "AssignmentExpression",
468 data: { operator: "??=" },
469 suggestions: [{
470 messageId: "useLogicalOperator",
471 data: { operator: "??=" },
472 output: "a.b.c ??= d"
473 }]
474 }]
475 }, {
476 code: "a[b] = a[b] ?? c",
477 output: null,
478 errors: [{
479 messageId: "assignment",
480 type: "AssignmentExpression",
481 data: { operator: "??=" },
482 suggestions: [{
483 messageId: "useLogicalOperator",
484 data: { operator: "??=" },
485 output: "a[b] ??= c"
486 }]
487 }]
488 }, {
489 code: "a['b'] = a['b'] ?? c",
490 output: null,
491 errors: [{
492 messageId: "assignment",
493 type: "AssignmentExpression",
494 data: { operator: "??=" },
495 suggestions: [{
496 messageId: "useLogicalOperator",
497 data: { operator: "??=" },
498 output: "a['b'] ??= c"
499 }]
500 }]
501 }, {
502 code: "a.b = a['b'] ?? c",
503 output: null,
504 errors: [{
505 messageId: "assignment",
506 type: "AssignmentExpression",
507 data: { operator: "??=" },
508 suggestions: [{
509 messageId: "useLogicalOperator",
510 data: { operator: "??=" },
511 output: "a.b ??= c"
512 }]
513 }]
514 }, {
515 code: "a['b'] = a.b ?? c",
516 output: null,
517 errors: [{
518 messageId: "assignment",
519 type: "AssignmentExpression",
520 data: { operator: "??=" },
521 suggestions: [{
522 messageId: "useLogicalOperator",
523 data: { operator: "??=" },
524 output: "a['b'] ??= c"
525 }]
526 }]
527 }, {
528 code: "this.prop = this.prop ?? {}",
529 output: null,
530 errors: [{
531 messageId: "assignment",
532 type: "AssignmentExpression",
533 data: { operator: "??=" },
534 suggestions: [{
535 messageId: "useLogicalOperator",
536 data: { operator: "??=" },
537 output: "this.prop ??= {}"
538 }]
539 }]
540 },
541
542 // > With
543 {
544 code: "with (object) a = a || b",
545 output: null,
546 errors: [{
547 messageId: "assignment",
548 type: "AssignmentExpression",
549 data: { operator: "||=" },
550 suggestions: [{
551 messageId: "useLogicalOperator",
552 data: { operator: "||=" },
553
554 output: "with (object) a ||= b"
555 }]
556 }]
557 }, {
558 code: "with (object) { a = a || b }",
559 output: null,
560 errors: [{
561 messageId: "assignment",
562 type: "AssignmentExpression",
563 data: { operator: "||=" },
564 suggestions: [{
565 messageId: "useLogicalOperator",
566 data: { operator: "||=" },
567 output: "with (object) { a ||= b }"
568 }]
569 }]
570 }, {
571 code: "with (object) { if (condition) a = a || b }",
572 output: null,
573 errors: [{
574 messageId: "assignment",
575 type: "AssignmentExpression",
576 data: { operator: "||=" },
577 suggestions: [{
578 messageId: "useLogicalOperator",
579 data: { operator: "||=" },
580 output: "with (object) { if (condition) a ||= b }"
581 }]
582 }]
583 }, {
584 code: "with (a = a || b) {}",
585 output: "with (a ||= b) {}",
586 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
587 }, {
588 code: "with (object) {} a = a || b",
589 output: "with (object) {} a ||= b",
590 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
591 }, {
592 code: "a = a || b; with (object) {}",
593 output: "a ||= b; with (object) {}",
594 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
595 }, {
596 code: "if (condition) a = a || b",
597 output: "if (condition) a ||= b",
598 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
599 }, {
600 code: [
601 "with (object) {",
602 ' "use strict";',
603 " a = a || b",
604 "}"
605 ].join("\n"),
606 output: null,
607 errors: [{
608 messageId: "assignment",
609 type: "AssignmentExpression",
610 data: { operator: "||=" },
611 suggestions: [{
612 messageId: "useLogicalOperator",
613 data: { operator: "||=" },
614 output: [
615 "with (object) {",
616 ' "use strict";',
617 " a ||= b",
618 "}"
619 ].join("\n")
620 }]
621 }]
622 },
623
624 // > Context
625 {
626 code: "fn(a = a || b)",
627 output: "fn(a ||= b)",
628 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
629 }, {
630 code: "fn((a = a || b))",
631 output: "fn((a ||= b))",
632 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
633 }, {
634 code: "(a = a || b) ? c : d",
635 output: "(a ||= b) ? c : d",
636 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
637 }, {
638 code: "a = b = b || c",
639 output: "a = b ||= c",
640 errors: [{ messageId: "assignment", type: "AssignmentExpression", data: { operator: "||=" }, suggestions: [] }]
641 },
642
643 // Logical
644 {
645 code: "a || (a = b)",
646 output: "a ||= b",
647 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
648 }, {
649 code: "a && (a = b)",
650 output: "a &&= b",
651 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "&&=" } }]
652 }, {
653 code: "a ?? (a = b)",
654 output: "a ??= b",
655 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "??=" } }]
656 }, {
657 code: "foo ?? (foo = bar)",
658 output: "foo ??= bar",
659 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "??=" } }]
660 },
661
662 // > Right
663 {
664 code: "a || (a = 0)",
665 output: "a ||= 0",
666 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
667 }, {
668 code: "a || (a = fn())",
669 output: "a ||= fn()",
670 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
671 }, {
672 code: "a || (a = (b || c))",
673 output: "a ||= (b || c)",
674 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
675 },
676
677 // > Parenthesis
678 {
679 code: "(a) || (a = b)",
680 output: "a ||= b",
681 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
682 }, {
683 code: "a || ((a) = b)",
684 output: "(a) ||= b",
685 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
686 }, {
687 code: "a || (a = (b))",
688 output: "a ||= (b)",
689 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
690 }, {
691 code: "a || ((a = b))",
692 output: "a ||= b",
693 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
694 }, {
695 code: "a || (((a = b)))",
696 output: "a ||= b",
697 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
698 }, {
699 code: "a || ( ( a = b ) )",
700 output: "a ||= b",
701 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
702 },
703
704 // > Comments
705 {
706 code: "/* before */ a || (a = b)",
707 output: "/* before */ a ||= b",
708 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
709 }, {
710 code: "a || (a = b) // after",
711 output: "a ||= b // after",
712 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
713 }, {
714 code: "a /* between */ || (a = b)",
715 output: null,
716 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
717 }, {
718 code: "a || /* between */ (a = b)",
719 output: null,
720 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
721 },
722
723 // > Fix Condition
724 {
725 code: "a.b || (a.b = c)",
726 output: "a.b ||= c",
727 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
728 }, {
729 code: "class Class { #prop; constructor() { this.#prop || (this.#prop = value) } }",
730 output: "class Class { #prop; constructor() { this.#prop ||= value } }",
731 parserOptions: { ecmaVersion: 2022 },
732 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
733 }, {
734 code: "a['b'] || (a['b'] = c)",
735 output: "a['b'] ||= c",
736 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
737 }, {
738 code: "a[0] || (a[0] = b)",
739 output: "a[0] ||= b",
740 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
741 }, {
742 code: "a[this] || (a[this] = b)",
743 output: "a[this] ||= b",
744 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
745 }, {
746 code: "foo.bar || (foo.bar = baz)",
747 output: "foo.bar ||= baz",
748 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
749 }, {
750 code: "a.b.c || (a.b.c = d)",
751 output: null,
752 errors: [{
753 messageId: "logical",
754 type: "LogicalExpression",
755 data: { operator: "||=" },
756 suggestions: [{
757 messageId: "convertLogical",
758 data: { operator: "||=" },
759 output: "a.b.c ||= d"
760 }]
761 }]
762 }, {
763 code: "a[b.c] || (a[b.c] = d)",
764 output: null,
765 errors: [{
766 messageId: "logical",
767 type: "LogicalExpression",
768 data: { operator: "||=" },
769 suggestions: [{
770 messageId: "convertLogical",
771 data: { operator: "||=" },
772 output: "a[b.c] ||= d"
773 }]
774 }]
775 }, {
776 code: "a[b?.c] || (a[b?.c] = d)",
777 output: null,
778 errors: [{
779 messageId: "logical",
780 type: "LogicalExpression",
781 data: { operator: "||=" },
782 suggestions: [{
783 messageId: "convertLogical",
784 data: { operator: "||=" },
785 output: "a[b?.c] ||= d"
786 }]
787 }]
788 }, {
789 code: "with (object) a.b || (a.b = c)",
790 output: null,
791 errors: [{
792 messageId: "logical",
793 type: "LogicalExpression",
794 data: { operator: "||=" },
795 suggestions: [{
796 messageId: "convertLogical",
797 data: { operator: "||=" },
798 output: "with (object) a.b ||= c"
799 }]
800 }]
801 },
802
803 // > Context
804 {
805 code: "a = a.b || (a.b = {})",
806 output: "a = a.b ||= {}",
807 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" }, suggestions: [] }]
808 },
809 {
810 code: "a || (a = 0) || b",
811 output: "(a ||= 0) || b",
812 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
813 }, {
814 code: "(a || (a = 0)) || b",
815 output: "(a ||= 0) || b",
816 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
817 }, {
818 code: "a || (b || (b = 0))",
819 output: "a || (b ||= 0)",
820 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
821 }, {
822 code: "a = b || (b = c)",
823 output: "a = b ||= c",
824 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
825 }, {
826 code: "a || (a = 0) ? b : c",
827 output: "(a ||= 0) ? b : c",
828 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
829 }, {
830 code: "fn(a || (a = 0))",
831 output: "fn(a ||= 0)",
832 errors: [{ messageId: "logical", type: "LogicalExpression", data: { operator: "||=" } }]
833 },
834
835 // If
836 {
837 code: "if (a) a = b",
838 output: "a &&= b",
839 options: ["always", { enforceForIfStatements: true }],
840 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
841 }, {
842 code: "if (Boolean(a)) a = b",
843 output: "a &&= b",
844 options: ["always", { enforceForIfStatements: true }],
845 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
846 }, {
847 code: "if (!!a) a = b",
848 output: "a &&= b",
849 options: ["always", { enforceForIfStatements: true }],
850 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
851 }, {
852 code: "if (!a) a = b",
853 output: "a ||= b",
854 options: ["always", { enforceForIfStatements: true }],
855 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "||=" } }]
856 }, {
857 code: "if (!Boolean(a)) a = b",
858 output: "a ||= b",
859 options: ["always", { enforceForIfStatements: true }],
860 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "||=" } }]
861 }, {
862 code: "if (a == undefined) a = b",
863 output: "a ??= b",
864 options: ["always", { enforceForIfStatements: true }],
865 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
866 }, {
867 code: "if (a == null) a = b",
868 output: "a ??= b",
869 options: ["always", { enforceForIfStatements: true }],
870 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
871 }, {
872 code: "if (a === null || a === undefined) a = b",
873 output: "a ??= b",
874 options: ["always", { enforceForIfStatements: true }],
875 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
876 }, {
877 code: "if (a === undefined || a === null) a = b",
878 output: "a ??= b",
879 options: ["always", { enforceForIfStatements: true }],
880 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
881 }, {
882 code: "if (a === null || a === void 0) a = b",
883 output: "a ??= b",
884 options: ["always", { enforceForIfStatements: true }],
885 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
886 }, {
887 code: "if (a === void 0 || a === null) a = b",
888 output: "a ??= b",
889 options: ["always", { enforceForIfStatements: true }],
890 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
891 }, {
892 code: "if (a) { a = b; }",
893 output: "a &&= b;",
894 options: ["always", { enforceForIfStatements: true }],
895 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
896 }, {
897 code: [
898 "{ const undefined = 0; }",
899 "if (a == undefined) a = b"
900 ].join("\n"),
901 output: [
902 "{ const undefined = 0; }",
903 "a ??= b"
904 ].join("\n"),
905 options: ["always", { enforceForIfStatements: true }],
906 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
907 }, {
908 code: [
909 "if (a == undefined) a = b",
910 "{ const undefined = 0; }"
911 ].join("\n"),
912 output: [
913 "a ??= b",
914 "{ const undefined = 0; }"
915 ].join("\n"),
916 options: ["always", { enforceForIfStatements: true }],
917 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" } }]
918 },
919
920 // > Yoda
921 {
922 code: "if (null == a) a = b",
923 output: "a ??= b",
924 options: ["always", { enforceForIfStatements: true }],
925 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
926 }, {
927 code: "if (undefined == a) a = b",
928 output: "a ??= b",
929 options: ["always", { enforceForIfStatements: true }],
930 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
931 }, {
932 code: "if (undefined === a || a === null) a = b",
933 output: "a ??= b",
934 options: ["always", { enforceForIfStatements: true }],
935 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
936 }, {
937 code: "if (a === undefined || null === a) a = b",
938 output: "a ??= b",
939 options: ["always", { enforceForIfStatements: true }],
940 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
941 }, {
942 code: "if (undefined === a || null === a) a = b",
943 output: "a ??= b",
944 options: ["always", { enforceForIfStatements: true }],
945 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
946 }, {
947 code: "if (null === a || a === undefined) a = b",
948 output: "a ??= b",
949 options: ["always", { enforceForIfStatements: true }],
950 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
951 }, {
952 code: "if (a === null || undefined === a) a = b",
953 output: "a ??= b",
954 options: ["always", { enforceForIfStatements: true }],
955 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
956 }, {
957 code: "if (null === a || undefined === a) a = b",
958 output: "a ??= b",
959 options: ["always", { enforceForIfStatements: true }],
960 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "??=" }, suggestions: [] }]
961 },
962
963 // > Parenthesis
964 {
965 code: "if ((a)) a = b",
966 output: "a &&= b",
967 options: ["always", { enforceForIfStatements: true }],
968 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
969 }, {
970 code: "if (a) (a) = b",
971 output: "(a) &&= b",
972 options: ["always", { enforceForIfStatements: true }],
973 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
974 }, {
975 code: "if (a) a = (b)",
976 output: "a &&= (b)",
977 options: ["always", { enforceForIfStatements: true }],
978 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
979 }, {
980 code: "if (a) (a = b)",
981 output: "(a &&= b)",
982 options: ["always", { enforceForIfStatements: true }],
983 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
984 },
985
986 // > Previous statement
987 {
988 code: ";if (a) (a) = b",
989 output: ";(a) &&= b",
990 options: ["always", { enforceForIfStatements: true }],
991 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
992 }, {
993 code: "{ if (a) (a) = b }",
994 output: "{ (a) &&= b }",
995 options: ["always", { enforceForIfStatements: true }],
996 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
997 }, {
998 code: "fn();if (a) (a) = b",
999 output: "fn();(a) &&= b",
1000 options: ["always", { enforceForIfStatements: true }],
1001 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1002 }, {
1003 code: "fn()\nif (a) a = b",
1004 output: "fn()\na &&= b",
1005 options: ["always", { enforceForIfStatements: true }],
1006 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1007 }, {
1008 code: "id\nif (a) (a) = b",
1009 output: null,
1010 options: ["always", { enforceForIfStatements: true }],
1011 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1012 }, {
1013 code: "object.prop\nif (a) (a) = b",
1014 output: null,
1015 options: ["always", { enforceForIfStatements: true }],
1016 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1017 }, {
1018 code: "object[computed]\nif (a) (a) = b",
1019 output: null,
1020 options: ["always", { enforceForIfStatements: true }],
1021 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1022 }, {
1023 code: "fn()\nif (a) (a) = b",
1024 output: null,
1025 options: ["always", { enforceForIfStatements: true }],
1026 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1027 },
1028
1029 // > Adding semicolon
1030 {
1031 code: "if (a) a = b; fn();",
1032 output: "a &&= b; fn();",
1033 options: ["always", { enforceForIfStatements: true }],
1034 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1035 }, {
1036 code: "if (a) { a = b }",
1037 output: "a &&= b;",
1038 options: ["always", { enforceForIfStatements: true }],
1039 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1040 }, {
1041 code: "if (a) { a = b; }\nfn();",
1042 output: "a &&= b;\nfn();",
1043 options: ["always", { enforceForIfStatements: true }],
1044 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1045 }, {
1046 code: "if (a) { a = b }\nfn();",
1047 output: "a &&= b;\nfn();",
1048 options: ["always", { enforceForIfStatements: true }],
1049 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1050 }, {
1051 code: "if (a) { a = b } fn();",
1052 output: "a &&= b; fn();",
1053 options: ["always", { enforceForIfStatements: true }],
1054 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1055 }, {
1056 code: "if (a) { a = b\n} fn();",
1057 output: "a &&= b; fn();",
1058 options: ["always", { enforceForIfStatements: true }],
1059 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1060 },
1061
1062 // > Spacing
1063 {
1064 code: "if (a) a = b",
1065 output: "a &&= b",
1066 options: ["always", { enforceForIfStatements: true }],
1067 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1068 }, {
1069 code: "if (a)\n a = b",
1070 output: "a &&= b",
1071 options: ["always", { enforceForIfStatements: true }],
1072 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1073 }, {
1074 code: "if (a) {\n a = b; \n}",
1075 output: "a &&= b;",
1076 options: ["always", { enforceForIfStatements: true }],
1077 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1078 },
1079
1080 // > Comments
1081 {
1082 code: "/* before */ if (a) a = b",
1083 output: "/* before */ a &&= b",
1084 options: ["always", { enforceForIfStatements: true }],
1085 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1086 }, {
1087 code: "if (a) a = b /* after */",
1088 output: "a &&= b /* after */",
1089 options: ["always", { enforceForIfStatements: true }],
1090 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1091 }, {
1092 code: "if (a) /* between */ a = b",
1093 output: null,
1094 options: ["always", { enforceForIfStatements: true }],
1095 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1096 }, {
1097 code: "if (a) a = /* between */ b",
1098 output: null,
1099 options: ["always", { enforceForIfStatements: true }],
1100 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1101 },
1102
1103 // > Members > Single Property Access
1104 {
1105 code: "if (a.b) a.b = c",
1106 output: "a.b &&= c",
1107 options: ["always", { enforceForIfStatements: true }],
1108 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" }, suggestions: [] }]
1109 }, {
1110 code: "if (a[b]) a[b] = c",
1111 output: "a[b] &&= c",
1112 options: ["always", { enforceForIfStatements: true }],
1113 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" }, suggestions: [] }]
1114 }, {
1115 code: "if (a['b']) a['b'] = c",
1116 output: "a['b'] &&= c",
1117 options: ["always", { enforceForIfStatements: true }],
1118 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" }, suggestions: [] }]
1119 }, {
1120 code: "if (this.prop) this.prop = value",
1121 output: "this.prop &&= value",
1122 options: ["always", { enforceForIfStatements: true }],
1123 errors: [{ messageId: "if", type: "IfStatement", suggestions: [] }]
1124 }, {
1125 code: "(class extends SuperClass { method() { if (super.prop) super.prop = value } })",
1126 output: "(class extends SuperClass { method() { super.prop &&= value } })",
1127 options: ["always", { enforceForIfStatements: true }],
1128 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" }, suggestions: [] }]
1129 }, {
1130 code: "with (object) if (a) a = b",
1131 output: "with (object) a &&= b",
1132 options: ["always", { enforceForIfStatements: true }],
1133 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" }, suggestions: [] }]
1134 },
1135
1136 // > Members > Possible Multiple Property Accesses
1137 {
1138 code: "if (a.b === undefined || a.b === null) a.b = c",
1139 output: null,
1140 options: ["always", { enforceForIfStatements: true }],
1141 errors: [{
1142 messageId: "if",
1143 type: "IfStatement",
1144 data: { operator: "??=" },
1145 suggestions: [{
1146 messageId: "convertIf",
1147 data: { operator: "??=" },
1148 output: "a.b ??= c"
1149 }]
1150 }]
1151 }, {
1152 code: "if (a.b.c) a.b.c = d",
1153 output: null,
1154 options: ["always", { enforceForIfStatements: true }],
1155 errors: [{
1156 messageId: "if",
1157 type: "IfStatement",
1158 data: { operator: "&&=" },
1159 suggestions: [{
1160 messageId: "convertIf",
1161 data: { operator: "&&=" },
1162 output: "a.b.c &&= d"
1163 }]
1164 }]
1165 }, {
1166 code: "if (a.b.c.d) a.b.c.d = e",
1167 output: null,
1168 options: ["always", { enforceForIfStatements: true }],
1169 errors: [{
1170 messageId: "if",
1171 type: "IfStatement",
1172 data: { operator: "&&=" },
1173 suggestions: [{
1174 messageId: "convertIf",
1175 data: { operator: "&&=" },
1176 output: "a.b.c.d &&= e"
1177 }]
1178 }]
1179 }, {
1180 code: "if (a[b].c) a[b].c = d",
1181 output: null,
1182 options: ["always", { enforceForIfStatements: true }],
1183 errors: [{
1184 messageId: "if",
1185 type: "IfStatement",
1186 data: { operator: "&&=" },
1187 suggestions: [{
1188 messageId: "convertIf",
1189 data: { operator: "&&=" },
1190 output: "a[b].c &&= d"
1191 }]
1192 }]
1193 }, {
1194 code: "with (object) if (a.b) a.b = c",
1195 output: null,
1196 options: ["always", { enforceForIfStatements: true }],
1197 errors: [{
1198 messageId: "if",
1199 type: "IfStatement",
1200 data: { operator: "&&=" },
1201 suggestions: [{
1202 messageId: "convertIf",
1203 data: { operator: "&&=" },
1204 output: "with (object) a.b &&= c"
1205 }]
1206 }]
1207 },
1208
1209 // > Else if
1210 {
1211 code: "if (unrelated) {} else if (a) a = b;",
1212 output: "if (unrelated) {} else a &&= b;",
1213 options: ["always", { enforceForIfStatements: true }],
1214 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1215 }, {
1216 code: "if (a) {} else if (b) {} else if (a) a = b;",
1217 output: "if (a) {} else if (b) {} else a &&= b;",
1218 options: ["always", { enforceForIfStatements: true }],
1219 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1220 }, {
1221 code: "if (unrelated) {} else\nif (a) a = b;",
1222 output: "if (unrelated) {} else\na &&= b;",
1223 options: ["always", { enforceForIfStatements: true }],
1224 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1225 }, {
1226 code: "if (unrelated) {\n}\nelse if (a) {\na = b;\n}",
1227 output: "if (unrelated) {\n}\nelse a &&= b;",
1228 options: ["always", { enforceForIfStatements: true }],
1229 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1230 }, {
1231 code: "if (unrelated) statement; else if (a) a = b;",
1232 output: "if (unrelated) statement; else a &&= b;",
1233 options: ["always", { enforceForIfStatements: true }],
1234 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1235 }, {
1236 code: "if (unrelated) id\nelse if (a) (a) = b",
1237 output: null,
1238 options: ["always", { enforceForIfStatements: true }],
1239 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1240 }, {
1241 code: "if (unrelated) {} else if (a) a = b; else if (c) c = d",
1242 output: "if (unrelated) {} else if (a) a = b; else c &&= d",
1243 options: ["always", { enforceForIfStatements: true }],
1244 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1245 },
1246
1247 // > Else if > Comments
1248 {
1249 code: "if (unrelated) { /* body */ } else if (a) a = b;",
1250 output: "if (unrelated) { /* body */ } else a &&= b;",
1251 options: ["always", { enforceForIfStatements: true }],
1252 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1253 }, {
1254 code: "if (unrelated) {} /* before else */ else if (a) a = b;",
1255 output: "if (unrelated) {} /* before else */ else a &&= b;",
1256 options: ["always", { enforceForIfStatements: true }],
1257 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1258 }, {
1259 code: "if (unrelated) {} else // Line\nif (a) a = b;",
1260 output: "if (unrelated) {} else // Line\na &&= b;",
1261 options: ["always", { enforceForIfStatements: true }],
1262 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1263 }, {
1264 code: "if (unrelated) {} else /* Block */ if (a) a = b;",
1265 output: "if (unrelated) {} else /* Block */ a &&= b;",
1266 options: ["always", { enforceForIfStatements: true }],
1267 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1268 },
1269
1270 // > Patterns
1271 {
1272 code: "if (array) array = array.filter(predicate)",
1273 output: "array &&= array.filter(predicate)",
1274 options: ["always", { enforceForIfStatements: true }],
1275 errors: [{ messageId: "if", type: "IfStatement", data: { operator: "&&=" } }]
1276 },
1277
1278 // Never
1279 {
1280 code: "a ||= b",
1281 output: "a = a || b",
1282 options: ["never"],
1283 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1284 }, {
1285 code: "a &&= b",
1286 output: "a = a && b",
1287 options: ["never"],
1288 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "&&=" } }]
1289 }, {
1290 code: "a ??= b",
1291 output: "a = a ?? b",
1292 options: ["never"],
1293 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "??=" } }]
1294 }, {
1295 code: "foo ||= bar",
1296 output: "foo = foo || bar",
1297 options: ["never"],
1298 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1299 },
1300
1301 // > Suggestions
1302 {
1303 code: "a.b ||= c",
1304 output: null,
1305 options: ["never"],
1306 errors: [{
1307 messageId: "unexpected",
1308 type: "AssignmentExpression",
1309 data: { operator: "||=" },
1310 suggestions: [{
1311 messageId: "separate",
1312 output: "a.b = a.b || c"
1313 }]
1314 }]
1315 }, {
1316 code: "a[b] ||= c",
1317 output: null,
1318 options: ["never"],
1319 errors: [{
1320 messageId: "unexpected",
1321 type: "AssignmentExpression",
1322 data: { operator: "||=" },
1323 suggestions: [{
1324 messageId: "separate",
1325 output: "a[b] = a[b] || c"
1326 }]
1327 }]
1328 }, {
1329 code: "a['b'] ||= c",
1330 output: null,
1331 options: ["never"],
1332 errors: [{
1333 messageId: "unexpected",
1334 type: "AssignmentExpression",
1335 data: { operator: "||=" },
1336 suggestions: [{
1337 messageId: "separate",
1338 output: "a['b'] = a['b'] || c"
1339 }]
1340 }]
1341 }, {
1342 code: "this.prop ||= 0",
1343 output: null,
1344 options: ["never"],
1345 errors: [{
1346 messageId: "unexpected",
1347 type: "AssignmentExpression",
1348 data: { operator: "||=" },
1349 suggestions: [{
1350 messageId: "separate",
1351 output: "this.prop = this.prop || 0"
1352 }]
1353 }]
1354 }, {
1355 code: "with (object) a ||= b",
1356 output: null,
1357 options: ["never"],
1358 errors: [{
1359 messageId: "unexpected",
1360 type: "AssignmentExpression",
1361 data: { operator: "||=" },
1362 suggestions: [{
1363 messageId: "separate",
1364 output: "with (object) a = a || b"
1365 }]
1366 }]
1367 },
1368
1369 // > Parenthesis
1370 {
1371 code: "(a) ||= b",
1372 output: "(a) = a || b",
1373 options: ["never"],
1374 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1375 }, {
1376 code: "a ||= (b)",
1377 output: "a = a || (b)",
1378 options: ["never"],
1379 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1380 }, {
1381 code: "(a ||= b)",
1382 output: "(a = a || b)",
1383 options: ["never"],
1384 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1385 },
1386
1387 // > Comments
1388 {
1389 code: "/* before */ a ||= b",
1390 output: "/* before */ a = a || b",
1391 options: ["never"],
1392 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1393 }, {
1394 code: "a ||= b // after",
1395 output: "a = a || b // after",
1396 options: ["never"],
1397 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1398 }, {
1399 code: "a /* before */ ||= b",
1400 output: null,
1401 options: ["never"],
1402 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1403 }, {
1404 code: "a ||= /* after */ b",
1405 output: null,
1406 options: ["never"],
1407 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1408 },
1409
1410 // > Precedence
1411 {
1412 code: "a ||= b && c",
1413 output: "a = a || b && c",
1414 options: ["never"],
1415 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1416 }, {
1417 code: "a &&= b || c",
1418 output: "a = a && (b || c)",
1419 options: ["never"],
1420 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "&&=" } }]
1421 }, {
1422 code: "a ||= b || c",
1423 output: "a = a || (b || c)",
1424 options: ["never"],
1425 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "||=" } }]
1426 }, {
1427 code: "a &&= b && c",
1428 output: "a = a && (b && c)",
1429 options: ["never"],
1430 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "&&=" } }]
1431 },
1432
1433 // > Mixed
1434 {
1435 code: "a ??= b || c",
1436 output: "a = a ?? (b || c)",
1437 options: ["never"],
1438 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "??=" } }]
1439 }, {
1440 code: "a ??= b && c",
1441 output: "a = a ?? (b && c)",
1442 options: ["never"],
1443 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "??=" } }]
1444 }, {
1445 code: "a ??= b ?? c",
1446 output: "a = a ?? (b ?? c)",
1447 options: ["never"],
1448 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "??=" } }]
1449 }, {
1450 code: "a ??= (b || c)",
1451 output: "a = a ?? (b || c)",
1452 options: ["never"],
1453 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "??=" } }]
1454 }, {
1455 code: "a ??= b + c",
1456 output: "a = a ?? b + c",
1457 options: ["never"],
1458 errors: [{ messageId: "unexpected", type: "AssignmentExpression", data: { operator: "??=" } }]
1459 }]
1460 });