]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/yoda.js
upgrade to v7.0.0
[pve-eslint.git] / eslint / tests / lib / rules / yoda.js
1 /**
2 * @fileoverview Tests for yoda rule.
3 * @author Raphael Pigulla
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11 const rule = require("../../../lib/rules/yoda"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester();
19
20 ruleTester.run("yoda", rule, {
21 valid: [
22
23 // "never" mode
24 { code: 'if (value === "red") {}', options: ["never"] },
25 { code: "if (value === value) {}", options: ["never"] },
26 { code: "if (value != 5) {}", options: ["never"] },
27 { code: "if (5 & foo) {}", options: ["never"] },
28 { code: "if (5 === 4) {}", options: ["never"] },
29 {
30 code: "if (value === `red`) {}",
31 options: ["never"],
32 parserOptions: { ecmaVersion: 2015 }
33 },
34 {
35 code: "if (`red` === `red`) {}",
36 options: ["never"],
37 parserOptions: { ecmaVersion: 2015 }
38 },
39 {
40 code: "if (`${foo}` === `red`) {}",
41 options: ["never"],
42 parserOptions: { ecmaVersion: 2015 }
43 },
44 {
45 code: 'if (`${""}` === `red`) {}',
46 options: ["never"],
47 parserOptions: { ecmaVersion: 2015 }
48 },
49 {
50 code: 'if (`${"red"}` === foo) {}',
51 options: ["never"],
52 parserOptions: { ecmaVersion: 2015 }
53 },
54 {
55 code: "if (b > `a` && b > `a`) {}",
56 options: ["never"],
57 parserOptions: { ecmaVersion: 2015 }
58 },
59 {
60 code: 'if (`b` > `a` && "b" > "a") {}',
61 options: ["never"],
62 parserOptions: { ecmaVersion: 2015 }
63 },
64
65 // "always" mode
66 { code: 'if ("blue" === value) {}', options: ["always"] },
67 { code: "if (value === value) {}", options: ["always"] },
68 { code: "if (4 != value) {}", options: ["always"] },
69 { code: "if (foo & 4) {}", options: ["always"] },
70 { code: "if (5 === 4) {}", options: ["always"] },
71 {
72 code: "if (`red` === value) {}",
73 options: ["always"],
74 parserOptions: { ecmaVersion: 2015 }
75 },
76 {
77 code: "if (`red` === `red`) {}",
78 options: ["always"],
79 parserOptions: { ecmaVersion: 2015 }
80 },
81 {
82 code: "if (`red` === `${foo}`) {}",
83 options: ["always"],
84 parserOptions: { ecmaVersion: 2015 }
85 },
86 {
87 code: 'if (`red` === `${""}`) {}',
88 options: ["always"],
89 parserOptions: { ecmaVersion: 2015 }
90 },
91 {
92 code: 'if (foo === `${"red"}`) {}',
93 options: ["always"],
94 parserOptions: { ecmaVersion: 2015 }
95 },
96 {
97 code: "if (`a` > b && `a` > b) {}",
98 options: ["always"],
99 parserOptions: { ecmaVersion: 2015 }
100 },
101 {
102 code: 'if (`b` > `a` && "b" > "a") {}',
103 options: ["always"],
104 parserOptions: { ecmaVersion: 2015 }
105 },
106
107 // Range exception
108 {
109 code: 'if ("a" < x && x < MAX ) {}',
110 options: ["never", { exceptRange: true }]
111 },
112 {
113 code: "if (1 < x && x < MAX ) {}",
114 options: ["never", { exceptRange: true }]
115 },
116 {
117 code: "if ('a' < x && x < MAX ) {}",
118 options: ["never", { exceptRange: true }]
119 },
120 {
121 code: "if (x < `x` || `x` <= x) {}",
122 options: ["never", { exceptRange: true }],
123 parserOptions: { ecmaVersion: 2015 }
124 },
125 {
126 code: "if (0 < x && x <= 1) {}",
127 options: ["never", { exceptRange: true }]
128 },
129 {
130 code: "if (0 <= x && x < 1) {}",
131 options: ["always", { exceptRange: true }]
132 },
133 {
134 code: "if ('blue' < x.y && x.y < 'green') {}",
135 options: ["never", { exceptRange: true }]
136 },
137 {
138 code: "if (0 < x[``] && x[``] < 100) {}",
139 options: ["never", { exceptRange: true }],
140 parserOptions: { ecmaVersion: 2015 }
141 },
142 {
143 code: "if (0 < x[''] && x[``] < 100) {}",
144 options: ["never", { exceptRange: true }],
145 parserOptions: { ecmaVersion: 2015 }
146 },
147 {
148 code:
149 "if (a < 4 || (b[c[0]].d['e'] < 0 || 1 <= b[c[0]].d['e'])) {}",
150 options: ["never", { exceptRange: true }]
151 },
152 {
153 code: "if (0 <= x['y'] && x['y'] <= 100) {}",
154 options: ["never", { exceptRange: true }]
155 },
156 {
157 code: "if (a < 0 && (0 < b && b < 1)) {}",
158 options: ["never", { exceptRange: true }]
159 },
160 {
161 code: "if ((0 < a && a < 1) && b < 0) {}",
162 options: ["never", { exceptRange: true }]
163 },
164 {
165 code: "if (-1 < x && x < 0) {}",
166 options: ["never", { exceptRange: true }]
167 },
168 {
169 code: "if (0 <= this.prop && this.prop <= 1) {}",
170 options: ["never", { exceptRange: true }]
171 },
172 {
173 code: "if (0 <= index && index < list.length) {}",
174 options: ["never", { exceptRange: true }]
175 },
176 {
177 code: "if (ZERO <= index && index < 100) {}",
178 options: ["never", { exceptRange: true }]
179 },
180 {
181 code: "if (value <= MIN || 10 < value) {}",
182 options: ["never", { exceptRange: true }]
183 },
184 {
185 code: "if (value <= 0 || MAX < value) {}",
186 options: ["never", { exceptRange: true }]
187 },
188 {
189 code: 'if (0 <= a.b && a["b"] <= 100) {}',
190 options: ["never", { exceptRange: true }]
191 },
192 {
193 code: "if (0 <= a.b && a[`b`] <= 100) {}",
194 options: ["never", { exceptRange: true }],
195 parserOptions: { ecmaVersion: 2015 }
196 },
197 {
198 code: "if (-1n < x && x <= 1n) {}",
199 options: ["never", { exceptRange: true }],
200 parserOptions: { ecmaVersion: 2020 }
201 },
202 {
203 code: "if (-1n <= x && x < 1n) {}",
204 options: ["always", { exceptRange: true }],
205 parserOptions: { ecmaVersion: 2020 }
206 },
207 {
208 code: "if (x < `1` || `1` < x) {}",
209 options: ["always", { exceptRange: true }],
210 parserOptions: { ecmaVersion: 2020 }
211 },
212 {
213 code: "if (1 <= a['/(?<zero>0)/'] && a[/(?<zero>0)/] <= 100) {}",
214 options: ["never", { exceptRange: true }],
215 parserOptions: { ecmaVersion: 2018 }
216 },
217 {
218 code: "if (x <= `bar` || `foo` < x) {}",
219 options: ["always", { exceptRange: true }],
220 parserOptions: { ecmaVersion: 2015 }
221 },
222 {
223 code: "if ('a' < x && x < MAX ) {}",
224 options: ["always", { exceptRange: true }],
225 parserOptions: { ecmaVersion: 2015 }
226 },
227 {
228 code: "if ('a' < x && x < MAX ) {}",
229 options: ["always"],
230 parserOptions: { ecmaVersion: 2015 }
231 },
232 {
233 code: "if (MIN < x && x < 'a' ) {}",
234 options: ["never", { exceptRange: true }],
235 parserOptions: { ecmaVersion: 2015 }
236 },
237 {
238 code: "if (MIN < x && x < 'a' ) {}",
239 options: ["never"],
240 parserOptions: { ecmaVersion: 2015 }
241 },
242 {
243 code: "if (`blue` < x.y && x.y < `green`) {}",
244 options: ["never", { exceptRange: true }],
245 parserOptions: { ecmaVersion: 2015 }
246 },
247 {
248 code: "if (0 <= x[`y`] && x[`y`] <= 100) {}",
249 options: ["never", { exceptRange: true }],
250 parserOptions: { ecmaVersion: 2015 }
251 },
252 {
253 code: 'if (0 <= x[`y`] && x["y"] <= 100) {}',
254 options: ["never", { exceptRange: true }],
255 parserOptions: { ecmaVersion: 2015 }
256 },
257 {
258 code: "if ('a' <= x && x < 'b') {}",
259 options: ["never", { exceptRange: true }]
260 },
261 {
262 code: "if (x < -1n || 1n <= x) {}",
263 options: ["never", { exceptRange: true }],
264 parserOptions: { ecmaVersion: 2020 }
265 },
266 {
267 code: "if (x < -1n || 1n <= x) {}",
268 options: ["always", { exceptRange: true }],
269 parserOptions: { ecmaVersion: 2020 }
270 },
271 {
272 code: "if (1 < a && a <= 2) {}",
273 options: ["never", { exceptRange: true }]
274 },
275 {
276 code: "if (x < -1 || 1 < x) {}",
277 options: ["never", { exceptRange: true }]
278 },
279 {
280 code: "if (x <= 'bar' || 'foo' < x) {}",
281 options: ["always", { exceptRange: true }]
282 },
283 {
284 code: "if (x < 0 || 1 <= x) {}",
285 options: ["never", { exceptRange: true }]
286 },
287 {
288 code: "if('a' <= x && x < MAX) {}",
289 options: ["never", { exceptRange: true }]
290 },
291
292 // onlyEquality
293 {
294 code: "if (0 < x && x <= 1) {}",
295 options: ["never", { onlyEquality: true }]
296 },
297 {
298 code: "if (x !== 'foo' && 'foo' !== x) {}",
299 options: ["never", { onlyEquality: true }]
300 },
301 {
302 code: "if (x < 2 && x !== -3) {}",
303 options: ["always", { onlyEquality: true }]
304 },
305 {
306 code: "if (x !== `foo` && `foo` !== x) {}",
307 options: ["never", { onlyEquality: true }],
308 parserOptions: { ecmaVersion: 2015 }
309 },
310 {
311 code: "if (x < `2` && x !== `-3`) {}",
312 options: ["always", { onlyEquality: true }],
313 parserOptions: { ecmaVersion: 2015 }
314 }
315 ],
316 invalid: [
317 {
318 code: "if (x <= 'foo' || 'bar' < x) {}",
319 output: "if ('foo' >= x || 'bar' < x) {}",
320 options: ["always", { exceptRange: true }],
321 errors: [
322 {
323 messageId: "expected",
324 data: { expectedSide: "left", operator: "<=" },
325 type: "BinaryExpression"
326 }
327 ]
328 },
329 {
330 code: 'if ("red" == value) {}',
331 output: 'if (value == "red") {}',
332 options: ["never"],
333 errors: [
334 {
335 messageId: "expected",
336 data: { expectedSide: "right", operator: "==" },
337 type: "BinaryExpression"
338 }
339 ]
340 },
341 {
342 code: "if (true === value) {}",
343 output: "if (value === true) {}",
344 options: ["never"],
345 errors: [
346 {
347 messageId: "expected",
348 data: { expectedSide: "right", operator: "===" },
349 type: "BinaryExpression"
350 }
351 ]
352 },
353 {
354 code: "if (5 != value) {}",
355 output: "if (value != 5) {}",
356 options: ["never"],
357 errors: [
358 {
359 messageId: "expected",
360 data: { expectedSide: "right", operator: "!=" },
361 type: "BinaryExpression"
362 }
363 ]
364 },
365 {
366 code: "if (5n != value) {}",
367 output: "if (value != 5n) {}",
368 options: ["never"],
369 parserOptions: { ecmaVersion: 2020 },
370 errors: [
371 {
372 messageId: "expected",
373 data: { expectedSide: "right", operator: "!=" },
374 type: "BinaryExpression"
375 }
376 ]
377 },
378 {
379 code: "if (null !== value) {}",
380 output: "if (value !== null) {}",
381 options: ["never"],
382 errors: [
383 {
384 messageId: "expected",
385 data: { expectedSide: "right", operator: "!==" },
386 type: "BinaryExpression"
387 }
388 ]
389 },
390 {
391 code: 'if ("red" <= value) {}',
392 output: 'if (value >= "red") {}',
393 options: ["never"],
394 errors: [
395 {
396 messageId: "expected",
397 data: { expectedSide: "right", operator: "<=" },
398 type: "BinaryExpression"
399 }
400 ]
401 },
402 {
403 code: "if (`red` <= value) {}",
404 output: "if (value >= `red`) {}",
405 options: ["never"],
406 parserOptions: { ecmaVersion: 2015 },
407 errors: [
408 {
409 messageId: "expected",
410 data: { expectedSide: "right", operator: "<=" },
411 type: "BinaryExpression"
412 }
413 ]
414 },
415 {
416 code: "if (`red` <= `${foo}`) {}",
417 output: "if (`${foo}` >= `red`) {}",
418 options: ["never"],
419 parserOptions: { ecmaVersion: 2015 },
420 errors: [
421 {
422 messageId: "expected",
423 data: { expectedSide: "right", operator: "<=" },
424 type: "BinaryExpression"
425 }
426 ]
427 },
428 {
429 code: 'if (`red` <= `${"red"}`) {}',
430 output: 'if (`${"red"}` >= `red`) {}',
431 options: ["never"],
432 parserOptions: { ecmaVersion: 2015 },
433 errors: [
434 {
435 messageId: "expected",
436 data: { expectedSide: "right", operator: "<=" },
437 type: "BinaryExpression"
438 }
439 ]
440 },
441 {
442 code: "if (true >= value) {}",
443 output: "if (value <= true) {}",
444 options: ["never"],
445 errors: [
446 {
447 messageId: "expected",
448 data: { expectedSide: "right", operator: ">=" },
449 type: "BinaryExpression"
450 }
451 ]
452 },
453 {
454 code: "var foo = (5 < value) ? true : false",
455 output: "var foo = (value > 5) ? true : false",
456 options: ["never"],
457 errors: [
458 {
459 messageId: "expected",
460 data: { expectedSide: "right", operator: "<" },
461 type: "BinaryExpression"
462 }
463 ]
464 },
465 {
466 code: "function foo() { return (null > value); }",
467 output: "function foo() { return (value < null); }",
468 options: ["never"],
469 errors: [
470 {
471 messageId: "expected",
472 data: { expectedSide: "right", operator: ">" },
473 type: "BinaryExpression"
474 }
475 ]
476 },
477 {
478 code: "if (-1 < str.indexOf(substr)) {}",
479 output: "if (str.indexOf(substr) > -1) {}",
480 options: ["never"],
481 errors: [
482 {
483 messageId: "expected",
484 data: { expectedSide: "right", operator: "<" },
485 type: "BinaryExpression"
486 }
487 ]
488 },
489 {
490 code: 'if (value == "red") {}',
491 output: 'if ("red" == value) {}',
492 options: ["always"],
493 errors: [
494 {
495 messageId: "expected",
496 data: { expectedSide: "left", operator: "==" },
497 type: "BinaryExpression"
498 }
499 ]
500 },
501 {
502 code: "if (value == `red`) {}",
503 output: "if (`red` == value) {}",
504 options: ["always"],
505 parserOptions: { ecmaVersion: 2015 },
506 errors: [
507 {
508 messageId: "expected",
509 data: { expectedSide: "left", operator: "==" },
510 type: "BinaryExpression"
511 }
512 ]
513 },
514 {
515 code: "if (value === true) {}",
516 output: "if (true === value) {}",
517 options: ["always"],
518 errors: [
519 {
520 messageId: "expected",
521 data: { expectedSide: "left", operator: "===" },
522 type: "BinaryExpression"
523 }
524 ]
525 },
526 {
527 code: "if (value === 5n) {}",
528 output: "if (5n === value) {}",
529 options: ["always"],
530 parserOptions: { ecmaVersion: 2020 },
531 errors: [
532 {
533 messageId: "expected",
534 data: { expectedSide: "left", operator: "===" },
535 type: "BinaryExpression"
536 }
537 ]
538 },
539 {
540 code: 'if (`${"red"}` <= `red`) {}',
541 output: 'if (`red` >= `${"red"}`) {}',
542 options: ["always"],
543 parserOptions: { ecmaVersion: 2015 },
544 errors: [
545 {
546 messageId: "expected",
547 data: { expectedSide: "left", operator: "<=" },
548 type: "BinaryExpression"
549 }
550 ]
551 },
552 {
553 code: "if (a < 0 && 0 <= b && b < 1) {}",
554 output: "if (a < 0 && b >= 0 && b < 1) {}",
555 options: ["never", { exceptRange: true }],
556 errors: [
557 {
558 messageId: "expected",
559 data: { expectedSide: "right", operator: "<=" },
560 type: "BinaryExpression"
561 }
562 ]
563 },
564 {
565 code: "if (0 <= a && a < 1 && b < 1) {}",
566 output: "if (a >= 0 && a < 1 && b < 1) {}",
567 options: ["never", { exceptRange: true }],
568 errors: [
569 {
570 messageId: "expected",
571 data: { expectedSide: "right", operator: "<=" },
572 type: "BinaryExpression"
573 }
574 ]
575 },
576 {
577 code: "if (1 < a && a < 0) {}",
578 output: "if (a > 1 && a < 0) {}",
579 options: ["never", { exceptRange: true }],
580 errors: [
581 {
582 messageId: "expected",
583 data: { expectedSide: "right", operator: "<" },
584 type: "BinaryExpression"
585 }
586 ]
587 },
588 {
589 code: "0 < a && a < 1",
590 output: "a > 0 && a < 1",
591 options: ["never", { exceptRange: true }],
592 errors: [
593 {
594 messageId: "expected",
595 data: { expectedSide: "right", operator: "<" },
596 type: "BinaryExpression"
597 }
598 ]
599 },
600 {
601 code: "var a = b < 0 || 1 <= b;",
602 output: "var a = b < 0 || b >= 1;",
603 options: ["never", { exceptRange: true }],
604 errors: [
605 {
606 messageId: "expected",
607 data: { expectedSide: "right", operator: "<=" },
608 type: "BinaryExpression"
609 }
610 ]
611 },
612 {
613 code: "if (0 <= x && x < -1) {}",
614 output: "if (x >= 0 && x < -1) {}",
615 options: ["never", { exceptRange: true }],
616 errors: [
617 {
618 messageId: "expected",
619 data: { expectedSide: "right", operator: "<=" },
620 type: "BinaryExpression"
621 }
622 ]
623 },
624 {
625 code: "var a = (b < 0 && 0 <= b);",
626 output: "var a = (0 > b && 0 <= b);",
627 options: ["always", { exceptRange: true }],
628 errors: [
629 {
630 messageId: "expected",
631 data: { expectedSide: "left", operator: "<" },
632 type: "BinaryExpression"
633 }
634 ]
635 },
636 {
637 code: "var a = (b < `0` && `0` <= b);",
638 output: "var a = (`0` > b && `0` <= b);",
639 options: ["always", { exceptRange: true }],
640 parserOptions: { ecmaVersion: 2015 },
641 errors: [
642 {
643 messageId: "expected",
644 data: { expectedSide: "left", operator: "<" },
645 type: "BinaryExpression"
646 }
647 ]
648 },
649 {
650 code: "if (`green` < x.y && x.y < `blue`) {}",
651 output: "if (x.y > `green` && x.y < `blue`) {}",
652 options: ["never", { exceptRange: true }],
653 parserOptions: { ecmaVersion: 2015 },
654 errors: [
655 {
656 messageId: "expected",
657 data: { expectedSide: "right", operator: "<" },
658 type: "BinaryExpression"
659 }
660 ]
661 },
662 {
663 code: "if (0 <= a[b] && a['b'] < 1) {}",
664 output: "if (a[b] >= 0 && a['b'] < 1) {}",
665 options: ["never", { exceptRange: true }],
666 errors: [
667 {
668 messageId: "expected",
669 data: { expectedSide: "right", operator: "<=" },
670 type: "BinaryExpression"
671 }
672 ]
673 },
674 {
675 code: "if (0 <= a[b] && a[`b`] < 1) {}",
676 output: "if (a[b] >= 0 && a[`b`] < 1) {}",
677 options: ["never", { exceptRange: true }],
678 parserOptions: { ecmaVersion: 2015 },
679 errors: [
680 {
681 messageId: "expected",
682 data: { expectedSide: "right", operator: "<=" },
683 type: "BinaryExpression"
684 }
685 ]
686 },
687 {
688 code: "if (`0` <= a[b] && a[`b`] < `1`) {}",
689 output: "if (a[b] >= `0` && a[`b`] < `1`) {}",
690 options: ["never", { exceptRange: true }],
691 parserOptions: { ecmaVersion: 2015 },
692 errors: [
693 {
694 messageId: "expected",
695 data: { expectedSide: "right", operator: "<=" },
696 type: "BinaryExpression"
697 }
698 ]
699 },
700 {
701 code: "if (0 <= a[b] && a.b < 1) {}",
702 output: "if (a[b] >= 0 && a.b < 1) {}",
703 options: ["never", { exceptRange: true }],
704 errors: [
705 {
706 messageId: "expected",
707 data: { expectedSide: "right", operator: "<=" },
708 type: "BinaryExpression"
709 }
710 ]
711 },
712 {
713 code: "if (0 <= a[''] && a.b < 1) {}",
714 output: "if (a[''] >= 0 && a.b < 1) {}",
715 options: ["never", { exceptRange: true }],
716 errors: [
717 {
718 messageId: "expected",
719 data: { expectedSide: "right", operator: "<=" },
720 type: "BinaryExpression"
721 }
722 ]
723 },
724 {
725 code: "if (0 <= a[''] && a[' '] < 1) {}",
726 output: "if (a[''] >= 0 && a[' '] < 1) {}",
727 options: ["never", { exceptRange: true }],
728 errors: [
729 {
730 messageId: "expected",
731 data: { expectedSide: "right", operator: "<=" },
732 type: "BinaryExpression"
733 }
734 ]
735 },
736 {
737 code: "if (0 <= a[''] && a[null] < 1) {}",
738 output: "if (a[''] >= 0 && a[null] < 1) {}",
739 options: ["never", { exceptRange: true }],
740 errors: [
741 {
742 messageId: "expected",
743 data: { expectedSide: "right", operator: "<=" },
744 type: "BinaryExpression"
745 }
746 ]
747 },
748 {
749 code: "if (0 <= a[``] && a[null] < 1) {}",
750 output: "if (a[``] >= 0 && a[null] < 1) {}",
751 options: ["never", { exceptRange: true }],
752 parserOptions: { ecmaVersion: 2015 },
753 errors: [
754 {
755 messageId: "expected",
756 data: { expectedSide: "right", operator: "<=" },
757 type: "BinaryExpression"
758 }
759 ]
760 },
761 {
762 code: "if (0 <= a[''] && a[b] < 1) {}",
763 output: "if (a[''] >= 0 && a[b] < 1) {}",
764 options: ["never", { exceptRange: true }],
765 errors: [
766 {
767 messageId: "expected",
768 data: { expectedSide: "right", operator: "<=" },
769 type: "BinaryExpression"
770 }
771 ]
772 },
773 {
774 code: "if (0 <= a[''] && a[b()] < 1) {}",
775 output: "if (a[''] >= 0 && a[b()] < 1) {}",
776 options: ["never", { exceptRange: true }],
777 errors: [
778 {
779 messageId: "expected",
780 data: { expectedSide: "right", operator: "<=" },
781 type: "BinaryExpression"
782 }
783 ]
784 },
785 {
786 code: "if (0 <= a[``] && a[b()] < 1) {}",
787 output: "if (a[``] >= 0 && a[b()] < 1) {}",
788 options: ["never", { exceptRange: true }],
789 parserOptions: { ecmaVersion: 2015 },
790 errors: [
791 {
792 messageId: "expected",
793 data: { expectedSide: "right", operator: "<=" },
794 type: "BinaryExpression"
795 }
796 ]
797 },
798 {
799 code: "if (0 <= a[b()] && a[b()] < 1) {}",
800 output: "if (a[b()] >= 0 && a[b()] < 1) {}",
801 options: ["never", { exceptRange: true }],
802 errors: [
803 {
804 messageId: "expected",
805 data: { expectedSide: "right", operator: "<=" },
806 type: "BinaryExpression"
807 }
808 ]
809 },
810 {
811 code: "if (0 <= a.null && a[/(?<zero>0)/] <= 1) {}",
812 output: "if (a.null >= 0 && a[/(?<zero>0)/] <= 1) {}",
813 options: ["never", { exceptRange: true }],
814 parserOptions: { ecmaVersion: 2018 },
815 errors: [
816 {
817 messageId: "expected",
818 data: { expectedSide: "right", operator: "<=" },
819 type: "BinaryExpression"
820 }
821 ]
822 },
823 {
824 code: "if (3 == a) {}",
825 output: "if (a == 3) {}",
826 options: ["never", { onlyEquality: true }],
827 errors: [
828 {
829 messageId: "expected",
830 data: { expectedSide: "right", operator: "==" },
831 type: "BinaryExpression"
832 }
833 ]
834 },
835 {
836 code: "foo(3 === a);",
837 output: "foo(a === 3);",
838 options: ["never", { onlyEquality: true }],
839 errors: [
840 {
841 messageId: "expected",
842 data: { expectedSide: "right", operator: "===" },
843 type: "BinaryExpression"
844 }
845 ]
846 },
847 {
848 code: "foo(a === 3);",
849 output: "foo(3 === a);",
850 options: ["always", { onlyEquality: true }],
851 errors: [
852 {
853 messageId: "expected",
854 data: { expectedSide: "left", operator: "===" },
855 type: "BinaryExpression"
856 }
857 ]
858 },
859 {
860 code: "foo(a === `3`);",
861 output: "foo(`3` === a);",
862 options: ["always", { onlyEquality: true }],
863 parserOptions: { ecmaVersion: 2015 },
864 errors: [
865 {
866 messageId: "expected",
867 data: { expectedSide: "left", operator: "===" },
868 type: "BinaryExpression"
869 }
870 ]
871 },
872 {
873 code: "if (0 <= x && x < 1) {}",
874 output: "if (x >= 0 && x < 1) {}",
875 errors: [
876 {
877 messageId: "expected",
878 data: { expectedSide: "right", operator: "<=" },
879 type: "BinaryExpression"
880 }
881 ]
882 },
883 {
884 code: "if ( /* a */ 0 /* b */ < /* c */ foo /* d */ ) {}",
885 output: "if ( /* a */ foo /* b */ > /* c */ 0 /* d */ ) {}",
886 options: ["never"],
887 errors: [
888 {
889 messageId: "expected",
890 data: { expectedSide: "right", operator: "<" },
891 type: "BinaryExpression"
892 }
893 ]
894 },
895 {
896 code: "if ( /* a */ foo /* b */ > /* c */ 0 /* d */ ) {}",
897 output: "if ( /* a */ 0 /* b */ < /* c */ foo /* d */ ) {}",
898 options: ["always"],
899 errors: [
900 {
901 messageId: "expected",
902 data: { expectedSide: "left", operator: ">" },
903 type: "BinaryExpression"
904 }
905 ]
906 },
907 {
908 code: "if (foo()===1) {}",
909 output: "if (1===foo()) {}",
910 options: ["always"],
911 errors: [
912 {
913 messageId: "expected",
914 data: { expectedSide: "left", operator: "===" },
915 type: "BinaryExpression"
916 }
917 ]
918 },
919 {
920 code: "if (foo() === 1) {}",
921 output: "if (1 === foo()) {}",
922 options: ["always"],
923 errors: [
924 {
925 messageId: "expected",
926 data: { expectedSide: "left", operator: "===" },
927 type: "BinaryExpression"
928 }
929 ]
930 },
931
932 // https://github.com/eslint/eslint/issues/7326
933 {
934 code: "while (0 === (a));",
935 output: "while ((a) === 0);",
936 options: ["never"],
937 errors: [
938 {
939 messageId: "expected",
940 data: { expectedSide: "right", operator: "===" },
941 type: "BinaryExpression"
942 }
943 ]
944 },
945 {
946 code: "while (0 === (a = b));",
947 output: "while ((a = b) === 0);",
948 options: ["never"],
949 errors: [
950 {
951 messageId: "expected",
952 data: { expectedSide: "right", operator: "===" },
953 type: "BinaryExpression"
954 }
955 ]
956 },
957 {
958 code: "while ((a) === 0);",
959 output: "while (0 === (a));",
960 options: ["always"],
961 errors: [
962 {
963 messageId: "expected",
964 data: { expectedSide: "left", operator: "===" },
965 type: "BinaryExpression"
966 }
967 ]
968 },
969 {
970 code: "while ((a = b) === 0);",
971 output: "while (0 === (a = b));",
972 options: ["always"],
973 errors: [
974 {
975 messageId: "expected",
976 data: { expectedSide: "left", operator: "===" },
977 type: "BinaryExpression"
978 }
979 ]
980 },
981 {
982 code: "if (((((((((((foo)))))))))) === ((((((5)))))));",
983 output: "if (((((((5)))))) === ((((((((((foo)))))))))));",
984 options: ["always"],
985 errors: [
986 {
987 messageId: "expected",
988 data: { expectedSide: "left", operator: "===" },
989 type: "BinaryExpression"
990 }
991 ]
992 },
993
994 // Adjacent tokens tests
995 {
996 code: "function *foo() { yield(1) < a }",
997 output: "function *foo() { yield a > (1) }",
998 options: ["never"],
999 parserOptions: { ecmaVersion: 2015 },
1000 errors: [
1001 {
1002 messageId: "expected",
1003 data: { expectedSide: "right", operator: "<" },
1004 type: "BinaryExpression"
1005 }
1006 ]
1007 },
1008 {
1009 code: "function *foo() { yield((1)) < a }",
1010 output: "function *foo() { yield a > ((1)) }",
1011 options: ["never"],
1012 parserOptions: { ecmaVersion: 2015 },
1013 errors: [
1014 {
1015 messageId: "expected",
1016 data: { expectedSide: "right", operator: "<" },
1017 type: "BinaryExpression"
1018 }
1019 ]
1020 },
1021 {
1022 code: "function *foo() { yield 1 < a }",
1023 output: "function *foo() { yield a > 1 }",
1024 options: ["never"],
1025 parserOptions: { ecmaVersion: 2015 },
1026 errors: [
1027 {
1028 messageId: "expected",
1029 data: { expectedSide: "right", operator: "<" },
1030 type: "BinaryExpression"
1031 }
1032 ]
1033 },
1034 {
1035 code: "function *foo() { yield/**/1 < a }",
1036 output: "function *foo() { yield/**/a > 1 }",
1037 options: ["never"],
1038 parserOptions: { ecmaVersion: 2015 },
1039 errors: [
1040 {
1041 messageId: "expected",
1042 data: { expectedSide: "right", operator: "<" },
1043 type: "BinaryExpression"
1044 }
1045 ]
1046 },
1047 {
1048 code: "function *foo() { yield(1) < ++a }",
1049 output: "function *foo() { yield++a > (1) }",
1050 options: ["never"],
1051 parserOptions: { ecmaVersion: 2015 },
1052 errors: [
1053 {
1054 messageId: "expected",
1055 data: { expectedSide: "right", operator: "<" },
1056 type: "BinaryExpression"
1057 }
1058 ]
1059 },
1060 {
1061 code: "function *foo() { yield(1) < (a) }",
1062 output: "function *foo() { yield(a) > (1) }",
1063 options: ["never"],
1064 parserOptions: { ecmaVersion: 2015 },
1065 errors: [
1066 {
1067 messageId: "expected",
1068 data: { expectedSide: "right", operator: "<" },
1069 type: "BinaryExpression"
1070 }
1071 ]
1072 },
1073 {
1074 code: "x=1 < a",
1075 output: "x=a > 1",
1076 options: ["never"],
1077 errors: [
1078 {
1079 messageId: "expected",
1080 data: { expectedSide: "right", operator: "<" },
1081 type: "BinaryExpression"
1082 }
1083 ]
1084 },
1085 {
1086 code: "function *foo() { yield++a < 1 }",
1087 output: "function *foo() { yield 1 > ++a }",
1088 options: ["always"],
1089 parserOptions: { ecmaVersion: 2015 },
1090 errors: [
1091 {
1092 messageId: "expected",
1093 data: { expectedSide: "left", operator: "<" },
1094 type: "BinaryExpression"
1095 }
1096 ]
1097 },
1098 {
1099 code: "function *foo() { yield(a) < 1 }",
1100 output: "function *foo() { yield 1 > (a) }",
1101 options: ["always"],
1102 parserOptions: { ecmaVersion: 2015 },
1103 errors: [
1104 {
1105 messageId: "expected",
1106 data: { expectedSide: "left", operator: "<" },
1107 type: "BinaryExpression"
1108 }
1109 ]
1110 },
1111 {
1112 code: "function *foo() { yield a < 1 }",
1113 output: "function *foo() { yield 1 > a }",
1114 options: ["always"],
1115 parserOptions: { ecmaVersion: 2015 },
1116 errors: [
1117 {
1118 messageId: "expected",
1119 data: { expectedSide: "left", operator: "<" },
1120 type: "BinaryExpression"
1121 }
1122 ]
1123 },
1124 {
1125 code: "function *foo() { yield/**/a < 1 }",
1126 output: "function *foo() { yield/**/1 > a }",
1127 options: ["always"],
1128 parserOptions: { ecmaVersion: 2015 },
1129 errors: [
1130 {
1131 messageId: "expected",
1132 data: { expectedSide: "left", operator: "<" },
1133 type: "BinaryExpression"
1134 }
1135 ]
1136 },
1137 {
1138 code: "function *foo() { yield++a < (1) }",
1139 output: "function *foo() { yield(1) > ++a }",
1140 options: ["always"],
1141 parserOptions: { ecmaVersion: 2015 },
1142 errors: [
1143 {
1144 messageId: "expected",
1145 data: { expectedSide: "left", operator: "<" },
1146 type: "BinaryExpression"
1147 }
1148 ]
1149 },
1150 {
1151 code: "x=a < 1",
1152 output: "x=1 > a",
1153 options: ["always"],
1154 errors: [
1155 {
1156 messageId: "expected",
1157 data: { expectedSide: "left", operator: "<" },
1158 type: "BinaryExpression"
1159 }
1160 ]
1161 },
1162 {
1163 code: "if (`green` < x.y && x.y < `blue`) {}",
1164 output: "if (`green` < x.y && `blue` > x.y) {}",
1165 options: ["always", { exceptRange: true }],
1166 parserOptions: { ecmaVersion: 2015 },
1167 errors: [
1168 {
1169 messageId: "expected",
1170 data: { expectedSide: "left", operator: "<" },
1171 type: "BinaryExpression"
1172 }
1173 ]
1174 },
1175 {
1176 code: "if('a' <= x && x < 'b') {}",
1177 output: "if('a' <= x && 'b' > x) {}",
1178 options: ["always"],
1179 errors: [
1180 {
1181 messageId: "expected",
1182 data: { expectedSide: "left", operator: "<" },
1183 type: "BinaryExpression"
1184 }
1185 ]
1186 },
1187 {
1188 code: "if ('b' <= x && x < 'a') {}",
1189 output: "if (x >= 'b' && x < 'a') {}",
1190 options: ["never", { exceptRange: true }],
1191 errors: [
1192 {
1193 messageId: "expected",
1194 data: { expectedSide: "right", operator: "<=" },
1195 type: "BinaryExpression"
1196 }
1197 ]
1198 },
1199 {
1200 code: "if('a' <= x && x < 1) {}",
1201 output: "if(x >= 'a' && x < 1) {}",
1202 options: ["never", { exceptRange: true }],
1203 errors: [
1204 {
1205 messageId: "expected",
1206 data: { expectedSide: "right", operator: "<=" },
1207 type: "BinaryExpression"
1208 }
1209 ]
1210 },
1211 {
1212 code: "if (0 < a && b < max) {}",
1213 output: "if (a > 0 && b < max) {}",
1214 options: ["never", { exceptRange: true }],
1215 errors: [
1216 {
1217 messageId: "expected",
1218 data: { expectedSide: "right", operator: "<" },
1219 type: "BinaryExpression"
1220 }
1221 ]
1222 }
1223 ]
1224 });