]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/logical-assignment-operators.js
2 * @fileoverview Tests for logical-assignment-operators.
3 * @author Daniel Martens
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const rule
= require("../../../lib/rules/logical-assignment-operators"),
12 { RuleTester
} = require("../../../lib/rule-tester");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 2021 } });
20 ruleTester
.run("logical-assignment-operators", rule
, {
29 "a === undefined ? a : b",
56 "object.a = object.b || c",
77 code
: "class Class { #prop; constructor() { this.#prop || (this.prop = value) } }",
78 parserOptions
: { ecmaVersion
: 2022 }
80 code
: "class Class { #prop; constructor() { this.prop || (this.#prop = value) } }",
81 parserOptions
: { ecmaVersion
: 2022 }
88 options
: ["always", { enforceForIfStatements
: false }]
90 code
: "if (a) { a = b } else {}",
91 options
: ["always", { enforceForIfStatements
: true }]
93 code
: "if (a) { a = b } else if (a) {}",
94 options
: ["always", { enforceForIfStatements
: true }]
96 code
: "if (unrelated) {} else if (a) a = b; else {}",
97 options
: ["always", { enforceForIfStatements
: true }]
99 code
: "if (unrelated) {} else if (a) a = b; else if (unrelated) {}",
100 options
: ["always", { enforceForIfStatements
: true }]
106 options
: ["always", { enforceForIfStatements
: true }]
108 code
: "if (a) { before; a = b }",
109 options
: ["always", { enforceForIfStatements
: true }]
111 code
: "if (a) { a = b; after }",
112 options
: ["always", { enforceForIfStatements
: true }]
114 code
: "if (a) throw new Error()",
115 options
: ["always", { enforceForIfStatements
: true }]
118 options
: ["always", { enforceForIfStatements
: true }]
120 code
: "if (a) a ||= b",
121 options
: ["always", { enforceForIfStatements
: true }]
123 code
: "if (a) b = a",
124 options
: ["always", { enforceForIfStatements
: true }]
126 code
: "if (a) { a() }",
127 options
: ["always", { enforceForIfStatements
: true }]
129 code
: "if (a) { a += a || b }",
130 options
: ["always", { enforceForIfStatements
: true }]
135 code
: "if (true) a = b",
136 options
: ["always", { enforceForIfStatements
: true }]
138 code
: "if (predicate(a)) a = b",
139 options
: ["always", { enforceForIfStatements
: true }]
141 code
: "if (a?.b) a.b = c",
142 options
: ["always", { enforceForIfStatements
: true }]
144 code
: "if (!a?.b) a.b = c",
145 options
: ["always", { enforceForIfStatements
: true }]
147 code
: "if (a === b) a = b",
148 options
: ["always", { enforceForIfStatements
: true }]
150 code
: "if (a === undefined) a = b",
151 options
: ["always", { enforceForIfStatements
: true }]
153 code
: "if (a === null) a = b",
154 options
: ["always", { enforceForIfStatements
: true }]
156 code
: "if (a != null) a = b",
157 options
: ["always", { enforceForIfStatements
: true }]
159 code
: "if (a === null && a === undefined) a = b",
160 options
: ["always", { enforceForIfStatements
: true }]
162 code
: "if (a === 0 || a === undefined) a = b",
163 options
: ["always", { enforceForIfStatements
: true }]
165 code
: "if (a === null || a === 1) a = b",
166 options
: ["always", { enforceForIfStatements
: true }]
168 code
: "if (a == null || a == undefined) a = b",
169 options
: ["always", { enforceForIfStatements
: true }]
171 code
: "if (a === null || a === !0) a = b",
172 options
: ["always", { enforceForIfStatements
: true }]
174 code
: "if (a === null || a === +0) a = b",
175 options
: ["always", { enforceForIfStatements
: true }]
177 code
: "if (a === null || a === null) a = b",
178 options
: ["always", { enforceForIfStatements
: true }]
180 code
: "if (a === undefined || a === void 0) a = b",
181 options
: ["always", { enforceForIfStatements
: true }]
183 code
: "if (a === null || a === void void 0) a = b",
184 options
: ["always", { enforceForIfStatements
: true }]
186 code
: "if (a === null || a === void 'string') a = b",
187 options
: ["always", { enforceForIfStatements
: true }]
189 code
: "if (a === null || a === void fn()) a = b",
190 options
: ["always", { enforceForIfStatements
: true }]
195 code
: "if (a == a) a = b",
196 options
: ["always", { enforceForIfStatements
: true }]
198 code
: "if (a == b) a = b",
199 options
: ["always", { enforceForIfStatements
: true }]
201 code
: "if (null == null) a = b",
202 options
: ["always", { enforceForIfStatements
: true }]
204 code
: "if (undefined == undefined) undefined = b",
205 options
: ["always", { enforceForIfStatements
: true }]
207 code
: "if (null == x) a = b",
208 options
: ["always", { enforceForIfStatements
: true }]
210 code
: "if (null == fn()) a = b",
211 options
: ["always", { enforceForIfStatements
: true }]
213 code
: "if (null === a || a === 0) a = b",
214 options
: ["always", { enforceForIfStatements
: true }]
216 code
: "if (0 === a || null === a) a = b",
217 options
: ["always", { enforceForIfStatements
: true }]
219 code
: "if (1 === a || a === undefined) a = b",
220 options
: ["always", { enforceForIfStatements
: true }]
222 code
: "if (undefined === a || 1 === a) a = b",
223 options
: ["always", { enforceForIfStatements
: true }]
225 code
: "if (a === null || a === b) a = b",
226 options
: ["always", { enforceForIfStatements
: true }]
228 code
: "if (b === undefined || a === null) a = b",
229 options
: ["always", { enforceForIfStatements
: true }]
231 code
: "if (null === a || b === a) a = b",
232 options
: ["always", { enforceForIfStatements
: true }]
234 code
: "if (null === null || undefined === undefined) a = b",
235 options
: ["always", { enforceForIfStatements
: true }]
237 code
: "if (null === null || a === a) a = b",
238 options
: ["always", { enforceForIfStatements
: true }]
240 code
: "if (undefined === undefined || a === a) a = b",
241 options
: ["always", { enforceForIfStatements
: true }]
243 code
: "if (null === undefined || a === a) a = b",
244 options
: ["always", { enforceForIfStatements
: true }]
247 // > Test > Undefined
251 " const undefined = 0;",
252 " if (a == undefined) a = b",
255 options
: ["always", { enforceForIfStatements
: true }]
259 " const undefined = 0;",
261 " if (a == undefined) a = b",
265 options
: ["always", { enforceForIfStatements
: true }]
269 " if (a == undefined) a = b",
273 options
: ["always", { enforceForIfStatements
: true }]
277 " const undefined = 0;",
278 " if (undefined == null) undefined = b",
281 options
: ["always", { enforceForIfStatements
: true }]
285 " const undefined = 0;",
286 " if (a === undefined || a === null) a = b",
289 options
: ["always", { enforceForIfStatements
: true }]
293 " const undefined = 0;",
294 " if (undefined === a || null === a) a = b",
297 options
: ["always", { enforceForIfStatements
: true }]
302 code
: "if (a) b = c",
303 options
: ["always", { enforceForIfStatements
: true }]
305 code
: "if (!a) b = c",
306 options
: ["always", { enforceForIfStatements
: true }]
308 code
: "if (!!a) b = c",
309 options
: ["always", { enforceForIfStatements
: true }]
311 code
: "if (a == null) b = c",
312 options
: ["always", { enforceForIfStatements
: true }]
314 code
: "if (a === null || a === undefined) b = c",
315 options
: ["always", { enforceForIfStatements
: true }]
317 code
: "if (a === null || b === undefined) a = b",
318 options
: ["always", { enforceForIfStatements
: true }]
320 code
: "if (a === null || b === undefined) b = c",
321 options
: ["always", { enforceForIfStatements
: true }]
323 code
: "if (Boolean(a)) b = c",
324 options
: ["always", { enforceForIfStatements
: true }]
327 "function fn(Boolean) {",
328 " if (Boolean(a)) a = b",
331 options
: ["always", { enforceForIfStatements
: true }]
354 code
: "a.b = a.b || c",
364 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
368 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "&&=" }, suggestions
: [] }]
372 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "??=" }, suggestions
: [] }]
374 code
: "foo = foo || bar",
375 output
: "foo ||= bar",
376 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
381 code
: "a = a || fn()",
382 output
: "a ||= fn()",
383 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
385 code
: "a = a || b && c",
386 output
: "a ||= b && c",
387 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
389 code
: "a = a || (b || c)",
390 output
: "a ||= (b || c)",
391 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
393 code
: "a = a || (b ? c : d)",
394 output
: "a ||= (b ? c : d)",
395 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
400 code
: "/* before */ a = a || b",
401 output
: "/* before */ a ||= b",
402 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
404 code
: "a = a || b // after",
405 output
: "a ||= b // after",
406 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
408 code
: "a /* between */ = a || b",
410 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
412 code
: "a = /** @type */ a || b",
414 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
416 code
: "a = a || /* between */ b",
418 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
423 code
: "(a) = a || b",
425 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
427 code
: "a = (a) || b",
429 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
431 code
: "a = a || (b)",
433 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
435 code
: "a = a || ((b))",
436 output
: "a ||= ((b))",
437 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
439 code
: "(a = a || b)",
441 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
443 code
: "a = a || (f(), b)",
444 output
: "a ||= (f(), b)",
445 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
450 code
: "a.b = a.b ?? c",
453 messageId
: "assignment",
454 type
: "AssignmentExpression",
455 data
: { operator
: "??=" },
457 messageId
: "useLogicalOperator",
458 data
: { operator
: "??=" },
463 code
: "a.b.c = a.b.c ?? d",
466 messageId
: "assignment",
467 type
: "AssignmentExpression",
468 data
: { operator
: "??=" },
470 messageId
: "useLogicalOperator",
471 data
: { operator
: "??=" },
472 output
: "a.b.c ??= d"
476 code
: "a[b] = a[b] ?? c",
479 messageId
: "assignment",
480 type
: "AssignmentExpression",
481 data
: { operator
: "??=" },
483 messageId
: "useLogicalOperator",
484 data
: { operator
: "??=" },
489 code
: "a['b'] = a['b'] ?? c",
492 messageId
: "assignment",
493 type
: "AssignmentExpression",
494 data
: { operator
: "??=" },
496 messageId
: "useLogicalOperator",
497 data
: { operator
: "??=" },
498 output
: "a['b'] ??= c"
502 code
: "a.b = a['b'] ?? c",
505 messageId
: "assignment",
506 type
: "AssignmentExpression",
507 data
: { operator
: "??=" },
509 messageId
: "useLogicalOperator",
510 data
: { operator
: "??=" },
515 code
: "a['b'] = a.b ?? c",
518 messageId
: "assignment",
519 type
: "AssignmentExpression",
520 data
: { operator
: "??=" },
522 messageId
: "useLogicalOperator",
523 data
: { operator
: "??=" },
524 output
: "a['b'] ??= c"
528 code
: "this.prop = this.prop ?? {}",
531 messageId
: "assignment",
532 type
: "AssignmentExpression",
533 data
: { operator
: "??=" },
535 messageId
: "useLogicalOperator",
536 data
: { operator
: "??=" },
537 output
: "this.prop ??= {}"
544 code
: "with (object) a = a || b",
547 messageId
: "assignment",
548 type
: "AssignmentExpression",
549 data
: { operator
: "||=" },
551 messageId
: "useLogicalOperator",
552 data
: { operator
: "||=" },
554 output
: "with (object) a ||= b"
558 code
: "with (object) { a = a || b }",
561 messageId
: "assignment",
562 type
: "AssignmentExpression",
563 data
: { operator
: "||=" },
565 messageId
: "useLogicalOperator",
566 data
: { operator
: "||=" },
567 output
: "with (object) { a ||= b }"
571 code
: "with (object) { if (condition) a = a || b }",
574 messageId
: "assignment",
575 type
: "AssignmentExpression",
576 data
: { operator
: "||=" },
578 messageId
: "useLogicalOperator",
579 data
: { operator
: "||=" },
580 output
: "with (object) { if (condition) a ||= b }"
584 code
: "with (a = a || b) {}",
585 output
: "with (a ||= b) {}",
586 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
588 code
: "with (object) {} a = a || b",
589 output
: "with (object) {} a ||= b",
590 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
592 code
: "a = a || b; with (object) {}",
593 output
: "a ||= b; with (object) {}",
594 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
596 code
: "if (condition) a = a || b",
597 output
: "if (condition) a ||= b",
598 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
608 messageId
: "assignment",
609 type
: "AssignmentExpression",
610 data
: { operator
: "||=" },
612 messageId
: "useLogicalOperator",
613 data
: { operator
: "||=" },
626 code
: "fn(a = a || b)",
627 output
: "fn(a ||= b)",
628 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
630 code
: "fn((a = a || b))",
631 output
: "fn((a ||= b))",
632 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
634 code
: "(a = a || b) ? c : d",
635 output
: "(a ||= b) ? c : d",
636 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
638 code
: "a = b = b || c",
639 output
: "a = b ||= c",
640 errors
: [{ messageId
: "assignment", type
: "AssignmentExpression", data
: { operator
: "||=" }, suggestions
: [] }]
645 code
: "a || (a = b)",
647 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
649 code
: "a && (a = b)",
651 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "&&=" } }]
653 code
: "a ?? (a = b)",
655 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "??=" } }]
657 code
: "foo ?? (foo = bar)",
658 output
: "foo ??= bar",
659 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "??=" } }]
664 code
: "a || (a = 0)",
666 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
668 code
: "a || (a = fn())",
669 output
: "a ||= fn()",
670 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
672 code
: "a || (a = (b || c))",
673 output
: "a ||= (b || c)",
674 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
679 code
: "(a) || (a = b)",
681 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
683 code
: "a || ((a) = b)",
685 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
687 code
: "a || (a = (b))",
689 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
691 code
: "a || ((a = b))",
693 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
695 code
: "a || (((a = b)))",
697 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
699 code
: "a || ( ( a = b ) )",
701 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
706 code
: "/* before */ a || (a = b)",
707 output
: "/* before */ a ||= b",
708 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
710 code
: "a || (a = b) // after",
711 output
: "a ||= b // after",
712 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
714 code
: "a /* between */ || (a = b)",
716 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
718 code
: "a || /* between */ (a = b)",
720 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
725 code
: "a.b || (a.b = c)",
727 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
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
: "||=" } }]
734 code
: "a['b'] || (a['b'] = c)",
735 output
: "a['b'] ||= c",
736 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
738 code
: "a[0] || (a[0] = b)",
739 output
: "a[0] ||= b",
740 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
742 code
: "a[this] || (a[this] = b)",
743 output
: "a[this] ||= b",
744 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
746 code
: "foo.bar || (foo.bar = baz)",
747 output
: "foo.bar ||= baz",
748 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
750 code
: "a.b.c || (a.b.c = d)",
753 messageId
: "logical",
754 type
: "LogicalExpression",
755 data
: { operator
: "||=" },
757 messageId
: "convertLogical",
758 data
: { operator
: "||=" },
759 output
: "a.b.c ||= d"
763 code
: "a[b.c] || (a[b.c] = d)",
766 messageId
: "logical",
767 type
: "LogicalExpression",
768 data
: { operator
: "||=" },
770 messageId
: "convertLogical",
771 data
: { operator
: "||=" },
772 output
: "a[b.c] ||= d"
776 code
: "a[b?.c] || (a[b?.c] = d)",
779 messageId
: "logical",
780 type
: "LogicalExpression",
781 data
: { operator
: "||=" },
783 messageId
: "convertLogical",
784 data
: { operator
: "||=" },
785 output
: "a[b?.c] ||= d"
789 code
: "with (object) a.b || (a.b = c)",
792 messageId
: "logical",
793 type
: "LogicalExpression",
794 data
: { operator
: "||=" },
796 messageId
: "convertLogical",
797 data
: { operator
: "||=" },
798 output
: "with (object) a.b ||= c"
805 code
: "a = a.b || (a.b = {})",
806 output
: "a = a.b ||= {}",
807 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" }, suggestions
: [] }]
810 code
: "a || (a = 0) || b",
811 output
: "(a ||= 0) || b",
812 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
814 code
: "(a || (a = 0)) || b",
815 output
: "(a ||= 0) || b",
816 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
818 code
: "a || (b || (b = 0))",
819 output
: "a || (b ||= 0)",
820 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
822 code
: "a = b || (b = c)",
823 output
: "a = b ||= c",
824 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
826 code
: "a || (a = 0) ? b : c",
827 output
: "(a ||= 0) ? b : c",
828 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
830 code
: "fn(a || (a = 0))",
831 output
: "fn(a ||= 0)",
832 errors
: [{ messageId
: "logical", type
: "LogicalExpression", data
: { operator
: "||=" } }]
837 code
: "if (a) a = b",
839 options
: ["always", { enforceForIfStatements
: true }],
840 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
842 code
: "if (Boolean(a)) a = b",
844 options
: ["always", { enforceForIfStatements
: true }],
845 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
847 code
: "if (!!a) a = b",
849 options
: ["always", { enforceForIfStatements
: true }],
850 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
852 code
: "if (!a) a = b",
854 options
: ["always", { enforceForIfStatements
: true }],
855 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "||=" } }]
857 code
: "if (!Boolean(a)) a = b",
859 options
: ["always", { enforceForIfStatements
: true }],
860 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "||=" } }]
862 code
: "if (a == undefined) a = b",
864 options
: ["always", { enforceForIfStatements
: true }],
865 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
867 code
: "if (a == null) a = b",
869 options
: ["always", { enforceForIfStatements
: true }],
870 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
872 code
: "if (a === null || a === undefined) a = b",
874 options
: ["always", { enforceForIfStatements
: true }],
875 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
877 code
: "if (a === undefined || a === null) a = b",
879 options
: ["always", { enforceForIfStatements
: true }],
880 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
882 code
: "if (a === null || a === void 0) a = b",
884 options
: ["always", { enforceForIfStatements
: true }],
885 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
887 code
: "if (a === void 0 || a === null) a = b",
889 options
: ["always", { enforceForIfStatements
: true }],
890 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
892 code
: "if (a) { a = b; }",
894 options
: ["always", { enforceForIfStatements
: true }],
895 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
898 "{ const undefined = 0; }",
899 "if (a == undefined) a = b"
902 "{ const undefined = 0; }",
905 options
: ["always", { enforceForIfStatements
: true }],
906 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
909 "if (a == undefined) a = b",
910 "{ const undefined = 0; }"
914 "{ const undefined = 0; }"
916 options
: ["always", { enforceForIfStatements
: true }],
917 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" } }]
922 code
: "if (null == a) a = b",
924 options
: ["always", { enforceForIfStatements
: true }],
925 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
927 code
: "if (undefined == a) a = b",
929 options
: ["always", { enforceForIfStatements
: true }],
930 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
932 code
: "if (undefined === a || a === null) a = b",
934 options
: ["always", { enforceForIfStatements
: true }],
935 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
937 code
: "if (a === undefined || null === a) a = b",
939 options
: ["always", { enforceForIfStatements
: true }],
940 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
942 code
: "if (undefined === a || null === a) a = b",
944 options
: ["always", { enforceForIfStatements
: true }],
945 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
947 code
: "if (null === a || a === undefined) a = b",
949 options
: ["always", { enforceForIfStatements
: true }],
950 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
952 code
: "if (a === null || undefined === a) a = b",
954 options
: ["always", { enforceForIfStatements
: true }],
955 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
957 code
: "if (null === a || undefined === a) a = b",
959 options
: ["always", { enforceForIfStatements
: true }],
960 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "??=" }, suggestions
: [] }]
965 code
: "if ((a)) a = b",
967 options
: ["always", { enforceForIfStatements
: true }],
968 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
970 code
: "if (a) (a) = b",
972 options
: ["always", { enforceForIfStatements
: true }],
973 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
975 code
: "if (a) a = (b)",
977 options
: ["always", { enforceForIfStatements
: true }],
978 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
980 code
: "if (a) (a = b)",
982 options
: ["always", { enforceForIfStatements
: true }],
983 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
986 // > Previous statement
988 code
: ";if (a) (a) = b",
989 output
: ";(a) &&= b",
990 options
: ["always", { enforceForIfStatements
: true }],
991 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
993 code
: "{ if (a) (a) = b }",
994 output
: "{ (a) &&= b }",
995 options
: ["always", { enforceForIfStatements
: true }],
996 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
1008 code
: "id\nif (a) (a) = b",
1010 options
: ["always", { enforceForIfStatements
: true }],
1011 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1013 code
: "object.prop\nif (a) (a) = b",
1015 options
: ["always", { enforceForIfStatements
: true }],
1016 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1018 code
: "object[computed]\nif (a) (a) = b",
1020 options
: ["always", { enforceForIfStatements
: true }],
1021 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1023 code
: "fn()\nif (a) (a) = b",
1025 options
: ["always", { enforceForIfStatements
: true }],
1026 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1029 // > Adding semicolon
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
: "&&=" } }]
1036 code
: "if (a) { a = b }",
1038 options
: ["always", { enforceForIfStatements
: true }],
1039 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
1064 code
: "if (a) a = b",
1066 options
: ["always", { enforceForIfStatements
: true }],
1067 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1069 code
: "if (a)\n a = b",
1071 options
: ["always", { enforceForIfStatements
: true }],
1072 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1074 code
: "if (a) {\n a = b; \n}",
1076 options
: ["always", { enforceForIfStatements
: true }],
1077 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
1092 code
: "if (a) /* between */ a = b",
1094 options
: ["always", { enforceForIfStatements
: true }],
1095 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1097 code
: "if (a) a = /* between */ b",
1099 options
: ["always", { enforceForIfStatements
: true }],
1100 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
1103 // > Members > Single Property Access
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
: [] }]
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
: [] }]
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
: [] }]
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
: [] }]
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
: [] }]
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
: [] }]
1136 // > Members > Possible Multiple Property Accesses
1138 code
: "if (a.b === undefined || a.b === null) a.b = c",
1140 options
: ["always", { enforceForIfStatements
: true }],
1143 type
: "IfStatement",
1144 data
: { operator
: "??=" },
1146 messageId
: "convertIf",
1147 data
: { operator
: "??=" },
1152 code
: "if (a.b.c) a.b.c = d",
1154 options
: ["always", { enforceForIfStatements
: true }],
1157 type
: "IfStatement",
1158 data
: { operator
: "&&=" },
1160 messageId
: "convertIf",
1161 data
: { operator
: "&&=" },
1162 output
: "a.b.c &&= d"
1166 code
: "if (a.b.c.d) a.b.c.d = e",
1168 options
: ["always", { enforceForIfStatements
: true }],
1171 type
: "IfStatement",
1172 data
: { operator
: "&&=" },
1174 messageId
: "convertIf",
1175 data
: { operator
: "&&=" },
1176 output
: "a.b.c.d &&= e"
1180 code
: "if (a[b].c) a[b].c = d",
1182 options
: ["always", { enforceForIfStatements
: true }],
1185 type
: "IfStatement",
1186 data
: { operator
: "&&=" },
1188 messageId
: "convertIf",
1189 data
: { operator
: "&&=" },
1190 output
: "a[b].c &&= d"
1194 code
: "with (object) if (a.b) a.b = c",
1196 options
: ["always", { enforceForIfStatements
: true }],
1199 type
: "IfStatement",
1200 data
: { operator
: "&&=" },
1202 messageId
: "convertIf",
1203 data
: { operator
: "&&=" },
1204 output
: "with (object) a.b &&= c"
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
1236 code
: "if (unrelated) id\nelse if (a) (a) = b",
1238 options
: ["always", { enforceForIfStatements
: true }],
1239 errors
: [{ messageId
: "if", type
: "IfStatement", data
: { operator
: "&&=" } }]
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
: "&&=" } }]
1247 // > Else if > Comments
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
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
: "&&=" } }]
1281 output
: "a = a || b",
1283 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1286 output
: "a = a && b",
1288 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "&&=" } }]
1291 output
: "a = a ?? b",
1293 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "??=" } }]
1295 code
: "foo ||= bar",
1296 output
: "foo = foo || bar",
1298 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1307 messageId
: "unexpected",
1308 type
: "AssignmentExpression",
1309 data
: { operator
: "||=" },
1311 messageId
: "separate",
1312 output
: "a.b = a.b || c"
1320 messageId
: "unexpected",
1321 type
: "AssignmentExpression",
1322 data
: { operator
: "||=" },
1324 messageId
: "separate",
1325 output
: "a[b] = a[b] || c"
1329 code
: "a['b'] ||= c",
1333 messageId
: "unexpected",
1334 type
: "AssignmentExpression",
1335 data
: { operator
: "||=" },
1337 messageId
: "separate",
1338 output
: "a['b'] = a['b'] || c"
1342 code
: "this.prop ||= 0",
1346 messageId
: "unexpected",
1347 type
: "AssignmentExpression",
1348 data
: { operator
: "||=" },
1350 messageId
: "separate",
1351 output
: "this.prop = this.prop || 0"
1355 code
: "with (object) a ||= b",
1359 messageId
: "unexpected",
1360 type
: "AssignmentExpression",
1361 data
: { operator
: "||=" },
1363 messageId
: "separate",
1364 output
: "with (object) a = a || b"
1372 output
: "(a) = a || b",
1374 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1377 output
: "a = a || (b)",
1379 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1382 output
: "(a = a || b)",
1384 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1389 code
: "/* before */ a ||= b",
1390 output
: "/* before */ a = a || b",
1392 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1394 code
: "a ||= b // after",
1395 output
: "a = a || b // after",
1397 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1399 code
: "a /* before */ ||= b",
1402 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1404 code
: "a ||= /* after */ b",
1407 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1412 code
: "a ||= b && c",
1413 output
: "a = a || b && c",
1415 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1417 code
: "a &&= b || c",
1418 output
: "a = a && (b || c)",
1420 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "&&=" } }]
1422 code
: "a ||= b || c",
1423 output
: "a = a || (b || c)",
1425 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "||=" } }]
1427 code
: "a &&= b && c",
1428 output
: "a = a && (b && c)",
1430 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "&&=" } }]
1435 code
: "a ??= b || c",
1436 output
: "a = a ?? (b || c)",
1438 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "??=" } }]
1440 code
: "a ??= b && c",
1441 output
: "a = a ?? (b && c)",
1443 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "??=" } }]
1445 code
: "a ??= b ?? c",
1446 output
: "a = a ?? (b ?? c)",
1448 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "??=" } }]
1450 code
: "a ??= (b || c)",
1451 output
: "a = a ?? (b || c)",
1453 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "??=" } }]
1455 code
: "a ??= b + c",
1456 output
: "a = a ?? b + c",
1458 errors
: [{ messageId
: "unexpected", type
: "AssignmentExpression", data
: { operator
: "??=" } }]