]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/yoda.js
first commit
[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 { code: "if (value === `red`) {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
30 { code: "if (`red` === `red`) {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
31 { code: "if (`${foo}` === `red`) {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
32 { code: "if (`${\"\"}` === `red`) {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
33 { code: "if (`${\"red\"}` === foo) {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
34 { code: "if (b > `a` && b > `a`) {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
35 { code: "if (`b` > `a` && \"b\" > \"a\") {}", options: ["never"], parserOptions: { ecmaVersion: 2015 } },
36
37 // "always" mode
38 { code: "if (\"blue\" === value) {}", options: ["always"] },
39 { code: "if (value === value) {}", options: ["always"] },
40 { code: "if (4 != value) {}", options: ["always"] },
41 { code: "if (foo & 4) {}", options: ["always"] },
42 { code: "if (5 === 4) {}", options: ["always"] },
43 { code: "if (`red` === value) {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
44 { code: "if (`red` === `red`) {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
45 { code: "if (`red` === `${foo}`) {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
46 { code: "if (`red` === `${\"\"}`) {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
47 { code: "if (foo === `${\"red\"}`) {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
48 { code: "if (`a` > b && `a` > b) {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
49 { code: "if (`b` > `a` && \"b\" > \"a\") {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },
50
51 // Range exception
52 {
53 code: "if (0 < x && x <= 1) {}",
54 options: ["never", { exceptRange: true }]
55 }, {
56 code: "if (x < 0 || 1 <= x) {}",
57 options: ["never", { exceptRange: true }]
58 }, {
59 code: "if (0 <= x && x < 1) {}",
60 options: ["always", { exceptRange: true }]
61 }, {
62 code: "if (x <= 'bar' || 'foo' < x) {}",
63 options: ["always", { exceptRange: true }]
64 }, {
65 code: "if ('blue' < x.y && x.y < 'green') {}",
66 options: ["never", { exceptRange: true }]
67 }, {
68 code: "if (0 < x[``] && x[``] < 100) {}",
69 options: ["never", { exceptRange: true }],
70 parserOptions: { ecmaVersion: 2015 }
71 }, {
72 code: "if (0 < x[''] && x[``] < 100) {}",
73 options: ["never", { exceptRange: true }],
74 parserOptions: { ecmaVersion: 2015 }
75 }, {
76 code: "if (0 <= x['y'] && x['y'] <= 100) {}",
77 options: ["never", { exceptRange: true }]
78 }, {
79 code: "if (a < 0 && (0 < b && b < 1)) {}",
80 options: ["never", { exceptRange: true }]
81 }, {
82 code: "if ((0 < a && a < 1) && b < 0) {}",
83 options: ["never", { exceptRange: true }]
84 }, {
85 code: "if (a < 4 || (b[c[0]].d['e'] < 0 || 1 <= b[c[0]].d['e'])) {}",
86 options: ["never", { exceptRange: true }]
87 }, {
88 code: "if (-1 < x && x < 0) {}",
89 options: ["never", { exceptRange: true }]
90 }, {
91 code: "if (0 <= this.prop && this.prop <= 1) {}",
92 options: ["never", { exceptRange: true }]
93 }, {
94 code: "if (0 <= index && index < list.length) {}",
95 options: ["never", { exceptRange: true }]
96 }, {
97 code: "if (ZERO <= index && index < 100) {}",
98 options: ["never", { exceptRange: true }]
99 }, {
100 code: "if (value <= MIN || 10 < value) {}",
101 options: ["never", { exceptRange: true }]
102 }, {
103 code: "if (value <= 0 || MAX < value) {}",
104 options: ["never", { exceptRange: true }]
105 }, {
106 code: "if (0 <= a.b && a[\"b\"] <= 100) {}",
107 options: ["never", { exceptRange: true }]
108 }, {
109 code: "if (0 <= a.b && a[`b`] <= 100) {}",
110 options: ["never", { exceptRange: true }],
111 parserOptions: { ecmaVersion: 2015 }
112 }, {
113 code: "if (-1n < x && x <= 1n) {}",
114 options: ["never", { exceptRange: true }],
115 parserOptions: { ecmaVersion: 2020 }
116 }, {
117 code: "if (x < -1n || 1n <= x) {}",
118 options: ["never", { exceptRange: true }],
119 parserOptions: { ecmaVersion: 2020 }
120 }, {
121 code: "if (-1n <= x && x < 1n) {}",
122 options: ["always", { exceptRange: true }],
123 parserOptions: { ecmaVersion: 2020 }
124 }, {
125 code: "if (x < -1n || 1n <= x) {}",
126 options: ["always", { exceptRange: true }],
127 parserOptions: { ecmaVersion: 2020 }
128 }, {
129 code: "if (x < `1` || `1` < x) {}",
130 options: ["always", { exceptRange: true }],
131 parserOptions: { ecmaVersion: 2020 }
132 }, {
133 code: "if (1 <= a['/(?<zero>0)/'] && a[/(?<zero>0)/] <= 100) {}",
134 options: ["never", { exceptRange: true }],
135 parserOptions: { ecmaVersion: 2018 }
136 }, {
137 code: "if (x <= `bar` || `foo` < x) {}",
138 options: ["always", { exceptRange: true }],
139 parserOptions: { ecmaVersion: 2015 }
140 }, {
141 code: "if (`blue` < x.y && x.y < `green`) {}",
142 options: ["never", { exceptRange: true }],
143 parserOptions: { ecmaVersion: 2015 }
144 }, {
145 code: "if (0 <= x[`y`] && x[`y`] <= 100) {}",
146 options: ["never", { exceptRange: true }],
147 parserOptions: { ecmaVersion: 2015 }
148 }, {
149 code: "if (0 <= x[`y`] && x[\"y\"] <= 100) {}",
150 options: ["never", { exceptRange: true }],
151 parserOptions: { ecmaVersion: 2015 }
152 },
153
154 // onlyEquality
155 { code: "if (0 < x && x <= 1) {}", options: ["never", { onlyEquality: true }] },
156 { code: "if (x !== 'foo' && 'foo' !== x) {}", options: ["never", { onlyEquality: true }] },
157 { code: "if (x < 2 && x !== -3) {}", options: ["always", { onlyEquality: true }] },
158 { code: "if (x !== `foo` && `foo` !== x) {}", options: ["never", { onlyEquality: true }], parserOptions: { ecmaVersion: 2015 } },
159 { code: "if (x < `2` && x !== `-3`) {}", options: ["always", { onlyEquality: true }], parserOptions: { ecmaVersion: 2015 } }
160 ],
161 invalid: [
162
163 {
164 code: "if (\"red\" == value) {}",
165 output: "if (value == \"red\") {}",
166 options: ["never"],
167 errors: [
168 {
169 messageId: "expected",
170 data: { expectedSide: "right", operator: "==" },
171 type: "BinaryExpression"
172 }
173 ]
174 },
175 {
176 code: "if (true === value) {}",
177 output: "if (value === true) {}",
178 options: ["never"],
179 errors: [
180 {
181 messageId: "expected",
182 data: { expectedSide: "right", operator: "===" },
183 type: "BinaryExpression"
184 }
185 ]
186 },
187 {
188 code: "if (5 != value) {}",
189 output: "if (value != 5) {}",
190 options: ["never"],
191 errors: [
192 {
193 messageId: "expected",
194 data: { expectedSide: "right", operator: "!=" },
195 type: "BinaryExpression"
196 }
197 ]
198 },
199 {
200 code: "if (5n != value) {}",
201 output: "if (value != 5n) {}",
202 options: ["never"],
203 parserOptions: { ecmaVersion: 2020 },
204 errors: [
205 {
206 messageId: "expected",
207 data: { expectedSide: "right", operator: "!=" },
208 type: "BinaryExpression"
209 }
210 ]
211 },
212 {
213 code: "if (null !== value) {}",
214 output: "if (value !== null) {}",
215 options: ["never"],
216 errors: [
217 {
218 messageId: "expected",
219 data: { expectedSide: "right", operator: "!==" },
220 type: "BinaryExpression"
221 }
222 ]
223 },
224 {
225 code: "if (\"red\" <= value) {}",
226 output: "if (value >= \"red\") {}",
227 options: ["never"],
228 errors: [
229 {
230 messageId: "expected",
231 data: { expectedSide: "right", operator: "<=" },
232 type: "BinaryExpression"
233 }
234 ]
235 },
236 {
237 code: "if (`red` <= value) {}",
238 output: "if (value >= `red`) {}",
239 options: ["never"],
240 parserOptions: { ecmaVersion: 2015 },
241 errors: [
242 {
243 messageId: "expected",
244 data: { expectedSide: "right", operator: "<=" },
245 type: "BinaryExpression"
246 }
247 ]
248 },
249 {
250 code: "if (`red` <= `${foo}`) {}",
251 output: "if (`${foo}` >= `red`) {}",
252 options: ["never"],
253 parserOptions: { ecmaVersion: 2015 },
254 errors: [
255 {
256 messageId: "expected",
257 data: { expectedSide: "right", operator: "<=" },
258 type: "BinaryExpression"
259 }
260 ]
261 },
262 {
263 code: "if (`red` <= `${\"red\"}`) {}",
264 output: "if (`${\"red\"}` >= `red`) {}",
265 options: ["never"],
266 parserOptions: { ecmaVersion: 2015 },
267 errors: [
268 {
269 messageId: "expected",
270 data: { expectedSide: "right", operator: "<=" },
271 type: "BinaryExpression"
272 }
273 ]
274 },
275 {
276 code: "if (true >= value) {}",
277 output: "if (value <= true) {}",
278 options: ["never"],
279 errors: [
280 {
281 messageId: "expected",
282 data: { expectedSide: "right", operator: ">=" },
283 type: "BinaryExpression"
284 }
285 ]
286 },
287 {
288 code: "var foo = (5 < value) ? true : false",
289 output: "var foo = (value > 5) ? true : false",
290 options: ["never"],
291 errors: [
292 {
293 messageId: "expected",
294 data: { expectedSide: "right", operator: "<" },
295 type: "BinaryExpression"
296 }
297 ]
298 },
299 {
300 code: "function foo() { return (null > value); }",
301 output: "function foo() { return (value < null); }",
302 options: ["never"],
303 errors: [
304 {
305 messageId: "expected",
306 data: { expectedSide: "right", operator: ">" },
307 type: "BinaryExpression"
308 }
309 ]
310 },
311 {
312 code: "if (-1 < str.indexOf(substr)) {}",
313 output: "if (str.indexOf(substr) > -1) {}",
314 options: ["never"],
315 errors: [
316 {
317 messageId: "expected",
318 data: { expectedSide: "right", operator: "<" },
319 type: "BinaryExpression"
320 }
321 ]
322 },
323 {
324 code: "if (value == \"red\") {}",
325 output: "if (\"red\" == value) {}",
326 options: ["always"],
327 errors: [
328 {
329 messageId: "expected",
330 data: { expectedSide: "left", operator: "==" },
331 type: "BinaryExpression"
332 }
333 ]
334 },
335 {
336 code: "if (value == `red`) {}",
337 output: "if (`red` == value) {}",
338 options: ["always"],
339 parserOptions: { ecmaVersion: 2015 },
340 errors: [
341 {
342 messageId: "expected",
343 data: { expectedSide: "left", operator: "==" },
344 type: "BinaryExpression"
345 }
346 ]
347 },
348 {
349 code: "if (value === true) {}",
350 output: "if (true === value) {}",
351 options: ["always"],
352 errors: [
353 {
354 messageId: "expected",
355 data: { expectedSide: "left", operator: "===" },
356 type: "BinaryExpression"
357 }
358 ]
359 },
360 {
361 code: "if (value === 5n) {}",
362 output: "if (5n === value) {}",
363 options: ["always"],
364 parserOptions: { ecmaVersion: 2020 },
365 errors: [
366 {
367 messageId: "expected",
368 data: { expectedSide: "left", operator: "===" },
369 type: "BinaryExpression"
370 }
371 ]
372 },
373 {
374 code: "if (`${\"red\"}` <= `red`) {}",
375 output: "if (`red` >= `${\"red\"}`) {}",
376 options: ["always"],
377 parserOptions: { ecmaVersion: 2015 },
378 errors: [
379 {
380 messageId: "expected",
381 data: { expectedSide: "left", operator: "<=" },
382 type: "BinaryExpression"
383 }
384 ]
385 },
386 {
387 code: "if (a < 0 && 0 <= b && b < 1) {}",
388 output: "if (a < 0 && b >= 0 && b < 1) {}",
389 options: ["never", { exceptRange: true }],
390 errors: [
391 {
392 messageId: "expected",
393 data: { expectedSide: "right", operator: "<=" },
394 type: "BinaryExpression"
395 }
396 ]
397 },
398 {
399 code: "if (0 <= a && a < 1 && b < 1) {}",
400 output: "if (a >= 0 && a < 1 && b < 1) {}",
401 options: ["never", { exceptRange: true }],
402 errors: [
403 {
404 messageId: "expected",
405 data: { expectedSide: "right", operator: "<=" },
406 type: "BinaryExpression"
407 }
408 ]
409 },
410 {
411 code: "if (1 < a && a < 0) {}",
412 output: "if (a > 1 && a < 0) {}",
413 options: ["never", { exceptRange: true }],
414 errors: [
415 {
416 messageId: "expected",
417 data: { expectedSide: "right", operator: "<" },
418 type: "BinaryExpression"
419 }
420 ]
421 },
422 {
423 code: "0 < a && a < 1",
424 output: "a > 0 && a < 1",
425 options: ["never", { exceptRange: true }],
426 errors: [
427 {
428 messageId: "expected",
429 data: { expectedSide: "right", operator: "<" },
430 type: "BinaryExpression"
431 }
432 ]
433 },
434 {
435 code: "var a = b < 0 || 1 <= b;",
436 output: "var a = b < 0 || b >= 1;",
437 options: ["never", { exceptRange: true }],
438 errors: [
439 {
440 messageId: "expected",
441 data: { expectedSide: "right", operator: "<=" },
442 type: "BinaryExpression"
443 }
444 ]
445 },
446 {
447 code: "if (0 <= x && x < -1) {}",
448 output: "if (x >= 0 && x < -1) {}",
449 options: ["never", { exceptRange: true }],
450 errors: [
451 {
452 messageId: "expected",
453 data: { expectedSide: "right", operator: "<=" },
454 type: "BinaryExpression"
455 }
456 ]
457 },
458 {
459 code: "var a = (b < 0 && 0 <= b);",
460 output: "var a = (0 > b && 0 <= b);",
461 options: ["always", { exceptRange: true }],
462 errors: [
463 {
464 messageId: "expected",
465 data: { expectedSide: "left", operator: "<" },
466 type: "BinaryExpression"
467 }
468 ]
469 },
470 {
471 code: "var a = (b < `0` && `0` <= b);",
472 output: "var a = (`0` > b && `0` <= b);",
473 options: ["always", { exceptRange: true }],
474 parserOptions: { ecmaVersion: 2015 },
475 errors: [
476 {
477 messageId: "expected",
478 data: { expectedSide: "left", operator: "<" },
479 type: "BinaryExpression"
480 }
481 ]
482 },
483 {
484 code: "if (0 <= a[b] && a['b'] < 1) {}",
485 output: "if (a[b] >= 0 && a['b'] < 1) {}",
486 options: ["never", { exceptRange: true }],
487 errors: [
488 {
489 messageId: "expected",
490 data: { expectedSide: "right", operator: "<=" },
491 type: "BinaryExpression"
492 }
493 ]
494 },
495 {
496 code: "if (0 <= a[b] && a[`b`] < 1) {}",
497 output: "if (a[b] >= 0 && a[`b`] < 1) {}",
498 options: ["never", { exceptRange: true }],
499 parserOptions: { ecmaVersion: 2015 },
500 errors: [
501 {
502 messageId: "expected",
503 data: { expectedSide: "right", operator: "<=" },
504 type: "BinaryExpression"
505 }
506 ]
507 },
508 {
509 code: "if (`0` <= a[b] && a[`b`] < `1`) {}",
510 output: "if (a[b] >= `0` && a[`b`] < `1`) {}",
511 options: ["never", { exceptRange: true }],
512 parserOptions: { ecmaVersion: 2015 },
513 errors: [
514 {
515 messageId: "expected",
516 data: { expectedSide: "right", operator: "<=" },
517 type: "BinaryExpression"
518 }
519 ]
520 },
521 {
522 code: "if (0 <= a[b] && a.b < 1) {}",
523 output: "if (a[b] >= 0 && a.b < 1) {}",
524 options: ["never", { exceptRange: true }],
525 errors: [
526 {
527 messageId: "expected",
528 data: { expectedSide: "right", operator: "<=" },
529 type: "BinaryExpression"
530 }
531 ]
532 },
533 {
534 code: "if (0 <= a[''] && a.b < 1) {}",
535 output: "if (a[''] >= 0 && a.b < 1) {}",
536 options: ["never", { exceptRange: true }],
537 errors: [
538 {
539 messageId: "expected",
540 data: { expectedSide: "right", operator: "<=" },
541 type: "BinaryExpression"
542 }
543 ]
544 },
545 {
546 code: "if (0 <= a[''] && a[' '] < 1) {}",
547 output: "if (a[''] >= 0 && a[' '] < 1) {}",
548 options: ["never", { exceptRange: true }],
549 errors: [
550 {
551 messageId: "expected",
552 data: { expectedSide: "right", operator: "<=" },
553 type: "BinaryExpression"
554 }
555 ]
556 },
557 {
558 code: "if (0 <= a[''] && a[null] < 1) {}",
559 output: "if (a[''] >= 0 && a[null] < 1) {}",
560 options: ["never", { exceptRange: true }],
561 errors: [
562 {
563 messageId: "expected",
564 data: { expectedSide: "right", operator: "<=" },
565 type: "BinaryExpression"
566 }
567 ]
568 },
569 {
570 code: "if (0 <= a[``] && a[null] < 1) {}",
571 output: "if (a[``] >= 0 && a[null] < 1) {}",
572 options: ["never", { exceptRange: true }],
573 parserOptions: { ecmaVersion: 2015 },
574 errors: [
575 {
576 messageId: "expected",
577 data: { expectedSide: "right", operator: "<=" },
578 type: "BinaryExpression"
579 }
580 ]
581 },
582 {
583 code: "if (0 <= a[''] && a[b] < 1) {}",
584 output: "if (a[''] >= 0 && a[b] < 1) {}",
585 options: ["never", { exceptRange: true }],
586 errors: [
587 {
588 messageId: "expected",
589 data: { expectedSide: "right", operator: "<=" },
590 type: "BinaryExpression"
591 }
592 ]
593 },
594 {
595 code: "if (0 <= a[''] && a[b()] < 1) {}",
596 output: "if (a[''] >= 0 && a[b()] < 1) {}",
597 options: ["never", { exceptRange: true }],
598 errors: [
599 {
600 messageId: "expected",
601 data: { expectedSide: "right", operator: "<=" },
602 type: "BinaryExpression"
603 }
604 ]
605 },
606 {
607 code: "if (0 <= a[``] && a[b()] < 1) {}",
608 output: "if (a[``] >= 0 && a[b()] < 1) {}",
609 options: ["never", { exceptRange: true }],
610 parserOptions: { ecmaVersion: 2015 },
611 errors: [
612 {
613 messageId: "expected",
614 data: { expectedSide: "right", operator: "<=" },
615 type: "BinaryExpression"
616 }
617 ]
618 },
619 {
620 code: "if (0 <= a[b()] && a[b()] < 1) {}",
621 output: "if (a[b()] >= 0 && a[b()] < 1) {}",
622 options: ["never", { exceptRange: true }],
623 errors: [
624 {
625 messageId: "expected",
626 data: { expectedSide: "right", operator: "<=" },
627 type: "BinaryExpression"
628 }
629 ]
630 },
631 {
632 code: "if (0 <= a.null && a[/(?<zero>0)/] <= 1) {}",
633 output: "if (a.null >= 0 && a[/(?<zero>0)/] <= 1) {}",
634 options: ["never", { exceptRange: true }],
635 parserOptions: { ecmaVersion: 2018 },
636 errors: [
637 {
638 messageId: "expected",
639 data: { expectedSide: "right", operator: "<=" },
640 type: "BinaryExpression"
641 }
642 ]
643 },
644 {
645 code: "if (`green` < x.y && x.y < `blue`) {}",
646 output: "if (x.y > `green` && x.y < `blue`) {}",
647 options: ["never", { exceptRange: true }],
648 parserOptions: { ecmaVersion: 2015 },
649 errors: [
650 {
651 messageId: "expected",
652 data: { expectedSide: "right", operator: "<" },
653 type: "BinaryExpression"
654 }
655 ]
656 },
657 {
658 code: "if (3 == a) {}",
659 output: "if (a == 3) {}",
660 options: ["never", { onlyEquality: true }],
661 errors: [
662 {
663 messageId: "expected",
664 data: { expectedSide: "right", operator: "==" },
665 type: "BinaryExpression"
666 }
667 ]
668 },
669 {
670 code: "foo(3 === a);",
671 output: "foo(a === 3);",
672 options: ["never", { onlyEquality: true }],
673 errors: [
674 {
675 messageId: "expected",
676 data: { expectedSide: "right", operator: "===" },
677 type: "BinaryExpression"
678 }
679 ]
680 },
681 {
682 code: "foo(a === 3);",
683 output: "foo(3 === a);",
684 options: ["always", { onlyEquality: true }],
685 errors: [
686 {
687 messageId: "expected",
688 data: { expectedSide: "left", operator: "===" },
689 type: "BinaryExpression"
690 }
691 ]
692 },
693 {
694 code: "foo(a === `3`);",
695 output: "foo(`3` === a);",
696 options: ["always", { onlyEquality: true }],
697 parserOptions: { ecmaVersion: 2015 },
698 errors: [
699 {
700 messageId: "expected",
701 data: { expectedSide: "left", operator: "===" },
702 type: "BinaryExpression"
703 }
704 ]
705 },
706 {
707 code: "if (0 <= x && x < 1) {}",
708 output: "if (x >= 0 && x < 1) {}",
709 errors: [
710 {
711 messageId: "expected",
712 data: { expectedSide: "right", operator: "<=" },
713 type: "BinaryExpression"
714 }
715 ]
716 },
717 {
718 code: "if ( /* a */ 0 /* b */ < /* c */ foo /* d */ ) {}",
719 output: "if ( /* a */ foo /* b */ > /* c */ 0 /* d */ ) {}",
720 options: ["never"],
721 errors: [
722 {
723 messageId: "expected",
724 data: { expectedSide: "right", operator: "<" },
725 type: "BinaryExpression"
726 }
727 ]
728 },
729 {
730 code: "if ( /* a */ foo /* b */ > /* c */ 0 /* d */ ) {}",
731 output: "if ( /* a */ 0 /* b */ < /* c */ foo /* d */ ) {}",
732 options: ["always"],
733 errors: [
734 {
735 messageId: "expected",
736 data: { expectedSide: "left", operator: ">" },
737 type: "BinaryExpression"
738 }
739 ]
740 },
741 {
742 code: "if (foo()===1) {}",
743 output: "if (1===foo()) {}",
744 options: ["always"],
745 errors: [
746 {
747 messageId: "expected",
748 data: { expectedSide: "left", operator: "===" },
749 type: "BinaryExpression"
750 }
751 ]
752 },
753 {
754 code: "if (foo() === 1) {}",
755 output: "if (1 === foo()) {}",
756 options: ["always"],
757 errors: [
758 {
759 messageId: "expected",
760 data: { expectedSide: "left", operator: "===" },
761 type: "BinaryExpression"
762 }
763 ]
764 },
765
766 // https://github.com/eslint/eslint/issues/7326
767 {
768 code: "while (0 === (a));",
769 output: "while ((a) === 0);",
770 options: ["never"],
771 errors: [
772 {
773 messageId: "expected",
774 data: { expectedSide: "right", operator: "===" },
775 type: "BinaryExpression"
776 }
777 ]
778 },
779 {
780 code: "while (0 === (a = b));",
781 output: "while ((a = b) === 0);",
782 options: ["never"],
783 errors: [
784 {
785 messageId: "expected",
786 data: { expectedSide: "right", operator: "===" },
787 type: "BinaryExpression"
788 }
789 ]
790 },
791 {
792 code: "while ((a) === 0);",
793 output: "while (0 === (a));",
794 options: ["always"],
795 errors: [
796 {
797 messageId: "expected",
798 data: { expectedSide: "left", operator: "===" },
799 type: "BinaryExpression"
800 }
801 ]
802 },
803 {
804 code: "while ((a = b) === 0);",
805 output: "while (0 === (a = b));",
806 options: ["always"],
807 errors: [
808 {
809 messageId: "expected",
810 data: { expectedSide: "left", operator: "===" },
811 type: "BinaryExpression"
812 }
813 ]
814 },
815 {
816 code: "if (((((((((((foo)))))))))) === ((((((5)))))));",
817 output: "if (((((((5)))))) === ((((((((((foo)))))))))));",
818 options: ["always"],
819 errors: [
820 {
821 messageId: "expected",
822 data: { expectedSide: "left", operator: "===" },
823 type: "BinaryExpression"
824 }
825 ]
826 },
827
828 // Adjacent tokens tests
829 {
830 code: "function *foo() { yield(1) < a }",
831 output: "function *foo() { yield a > (1) }",
832 options: ["never"],
833 parserOptions: { ecmaVersion: 2015 },
834 errors: [
835 {
836 messageId: "expected",
837 data: { expectedSide: "right", operator: "<" },
838 type: "BinaryExpression"
839 }
840 ]
841 },
842 {
843 code: "function *foo() { yield((1)) < a }",
844 output: "function *foo() { yield a > ((1)) }",
845 options: ["never"],
846 parserOptions: { ecmaVersion: 2015 },
847 errors: [
848 {
849 messageId: "expected",
850 data: { expectedSide: "right", operator: "<" },
851 type: "BinaryExpression"
852 }
853 ]
854 },
855 {
856 code: "function *foo() { yield 1 < a }",
857 output: "function *foo() { yield a > 1 }",
858 options: ["never"],
859 parserOptions: { ecmaVersion: 2015 },
860 errors: [
861 {
862 messageId: "expected",
863 data: { expectedSide: "right", operator: "<" },
864 type: "BinaryExpression"
865 }
866 ]
867 },
868 {
869 code: "function *foo() { yield/**/1 < a }",
870 output: "function *foo() { yield/**/a > 1 }",
871 options: ["never"],
872 parserOptions: { ecmaVersion: 2015 },
873 errors: [
874 {
875 messageId: "expected",
876 data: { expectedSide: "right", operator: "<" },
877 type: "BinaryExpression"
878 }
879 ]
880 },
881 {
882 code: "function *foo() { yield(1) < ++a }",
883 output: "function *foo() { yield++a > (1) }",
884 options: ["never"],
885 parserOptions: { ecmaVersion: 2015 },
886 errors: [
887 {
888 messageId: "expected",
889 data: { expectedSide: "right", operator: "<" },
890 type: "BinaryExpression"
891 }
892 ]
893 },
894 {
895 code: "function *foo() { yield(1) < (a) }",
896 output: "function *foo() { yield(a) > (1) }",
897 options: ["never"],
898 parserOptions: { ecmaVersion: 2015 },
899 errors: [
900 {
901 messageId: "expected",
902 data: { expectedSide: "right", operator: "<" },
903 type: "BinaryExpression"
904 }
905 ]
906 },
907 {
908 code: "x=1 < a",
909 output: "x=a > 1",
910 options: ["never"],
911 errors: [
912 {
913 messageId: "expected",
914 data: { expectedSide: "right", operator: "<" },
915 type: "BinaryExpression"
916 }
917 ]
918 },
919 {
920 code: "function *foo() { yield++a < 1 }",
921 output: "function *foo() { yield 1 > ++a }",
922 options: ["always"],
923 parserOptions: { ecmaVersion: 2015 },
924 errors: [
925 {
926 messageId: "expected",
927 data: { expectedSide: "left", operator: "<" },
928 type: "BinaryExpression"
929 }
930 ]
931 },
932 {
933 code: "function *foo() { yield(a) < 1 }",
934 output: "function *foo() { yield 1 > (a) }",
935 options: ["always"],
936 parserOptions: { ecmaVersion: 2015 },
937 errors: [
938 {
939 messageId: "expected",
940 data: { expectedSide: "left", operator: "<" },
941 type: "BinaryExpression"
942 }
943 ]
944 },
945 {
946 code: "function *foo() { yield a < 1 }",
947 output: "function *foo() { yield 1 > a }",
948 options: ["always"],
949 parserOptions: { ecmaVersion: 2015 },
950 errors: [
951 {
952 messageId: "expected",
953 data: { expectedSide: "left", operator: "<" },
954 type: "BinaryExpression"
955 }
956 ]
957 },
958 {
959 code: "function *foo() { yield/**/a < 1 }",
960 output: "function *foo() { yield/**/1 > a }",
961 options: ["always"],
962 parserOptions: { ecmaVersion: 2015 },
963 errors: [
964 {
965 messageId: "expected",
966 data: { expectedSide: "left", operator: "<" },
967 type: "BinaryExpression"
968 }
969 ]
970 },
971 {
972 code: "function *foo() { yield++a < (1) }",
973 output: "function *foo() { yield(1) > ++a }",
974 options: ["always"],
975 parserOptions: { ecmaVersion: 2015 },
976 errors: [
977 {
978 messageId: "expected",
979 data: { expectedSide: "left", operator: "<" },
980 type: "BinaryExpression"
981 }
982 ]
983 },
984 {
985 code: "x=a < 1",
986 output: "x=1 > a",
987 options: ["always"],
988 errors: [
989 {
990 messageId: "expected",
991 data: { expectedSide: "left", operator: "<" },
992 type: "BinaryExpression"
993 }
994 ]
995 }
996 ]
997 });