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