]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/curly.js
import 8.3.0 source
[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 // Tests
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 line: 1,
434 column: 10,
435 endLine: 1,
436 endColumn: 15
437 }
438 ]
439 },
440 {
441 code: "if (foo) \n bar()",
442 output: "if (foo) \n {bar()}",
443 errors: [
444 {
445 messageId: "missingCurlyAfterCondition",
446 data: { name: "if" },
447 type: "IfStatement",
448 line: 2,
449 column: 2,
450 endLine: 2,
451 endColumn: 7
452 }
453 ]
454 },
455 {
456 code: "if (foo) { bar() } else baz()",
457 output: "if (foo) { bar() } else {baz()}",
458 errors: [
459 {
460 messageId: "missingCurlyAfter",
461 data: { name: "else" },
462 type: "IfStatement"
463 }
464 ]
465 },
466 {
467 code: "if (foo) { bar() } else if (faa) baz()",
468 output: "if (foo) { bar() } else if (faa) {baz()}",
469 errors: [
470 {
471 messageId: "missingCurlyAfterCondition",
472 data: { name: "if" },
473 type: "IfStatement"
474 }
475 ]
476 },
477 {
478 code: "while (foo) bar()",
479 output: "while (foo) {bar()}",
480 errors: [
481 {
482 messageId: "missingCurlyAfterCondition",
483 data: { name: "while" },
484 type: "WhileStatement",
485 line: 1,
486 column: 13,
487 endLine: 1,
488 endColumn: 18
489 }
490 ]
491 },
492 {
493 code: "while (foo) \n bar()",
494 output: "while (foo) \n {bar()}",
495 errors: [
496 {
497 messageId: "missingCurlyAfterCondition",
498 data: { name: "while" },
499 type: "WhileStatement",
500 line: 2,
501 column: 2,
502 endLine: 2,
503 endColumn: 7
504 }
505 ]
506 },
507 {
508 code: "do bar(); while (foo)",
509 output: "do {bar();} while (foo)",
510 errors: [
511 {
512 messageId: "missingCurlyAfter",
513 data: { name: "do" },
514 type: "DoWhileStatement",
515 line: 1,
516 column: 4,
517 endLine: 1,
518 endColumn: 10
519 }
520 ]
521 },
522 {
523 code: "do \n bar(); while (foo)",
524 output: "do \n {bar();} while (foo)",
525 errors: [
526 {
527 messageId: "missingCurlyAfter",
528 data: { name: "do" },
529 type: "DoWhileStatement",
530 line: 2,
531 column: 2,
532 endLine: 2,
533 endColumn: 8
534 }
535 ]
536 },
537 {
538 code: "for (;foo;) bar()",
539 output: "for (;foo;) {bar()}",
540 errors: [
541 {
542 messageId: "missingCurlyAfterCondition",
543 data: { name: "for" },
544 type: "ForStatement"
545 }
546 ]
547 },
548 {
549 code: "for (var foo in bar) console.log(foo)",
550 output: "for (var foo in bar) {console.log(foo)}",
551 errors: [
552 {
553 messageId: "missingCurlyAfter",
554 data: { name: "for-in" },
555 type: "ForInStatement"
556 }
557 ]
558 },
559 {
560 code: "for (var foo of bar) console.log(foo)",
561 output: "for (var foo of bar) {console.log(foo)}",
562 parserOptions: { ecmaVersion: 6 },
563 errors: [
564 {
565 messageId: "missingCurlyAfter",
566 data: { name: "for-of" },
567 type: "ForOfStatement",
568 line: 1,
569 column: 22,
570 endLine: 1,
571 endColumn: 38
572 }
573 ]
574 },
575 {
576 code: "for (var foo of bar) \n console.log(foo)",
577 output: "for (var foo of bar) \n {console.log(foo)}",
578 parserOptions: { ecmaVersion: 6 },
579 errors: [
580 {
581 messageId: "missingCurlyAfter",
582 data: { name: "for-of" },
583 type: "ForOfStatement",
584 line: 2,
585 column: 2,
586 endLine: 2,
587 endColumn: 18
588 }
589 ]
590 },
591 {
592 code: "for (a;;) console.log(foo)",
593 output: "for (a;;) {console.log(foo)}",
594 parserOptions: { ecmaVersion: 6 },
595 errors: [
596 {
597 messageId: "missingCurlyAfterCondition",
598 data: { name: "for" },
599 type: "ForStatement",
600 line: 1,
601 column: 11,
602 endLine: 1,
603 endColumn: 27
604 }
605 ]
606 },
607 {
608 code: "for (a;;) \n console.log(foo)",
609 output: "for (a;;) \n {console.log(foo)}",
610 parserOptions: { ecmaVersion: 6 },
611 errors: [
612 {
613 messageId: "missingCurlyAfterCondition",
614 data: { name: "for" },
615 type: "ForStatement",
616 line: 2,
617 column: 2,
618 endLine: 2,
619 endColumn: 18
620 }
621 ]
622 },
623 {
624 code: "for (var foo of bar) {console.log(foo)}",
625 output: "for (var foo of bar) console.log(foo)",
626 options: ["multi"],
627 parserOptions: { ecmaVersion: 6 },
628 errors: [
629 {
630 messageId: "unexpectedCurlyAfter",
631 data: { name: "for-of" },
632 type: "ForOfStatement",
633 line: 1,
634 column: 22,
635 endLine: 1,
636 endColumn: 40
637 }
638 ]
639 },
640 {
641 code: "do{foo();} while(bar);",
642 output: "do foo(); while(bar);",
643 options: ["multi"],
644 parserOptions: { ecmaVersion: 6 },
645 errors: [
646 {
647 messageId: "unexpectedCurlyAfter",
648 data: { name: "do" },
649 type: "DoWhileStatement",
650 line: 1,
651 column: 3,
652 endLine: 1,
653 endColumn: 11
654 }
655 ]
656 },
657 {
658 code: "for (;foo;) { bar() }",
659 output: "for (;foo;) bar() ",
660 options: ["multi"],
661 errors: [
662 {
663 messageId: "unexpectedCurlyAfterCondition",
664 data: { name: "for" },
665 type: "ForStatement",
666 line: 1,
667 column: 13,
668 endLine: 1,
669 endColumn: 22
670 }
671 ]
672 },
673 {
674 code: "for (;foo;) \n bar()",
675 output: "for (;foo;) \n {bar()}",
676 errors: [
677 {
678 data: { name: "for" },
679 type: "ForStatement",
680 messageId: "missingCurlyAfterCondition",
681 line: 2,
682 column: 2,
683 endLine: 2,
684 endColumn: 7
685 }
686 ]
687 },
688 {
689 code: "if (foo) { bar() }",
690 output: "if (foo) bar() ",
691 options: ["multi"],
692 errors: [
693 {
694 messageId: "unexpectedCurlyAfterCondition",
695 data: { name: "if" },
696 type: "IfStatement",
697 line: 1,
698 column: 10,
699 endLine: 1,
700 endColumn: 19
701 }
702 ]
703 },
704 {
705 code: "if (foo) if (bar) { baz() }",
706 output: "if (foo) if (bar) baz() ",
707 options: ["multi"],
708 errors: [
709 {
710 messageId: "unexpectedCurlyAfterCondition",
711 data: { name: "if" },
712 type: "IfStatement"
713 }
714 ]
715 },
716 {
717 code: "if (foo) if (bar) baz(); else if (quux) { quuux(); }",
718 output: "if (foo) if (bar) baz(); else if (quux) quuux(); ",
719 options: ["multi"],
720 errors: [
721 {
722 messageId: "unexpectedCurlyAfterCondition",
723 data: { name: "if" },
724 type: "IfStatement"
725 }
726 ]
727 },
728 {
729 code: "while (foo) { bar() }",
730 output: "while (foo) bar() ",
731 options: ["multi"],
732 errors: [
733 {
734 messageId: "unexpectedCurlyAfterCondition",
735 data: { name: "while" },
736 type: "WhileStatement",
737 line: 1,
738 column: 13,
739 endLine: 1,
740 endColumn: 22
741 }
742 ]
743 },
744 {
745 code: "if (foo) baz(); else { bar() }",
746 output: "if (foo) baz(); else bar() ",
747 options: ["multi"],
748 errors: [
749 {
750 messageId: "unexpectedCurlyAfter",
751 data: { name: "else" },
752 type: "IfStatement"
753 }
754 ]
755 },
756 {
757 code: "if (foo) if (bar); else { baz() }",
758 output: "if (foo) if (bar); else baz() ",
759 options: ["multi"],
760 errors: [
761 {
762 messageId: "unexpectedCurlyAfter",
763 data: { name: "else" },
764 type: "IfStatement"
765 }
766 ]
767 },
768 {
769 code: "if (true) { if (false) console.log(1) }",
770 output: "if (true) if (false) console.log(1) ",
771 options: ["multi"],
772 errors: [
773 {
774 messageId: "unexpectedCurlyAfterCondition",
775 data: { name: "if" },
776 type: "IfStatement"
777 }
778 ]
779 },
780 {
781 code: "if (a) { if (b) console.log(1); else console.log(2) } else console.log(3)",
782 output: null,
783 options: ["multi"],
784 errors: [
785 {
786 messageId: "unexpectedCurlyAfterCondition",
787 data: { name: "if" },
788 type: "IfStatement"
789 }
790 ]
791 },
792 {
793 code: [
794 "if (0)",
795 " console.log(0)",
796 "else if (1) {",
797 " console.log(1)",
798 " console.log(1)",
799 "} else {",
800 " if (2)",
801 " console.log(2)",
802 " else",
803 " console.log(3)",
804 "}"
805 ].join("\n"),
806 output: [
807 "if (0)",
808 " console.log(0)",
809 "else if (1) {",
810 " console.log(1)",
811 " console.log(1)",
812 "} else ",
813 " if (2)",
814 " console.log(2)",
815 " else",
816 " console.log(3)",
817 ""
818 ].join("\n"),
819 options: ["multi"],
820 errors: [
821 {
822 messageId: "unexpectedCurlyAfter",
823 data: { name: "else" },
824 type: "IfStatement",
825 line: 6,
826 column: 8,
827 endLine: 11,
828 endColumn: 2
829 }
830 ]
831 },
832 {
833 code: "for (var foo in bar) { console.log(foo) }",
834 output: "for (var foo in bar) console.log(foo) ",
835 options: ["multi"],
836 errors: [
837 {
838 messageId: "unexpectedCurlyAfter",
839 data: { name: "for-in" },
840 type: "ForInStatement",
841 line: 1,
842 column: 22,
843 endLine: 1,
844 endColumn: 42
845 }
846 ]
847 },
848 {
849 code: "for (var foo of bar) { console.log(foo) }",
850 output: "for (var foo of bar) console.log(foo) ",
851 options: ["multi"],
852 parserOptions: { ecmaVersion: 6 },
853 errors: [
854 {
855 messageId: "unexpectedCurlyAfter",
856 data: { name: "for-of" },
857 type: "ForOfStatement"
858 }
859 ]
860 },
861 {
862 code: "if (foo) \n baz()",
863 output: "if (foo) \n {baz()}",
864 options: ["multi-line"],
865 errors: [
866 {
867 messageId: "missingCurlyAfterCondition",
868 data: { name: "if" },
869 type: "IfStatement",
870 line: 2,
871 column: 2,
872 endLine: 2,
873 endColumn: 7
874 }
875 ]
876 },
877 {
878 code: "if (foo) baz()",
879 output: "if (foo) {baz()}",
880 errors: [
881 {
882 messageId: "missingCurlyAfterCondition",
883 data: { name: "if" },
884 type: "IfStatement",
885 line: 1,
886 column: 10,
887 endLine: 1,
888 endColumn: 15
889 }
890 ]
891 },
892 {
893 code: "while (foo) \n baz()",
894 output: "while (foo) \n {baz()}",
895 options: ["multi-line"],
896 errors: [
897 {
898 messageId: "missingCurlyAfterCondition",
899 data: { name: "while" },
900 type: "WhileStatement"
901 }
902 ]
903 },
904 {
905 code: "for (;foo;) \n bar()",
906 output: "for (;foo;) \n {bar()}",
907 options: ["multi-line"],
908 errors: [
909 {
910 messageId: "missingCurlyAfterCondition",
911 data: { name: "for" },
912 type: "ForStatement"
913 }
914 ]
915 },
916 {
917 code: "while (bar && \n baz) \n foo()",
918 output: "while (bar && \n baz) \n {foo()}",
919 options: ["multi-line"],
920 errors: [
921 {
922 messageId: "missingCurlyAfterCondition",
923 data: { name: "while" },
924 type: "WhileStatement"
925 }
926 ]
927 },
928 {
929 code: "if (foo) bar(baz, \n baz)",
930 output: "if (foo) {bar(baz, \n baz)}",
931 options: ["multi-line"],
932 errors: [
933 {
934 messageId: "missingCurlyAfterCondition",
935 data: { name: "if" },
936 type: "IfStatement"
937 }
938 ]
939 },
940 {
941 code: "do foo(); while (bar)",
942 output: "do {foo();} while (bar)",
943 options: ["all"],
944 errors: [
945 {
946 messageId: "missingCurlyAfter",
947 data: { name: "do" },
948 type: "DoWhileStatement",
949 line: 1,
950 column: 4,
951 endLine: 1,
952 endColumn: 10
953 }
954 ]
955 },
956 {
957 code: "do \n foo(); \n while (bar)",
958 output: "do \n {foo();} \n while (bar)",
959 options: ["multi-line"],
960 errors: [
961 {
962 messageId: "missingCurlyAfter",
963 data: { name: "do" },
964 type: "DoWhileStatement",
965 line: 2,
966 column: 2,
967 endLine: 2,
968 endColumn: 8
969 }
970 ]
971 },
972 {
973 code: "for (var foo in bar) {console.log(foo)}",
974 output: "for (var foo in bar) console.log(foo)",
975 options: ["multi"],
976 errors: [
977 {
978 messageId: "unexpectedCurlyAfter",
979 data: { name: "for-in" },
980 type: "ForInStatement",
981 line: 1,
982 column: 22,
983 endLine: 1,
984 endColumn: 40
985 }
986 ]
987 },
988 {
989 code: "for (var foo in bar) \n console.log(foo)",
990 output: "for (var foo in bar) \n {console.log(foo)}",
991 options: ["multi-line"],
992 errors: [
993 {
994 messageId: "missingCurlyAfter",
995 data: { name: "for-in" },
996 type: "ForInStatement"
997 }
998 ]
999 },
1000 {
1001 code: "for (var foo in bar) \n console.log(1); \n console.log(2)",
1002 output: "for (var foo in bar) \n {console.log(1);} \n console.log(2)",
1003 options: ["multi-line"],
1004 errors: [
1005 {
1006 messageId: "missingCurlyAfter",
1007 data: { name: "for-in" },
1008 type: "ForInStatement"
1009 }
1010 ]
1011 },
1012 {
1013 code: "for (var foo of bar) \n console.log(foo)",
1014 output: "for (var foo of bar) \n {console.log(foo)}",
1015 options: ["multi-line"],
1016 parserOptions: { ecmaVersion: 6 },
1017 errors: [
1018 {
1019 messageId: "missingCurlyAfter",
1020 data: { name: "for-of" },
1021 type: "ForOfStatement",
1022 line: 2,
1023 column: 2,
1024 endLine: 2,
1025 endColumn: 18
1026 }
1027 ]
1028 },
1029 {
1030 code: "for (var foo of bar) \n console.log(1); \n console.log(2)",
1031 output: "for (var foo of bar) \n {console.log(1);} \n console.log(2)",
1032 options: ["multi-line"],
1033 parserOptions: { ecmaVersion: 6 },
1034 errors: [
1035 {
1036 messageId: "missingCurlyAfter",
1037 data: { name: "for-of" },
1038 type: "ForOfStatement"
1039 }
1040 ]
1041 },
1042 {
1043 code: "if (foo) \n quz = { \n bar: baz, \n qux: foo \n };",
1044 output: "if (foo) \n {quz = { \n bar: baz, \n qux: foo \n };}",
1045 options: ["multi-or-nest"],
1046 errors: [
1047 {
1048 messageId: "missingCurlyAfterCondition",
1049 data: { name: "if" },
1050 type: "IfStatement"
1051 }
1052 ]
1053 },
1054 {
1055 code: "while (true) \n if (foo) \n doSomething(); \n else \n doSomethingElse(); \n",
1056 output: "while (true) \n {if (foo) \n doSomething(); \n else \n doSomethingElse();} \n",
1057 options: ["multi-or-nest"],
1058 errors: [
1059 {
1060 messageId: "missingCurlyAfterCondition",
1061 data: { name: "while" },
1062 type: "WhileStatement"
1063 }
1064 ]
1065 },
1066 {
1067 code: "if (foo) { \n quz = true; \n }",
1068 output: "if (foo) \n quz = true; \n ",
1069 options: ["multi-or-nest"],
1070 errors: [
1071 {
1072 messageId: "unexpectedCurlyAfterCondition",
1073 data: { name: "if" },
1074 type: "IfStatement"
1075 }
1076 ]
1077 },
1078 {
1079 code: "if (foo) { var bar = 'baz'; }",
1080 output: "if (foo) var bar = 'baz'; ",
1081 options: ["multi"],
1082 errors: [
1083 {
1084 messageId: "unexpectedCurlyAfterCondition",
1085 data: { name: "if" },
1086 type: "IfStatement"
1087 }
1088 ]
1089 },
1090 {
1091 code: "if (foo) { let bar; } else baz();",
1092 output: "if (foo) { let bar; } else {baz();}",
1093 options: ["multi", "consistent"],
1094 parserOptions: { ecmaVersion: 6 },
1095 errors: [
1096 {
1097 messageId: "missingCurlyAfter",
1098 data: { name: "else" },
1099 type: "IfStatement"
1100 }
1101 ]
1102 },
1103 {
1104 code: "if (foo) bar(); else { const baz = 'quux' }",
1105 output: "if (foo) {bar();} else { const baz = 'quux' }",
1106 options: ["multi", "consistent"],
1107 parserOptions: { ecmaVersion: 6 },
1108 errors: [
1109 {
1110 messageId: "missingCurlyAfterCondition",
1111 data: { name: "if" },
1112 type: "IfStatement"
1113 }
1114 ]
1115 },
1116 {
1117 code: "if (foo) { \n var bar = 'baz'; \n }",
1118 output: "if (foo) \n var bar = 'baz'; \n ",
1119 options: ["multi-or-nest"],
1120 errors: [
1121 {
1122 messageId: "unexpectedCurlyAfterCondition",
1123 data: { name: "if" },
1124 type: "IfStatement"
1125 }
1126 ]
1127 },
1128 {
1129 code: "while (true) { \n doSomething(); \n }",
1130 output: "while (true) \n doSomething(); \n ",
1131 options: ["multi-or-nest"],
1132 errors: [
1133 {
1134 messageId: "unexpectedCurlyAfterCondition",
1135 data: { name: "while" },
1136 type: "WhileStatement"
1137 }
1138 ]
1139 },
1140 {
1141 code: "for (var i = 0; foo; i++) { \n doSomething(); \n }",
1142 output: "for (var i = 0; foo; i++) \n doSomething(); \n ",
1143 options: ["multi-or-nest"],
1144 errors: [
1145 {
1146 messageId: "unexpectedCurlyAfterCondition",
1147 data: { name: "for" },
1148 type: "ForStatement",
1149 line: 1,
1150 column: 27,
1151 endLine: 3,
1152 endColumn: 3
1153 }
1154 ]
1155 },
1156 {
1157 code: "for (var foo in bar) if (foo) console.log(1); else console.log(2);",
1158 output: "for (var foo in bar) {if (foo) console.log(1); else console.log(2);}",
1159 options: ["all"],
1160 errors: [
1161 {
1162 messageId: "missingCurlyAfter",
1163 data: { name: "for-in" },
1164 type: "ForInStatement",
1165 line: 1,
1166 column: 22,
1167 endLine: 1,
1168 endColumn: 67
1169 },
1170 {
1171 messageId: "missingCurlyAfterCondition",
1172 data: { name: "if" },
1173 type: "IfStatement",
1174 line: 1,
1175 column: 31,
1176 endLine: 1,
1177 endColumn: 46
1178 },
1179 {
1180 messageId: "missingCurlyAfter",
1181 data: { name: "else" },
1182 type: "IfStatement",
1183 line: 1,
1184 column: 52,
1185 endLine: 1,
1186 endColumn: 67
1187 }
1188 ]
1189 },
1190 {
1191 code: "for (var foo in bar) \n if (foo) console.log(1); \n else console.log(2);",
1192 output: "for (var foo in bar) \n {if (foo) console.log(1); \n else console.log(2);}",
1193 options: ["multi-or-nest"],
1194 errors: [
1195 {
1196 messageId: "missingCurlyAfter",
1197 data: { name: "for-in" },
1198 type: "ForInStatement",
1199 line: 2,
1200 column: 2,
1201 endLine: 3,
1202 endColumn: 22
1203 }
1204 ]
1205 },
1206 {
1207 code: "for (var foo in bar) { if (foo) console.log(1) }",
1208 output: "for (var foo in bar) if (foo) console.log(1) ",
1209 options: ["multi-or-nest"],
1210 errors: [
1211 {
1212 messageId: "unexpectedCurlyAfter",
1213 data: { name: "for-in" },
1214 type: "ForInStatement"
1215 }
1216 ]
1217 },
1218 {
1219 code: "for (var foo of bar) \n if (foo) console.log(1); \n else console.log(2);",
1220 output: "for (var foo of bar) \n {if (foo) console.log(1); \n else console.log(2);}",
1221 options: ["multi-or-nest"],
1222 parserOptions: { ecmaVersion: 6 },
1223 errors: [
1224 {
1225 messageId: "missingCurlyAfter",
1226 data: { name: "for-of" },
1227 type: "ForOfStatement"
1228 }
1229 ]
1230 },
1231 {
1232 code: "for (var foo of bar) { if (foo) console.log(1) }",
1233 output: "for (var foo of bar) if (foo) console.log(1) ",
1234 options: ["multi-or-nest"],
1235 parserOptions: { ecmaVersion: 6 },
1236 errors: [
1237 {
1238 messageId: "unexpectedCurlyAfter",
1239 data: { name: "for-of" },
1240 type: "ForOfStatement"
1241 }
1242 ]
1243 },
1244 {
1245 code: "if (true) foo(); \n else { \n bar(); \n baz(); \n }",
1246 output: "if (true) {foo();} \n else { \n bar(); \n baz(); \n }",
1247 options: ["multi", "consistent"],
1248 errors: [
1249 {
1250 messageId: "missingCurlyAfterCondition",
1251 data: { name: "if" },
1252 type: "IfStatement"
1253 }
1254 ]
1255 },
1256 {
1257 code: "if (true) { foo(); faa(); }\n else bar();",
1258 output: "if (true) { foo(); faa(); }\n else {bar();}",
1259 options: ["multi", "consistent"],
1260 errors: [
1261 {
1262 messageId: "missingCurlyAfter",
1263 data: { name: "else" },
1264 type: "IfStatement"
1265 }
1266 ]
1267 },
1268 {
1269 code: "if (true) foo(); else { baz(); }",
1270 output: "if (true) foo(); else baz(); ",
1271 options: ["multi", "consistent"],
1272 errors: [
1273 {
1274 messageId: "unexpectedCurlyAfter",
1275 data: { name: "else" },
1276 type: "IfStatement",
1277 line: 1,
1278 column: 23,
1279 endLine: 1,
1280 endColumn: 33
1281 }
1282 ]
1283 },
1284 {
1285 code: "if (true) foo(); else if (true) faa(); else { bar(); baz(); }",
1286 output: "if (true) {foo();} else if (true) {faa();} else { bar(); baz(); }",
1287 options: ["multi", "consistent"],
1288 errors: [
1289 {
1290 messageId: "missingCurlyAfterCondition",
1291 data: { name: "if" },
1292 type: "IfStatement"
1293 },
1294 {
1295 messageId: "missingCurlyAfterCondition",
1296 data: { name: "if" },
1297 type: "IfStatement"
1298 }
1299 ]
1300 },
1301 {
1302 code: "if (true) if (true) foo(); else { bar(); baz(); }",
1303 output: "if (true) if (true) {foo();} else { bar(); baz(); }",
1304 options: ["multi", "consistent"],
1305 errors: [
1306 {
1307 messageId: "missingCurlyAfterCondition",
1308 data: { name: "if" },
1309 type: "IfStatement"
1310 }
1311 ]
1312 },
1313 {
1314 code: "do{foo();} while (bar)",
1315 output: "do foo(); while (bar)",
1316 options: ["multi"],
1317 errors: [
1318 {
1319 messageId: "unexpectedCurlyAfter",
1320 data: { name: "do" },
1321 type: "DoWhileStatement"
1322 }
1323 ]
1324 },
1325 {
1326 code: "do\n{foo();} while (bar)",
1327 output: "do\nfoo(); while (bar)",
1328 options: ["multi"],
1329 errors: [
1330 {
1331 messageId: "unexpectedCurlyAfter",
1332 data: { name: "do" },
1333 type: "DoWhileStatement",
1334 line: 2,
1335 column: 1,
1336 endLine: 2,
1337 endColumn: 9
1338 }
1339 ]
1340 },
1341 {
1342 code: "while (bar) { foo(); }",
1343 output: "while (bar) foo(); ",
1344 options: ["multi"],
1345 errors: [
1346 {
1347 messageId: "unexpectedCurlyAfterCondition",
1348 data: { name: "while" },
1349 type: "WhileStatement",
1350 line: 1,
1351 column: 13,
1352 endLine: 1,
1353 endColumn: 23
1354 }
1355 ]
1356 },
1357 {
1358 code: "while (bar) \n{\n foo(); }",
1359 output: "while (bar) \n\n foo(); ",
1360 options: ["multi"],
1361 errors: [
1362 {
1363 messageId: "unexpectedCurlyAfterCondition",
1364 data: { name: "while" },
1365 type: "WhileStatement",
1366 line: 2,
1367 column: 1,
1368 endLine: 3,
1369 endColumn: 10
1370 }
1371 ]
1372 },
1373 {
1374 code: "for (;;) { foo(); }",
1375 output: "for (;;) foo(); ",
1376 options: ["multi"],
1377 errors: [
1378 {
1379 messageId: "unexpectedCurlyAfterCondition",
1380 data: { name: "for" },
1381 type: "ForStatement",
1382 line: 1,
1383 column: 10,
1384 endLine: 1,
1385 endColumn: 20
1386 }
1387 ]
1388 },
1389 {
1390 code: "do{[1, 2, 3].map(bar);} while (bar)",
1391 output: "do[1, 2, 3].map(bar); while (bar)",
1392 options: ["multi"],
1393 errors: [
1394 {
1395 messageId: "unexpectedCurlyAfter",
1396 data: { name: "do" },
1397 type: "DoWhileStatement"
1398 }
1399 ]
1400 },
1401 {
1402 code: "if (foo) {bar()} baz()",
1403 output: null,
1404 options: ["multi"],
1405 errors: [
1406 {
1407 messageId: "unexpectedCurlyAfterCondition",
1408 data: { name: "if" },
1409 type: "IfStatement"
1410 }
1411 ]
1412 },
1413 {
1414 code: "do {foo();} while (bar)",
1415 output: "do foo(); while (bar)",
1416 options: ["multi"],
1417 errors: [
1418 {
1419 messageId: "unexpectedCurlyAfter",
1420 data: { name: "do" },
1421 type: "DoWhileStatement"
1422 }
1423 ]
1424 },
1425
1426 // Don't remove curly braces if it would cause issues due to ASI.
1427 {
1428 code: "if (foo) { bar }\n++baz;",
1429 output: null,
1430 options: ["multi"],
1431 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1432 },
1433 {
1434 code: "if (foo) { bar; }\n++baz;",
1435 output: "if (foo) bar; \n++baz;",
1436 options: ["multi"],
1437 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1438 },
1439 {
1440 code: "if (foo) { bar++ }\nbaz;",
1441 output: null,
1442 options: ["multi"],
1443 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1444 },
1445 {
1446 code: "if (foo) { bar }\n[1, 2, 3].map(foo);",
1447 output: null,
1448 options: ["multi"],
1449 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1450 },
1451 {
1452 code: "if (foo) { bar }\n(1).toString();",
1453 output: null,
1454 options: ["multi"],
1455 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1456 },
1457 {
1458 code: "if (foo) { bar }\n/regex/.test('foo');",
1459 output: null,
1460 options: ["multi"],
1461 parserOptions: { ecmaVersion: 6 },
1462 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1463 },
1464 {
1465 code: "if (foo) { bar }\nBaz();",
1466 output: "if (foo) bar \nBaz();",
1467 options: ["multi"],
1468 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1469 },
1470 {
1471 code:
1472 "if (a) {\n" +
1473 " while (b) {\n" +
1474 " c();\n" +
1475 " d();\n" +
1476 " }\n" +
1477 "} else e();",
1478 output:
1479 "if (a) \n" +
1480 " while (b) {\n" +
1481 " c();\n" +
1482 " d();\n" +
1483 " }\n" +
1484 " else e();",
1485 options: ["multi"],
1486 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1487 },
1488 {
1489 code: "if (foo) { while (bar) {} } else {}",
1490 output: "if (foo) while (bar) {} else {}",
1491 options: ["multi"],
1492 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1493 },
1494 {
1495 code: "if (foo) { var foo = () => {} } else {}",
1496 output: null,
1497 options: ["multi"],
1498 parserOptions: { ecmaVersion: 6 },
1499 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1500 },
1501 {
1502 code: "if (foo) { var foo = function() {} } else {}",
1503 output: null,
1504 options: ["multi"],
1505 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1506 },
1507 {
1508 code: "if (foo) { var foo = function*() {} } else {}",
1509 output: null,
1510 options: ["multi"],
1511 parserOptions: { ecmaVersion: 6 },
1512 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1513 },
1514 {
1515 code: "if (true)\nfoo()\n;[1, 2, 3].bar()",
1516 output: "if (true)\n{foo()\n;}[1, 2, 3].bar()",
1517 options: ["multi-line"],
1518 errors: [{ messageId: "missingCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1519 },
1520
1521 // https://github.com/eslint/eslint/issues/12370
1522 {
1523 code: "if (foo) {\ndoSomething()\n;\n}",
1524 output: "if (foo) \ndoSomething()\n;\n",
1525 options: ["multi-or-nest"],
1526 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1527 },
1528 {
1529 code: "if (foo) doSomething();\nelse if (bar) {\ndoSomethingElse()\n;\n}",
1530 output: "if (foo) doSomething();\nelse if (bar) \ndoSomethingElse()\n;\n",
1531 options: ["multi-or-nest"],
1532 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1533 },
1534 {
1535 code: "if (foo) doSomething();\nelse {\ndoSomethingElse()\n;\n}",
1536 output: "if (foo) doSomething();\nelse \ndoSomethingElse()\n;\n",
1537 options: ["multi-or-nest"],
1538 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "else" }, type: "IfStatement" }]
1539 },
1540 {
1541 code: "for (var i = 0; foo; i++) {\ndoSomething()\n;\n}",
1542 output: "for (var i = 0; foo; i++) \ndoSomething()\n;\n",
1543 options: ["multi-or-nest"],
1544 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "for" }, type: "ForStatement" }]
1545 },
1546 {
1547 code: "for (var foo in bar) {\ndoSomething()\n;\n}",
1548 output: "for (var foo in bar) \ndoSomething()\n;\n",
1549 options: ["multi-or-nest"],
1550 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-in" }, type: "ForInStatement" }]
1551 },
1552 {
1553 code: "for (var foo of bar) {\ndoSomething()\n;\n}",
1554 output: "for (var foo of bar) \ndoSomething()\n;\n",
1555 options: ["multi-or-nest"],
1556 parserOptions: { ecmaVersion: 6 },
1557 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-of" }, type: "ForOfStatement" }]
1558 },
1559 {
1560 code: "while (foo) {\ndoSomething()\n;\n}",
1561 output: "while (foo) \ndoSomething()\n;\n",
1562 options: ["multi-or-nest"],
1563 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1564 },
1565 {
1566 code: "do {\ndoSomething()\n;\n} while (foo)",
1567 output: "do \ndoSomething()\n;\n while (foo)",
1568 options: ["multi-or-nest"],
1569 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "do" }, type: "DoWhileStatement" }]
1570 },
1571
1572 // https://github.com/eslint/eslint/issues/12928 (also in valid[])
1573 {
1574 code: "if (a) { if (b) foo(); }",
1575 output: "if (a) if (b) foo(); ",
1576 options: ["multi"],
1577 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1578 },
1579 {
1580 code: "if (a) { if (b) foo(); else bar(); }",
1581 output: "if (a) if (b) foo(); else bar(); ",
1582 options: ["multi"],
1583 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1584 },
1585 {
1586 code: "if (a) { if (b) foo(); else bar(); } baz();",
1587 output: "if (a) if (b) foo(); else bar(); baz();",
1588 options: ["multi"],
1589 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1590 },
1591 {
1592 code: "if (a) { while (cond) if (b) foo(); }",
1593 output: "if (a) while (cond) if (b) foo(); ",
1594 options: ["multi"],
1595 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1596 },
1597 {
1598 code: "if (a) while (cond) { if (b) foo(); }",
1599 output: "if (a) while (cond) if (b) foo(); ",
1600 options: ["multi"],
1601 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1602 },
1603 {
1604 code: "if (a) while (cond) { if (b) foo(); else bar(); }",
1605 output: "if (a) while (cond) if (b) foo(); else bar(); ",
1606 options: ["multi"],
1607 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1608 },
1609 {
1610 code: "if (a) { while (cond) { if (b) foo(); } bar(); baz() } else quux();",
1611 output: "if (a) { while (cond) if (b) foo(); bar(); baz() } else quux();",
1612 options: ["multi"],
1613 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1614 },
1615 {
1616 code: "if (a) { if (b) foo(); } bar();",
1617 output: "if (a) if (b) foo(); bar();",
1618 options: ["multi"],
1619 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1620 },
1621 {
1622 code: "if(a) { if (b) foo(); } if (c) bar(); else baz();",
1623 output: "if(a) if (b) foo(); if (c) bar(); else baz();",
1624 options: ["multi-or-nest"],
1625 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1626 },
1627 {
1628 code: "if (a) { do if (b) foo(); while (cond); } else bar();",
1629 output: "if (a) do if (b) foo(); while (cond); else bar();",
1630 options: ["multi"],
1631 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1632 },
1633 {
1634 code: "if (a) do { if (b) foo(); } while (cond); else bar();",
1635 output: "if (a) do if (b) foo(); while (cond); else bar();",
1636 options: ["multi"],
1637 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "do" }, type: "DoWhileStatement" }]
1638 },
1639 {
1640 code: "if (a) { if (b) foo(); else bar(); } else baz();",
1641 output: "if (a) if (b) foo(); else bar(); else baz();",
1642 options: ["multi"],
1643 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1644 },
1645 {
1646 code: "if (a) while (cond) { bar(); } else baz();",
1647 output: "if (a) while (cond) bar(); else baz();",
1648 options: ["multi"],
1649 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1650 },
1651 {
1652 code: "if (a) { for (;;); } else bar();",
1653 output: "if (a) for (;;); else bar();",
1654 options: ["multi"],
1655 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1656 },
1657 {
1658 code: "if (a) { while (cond) if (b) foo() } else bar();",
1659 output: "if (a) { while (cond) if (b) foo() } else {bar();}",
1660 options: ["multi", "consistent"],
1661 errors: [
1662 {
1663 messageId: "missingCurlyAfter",
1664 data: { name: "else" },
1665 type: "IfStatement",
1666 line: 1,
1667 column: 43,
1668 endLine: 1,
1669 endColumn: 49
1670 }
1671 ]
1672 },
1673 {
1674 code: "if (a) while (cond) if (b) foo() \nelse\n {bar();}",
1675 output: "if (a) while (cond) if (b) foo() \nelse\n bar();",
1676 options: ["multi", "consistent"],
1677 errors: [
1678 {
1679 messageId: "unexpectedCurlyAfter",
1680 data: { name: "else" },
1681 type: "IfStatement",
1682 line: 3,
1683 column: 2,
1684 endLine: 3,
1685 endColumn: 10
1686 }
1687 ]
1688 },
1689 {
1690 code: "if (a) foo() \nelse\n bar();",
1691 output: "if (a) {foo()} \nelse\n {bar();}",
1692 errors: [{
1693 type: "IfStatement",
1694 messageId: "missingCurlyAfterCondition",
1695 line: 1,
1696 column: 8,
1697 endLine: 1,
1698 endColumn: 13
1699 },
1700 {
1701 type: "IfStatement",
1702 messageId: "missingCurlyAfter",
1703 line: 3,
1704 column: 2,
1705 endLine: 3,
1706 endColumn: 8
1707 }]
1708 },
1709 {
1710 code: "if (a) { while (cond) if (b) foo() } ",
1711 output: "if (a) while (cond) if (b) foo() ",
1712 options: ["multi", "consistent"],
1713 errors: [{
1714 messageId: "unexpectedCurlyAfterCondition",
1715 data: { name: "if" },
1716 type: "IfStatement",
1717 line: 1,
1718 column: 8,
1719 endLine: 1,
1720 endColumn: 37
1721 }]
1722 },
1723 {
1724 code: "if(a) { if (b) foo(); } if (c) bar(); else if(foo){bar();}",
1725 output: "if(a) if (b) foo(); if (c) bar(); else if(foo)bar();",
1726 options: ["multi-or-nest"],
1727 errors: [{
1728 type: "IfStatement",
1729 data: { name: "if" },
1730 messageId: "unexpectedCurlyAfterCondition",
1731 line: 1,
1732 column: 7,
1733 endLine: 1,
1734 endColumn: 24
1735 },
1736 {
1737 type: "IfStatement",
1738 data: { name: "if" },
1739 messageId: "unexpectedCurlyAfterCondition",
1740 line: 1,
1741 column: 51,
1742 endLine: 1,
1743 endColumn: 59
1744 }]
1745 },
1746 {
1747 code: "if (true) [1, 2, 3]\n.bar()",
1748 output: "if (true) {[1, 2, 3]\n.bar()}",
1749 options: ["multi-line"],
1750 errors: [{
1751 data: { name: "if" },
1752 type: "IfStatement",
1753 messageId: "missingCurlyAfterCondition",
1754 line: 1,
1755 column: 11,
1756 endLine: 2,
1757 endColumn: 7
1758 }]
1759 },
1760 {
1761 code: "for(\n;\n;\n) {foo()}",
1762 output: "for(\n;\n;\n) foo()",
1763 options: ["multi"],
1764 errors: [{
1765 data: { name: "for" },
1766 type: "ForStatement",
1767 messageId: "unexpectedCurlyAfterCondition",
1768 line: 4,
1769 column: 3,
1770 endLine: 4,
1771 endColumn: 10
1772 }]
1773 },
1774 {
1775 code: "for(\n;\n;\n) \nfoo()\n",
1776 output: "for(\n;\n;\n) \n{foo()}\n",
1777 options: ["multi-line"],
1778 errors: [{
1779 data: { name: "for" },
1780 type: "ForStatement",
1781 messageId: "missingCurlyAfterCondition",
1782 line: 5,
1783 column: 1,
1784 endLine: 5,
1785 endColumn: 6
1786 }]
1787 },
1788 {
1789
1790 /**
1791 * Reports 2 errors, but one pair of braces is necessary if the other pair gets removed.
1792 * Auto-fix will remove only outer braces in the first iteration.
1793 * After that, the inner braces will become valid and won't be removed in the second iteration.
1794 * If user manually removes inner braces first, the outer braces will become valid.
1795 */
1796 code: "if (a) { while (cond) { if (b) foo(); } } else bar();",
1797 output: "if (a) while (cond) { if (b) foo(); } else bar();",
1798 options: ["multi"],
1799 errors: [
1800 { messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" },
1801 { messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }
1802 ]
1803 },
1804 {
1805 code: "for(;;)foo()\n",
1806 output: "for(;;){foo()}\n",
1807 errors: [{
1808 data: { name: "for" },
1809 type: "ForStatement",
1810 messageId: "missingCurlyAfterCondition",
1811 line: 1,
1812 column: 8,
1813 endLine: 1,
1814 endColumn: 13
1815 }]
1816 },
1817 {
1818 code: "for(var \ni \n in \n z)foo()\n",
1819 output: "for(var \ni \n in \n z){foo()}\n",
1820 errors: [{
1821 data: { name: "for-in" },
1822 type: "ForInStatement",
1823 messageId: "missingCurlyAfter",
1824 line: 4,
1825 column: 4,
1826 endLine: 4,
1827 endColumn: 9
1828 }]
1829 },
1830 {
1831 code: "for(var i of \n z)\nfoo()\n",
1832 output: "for(var i of \n z)\n{foo()}\n",
1833 parserOptions: { ecmaVersion: 6 },
1834 errors: [{
1835 data: { name: "for-of" },
1836 type: "ForOfStatement",
1837 messageId: "missingCurlyAfter",
1838 line: 3,
1839 column: 1,
1840 endLine: 3,
1841 endColumn: 6
1842 }]
1843 }
1844 ]
1845 });