]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/curly.js
1d5ee8c6e4f328e858f183e93e8925fe26e5f44a
[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: "while (foo) { bar() }",
540 output: "while (foo) bar() ",
541 options: ["multi"],
542 errors: [
543 {
544 messageId: "unexpectedCurlyAfterCondition",
545 data: { name: "while" },
546 type: "WhileStatement"
547 }
548 ]
549 },
550 {
551 code: "if (foo) baz(); else { bar() }",
552 output: "if (foo) baz(); else bar() ",
553 options: ["multi"],
554 errors: [
555 {
556 messageId: "unexpectedCurlyAfter",
557 data: { name: "else" },
558 type: "IfStatement"
559 }
560 ]
561 },
562 {
563 code: "if (true) { if (false) console.log(1) }",
564 output: "if (true) if (false) console.log(1) ",
565 options: ["multi"],
566 errors: [
567 {
568 messageId: "unexpectedCurlyAfterCondition",
569 data: { name: "if" },
570 type: "IfStatement"
571 }
572 ]
573 },
574 {
575 code: "if (a) { if (b) console.log(1); else console.log(2) } else console.log(3)",
576 output: null,
577 options: ["multi"],
578 errors: [
579 {
580 messageId: "unexpectedCurlyAfterCondition",
581 data: { name: "if" },
582 type: "IfStatement"
583 }
584 ]
585 },
586 {
587 code: [
588 "if (0)",
589 " console.log(0)",
590 "else if (1) {",
591 " console.log(1)",
592 " console.log(1)",
593 "} else {",
594 " if (2)",
595 " console.log(2)",
596 " else",
597 " console.log(3)",
598 "}"
599 ].join("\n"),
600 output: [
601 "if (0)",
602 " console.log(0)",
603 "else if (1) {",
604 " console.log(1)",
605 " console.log(1)",
606 "} else ",
607 " if (2)",
608 " console.log(2)",
609 " else",
610 " console.log(3)",
611 ""
612 ].join("\n"),
613 options: ["multi"],
614 errors: [
615 {
616 messageId: "unexpectedCurlyAfter",
617 data: { name: "else" },
618 type: "IfStatement",
619 line: 6,
620 column: 3
621 }
622 ]
623 },
624 {
625 code: "for (var foo in bar) { console.log(foo) }",
626 output: "for (var foo in bar) console.log(foo) ",
627 options: ["multi"],
628 errors: [
629 {
630 messageId: "unexpectedCurlyAfter",
631 data: { name: "for-in" },
632 type: "ForInStatement"
633 }
634 ]
635 },
636 {
637 code: "for (var foo of bar) { console.log(foo) }",
638 output: "for (var foo of bar) console.log(foo) ",
639 options: ["multi"],
640 parserOptions: { ecmaVersion: 6 },
641 errors: [
642 {
643 messageId: "unexpectedCurlyAfter",
644 data: { name: "for-of" },
645 type: "ForOfStatement"
646 }
647 ]
648 },
649 {
650 code: "if (foo) \n baz()",
651 output: "if (foo) \n {baz()}",
652 options: ["multi-line"],
653 errors: [
654 {
655 messageId: "missingCurlyAfterCondition",
656 data: { name: "if" },
657 type: "IfStatement"
658 }
659 ]
660 },
661 {
662 code: "while (foo) \n baz()",
663 output: "while (foo) \n {baz()}",
664 options: ["multi-line"],
665 errors: [
666 {
667 messageId: "missingCurlyAfterCondition",
668 data: { name: "while" },
669 type: "WhileStatement"
670 }
671 ]
672 },
673 {
674 code: "for (;foo;) \n bar()",
675 output: "for (;foo;) \n {bar()}",
676 options: ["multi-line"],
677 errors: [
678 {
679 messageId: "missingCurlyAfterCondition",
680 data: { name: "for" },
681 type: "ForStatement"
682 }
683 ]
684 },
685 {
686 code: "while (bar && \n baz) \n foo()",
687 output: "while (bar && \n baz) \n {foo()}",
688 options: ["multi-line"],
689 errors: [
690 {
691 messageId: "missingCurlyAfterCondition",
692 data: { name: "while" },
693 type: "WhileStatement"
694 }
695 ]
696 },
697 {
698 code: "if (foo) bar(baz, \n baz)",
699 output: "if (foo) {bar(baz, \n baz)}",
700 options: ["multi-line"],
701 errors: [
702 {
703 messageId: "missingCurlyAfterCondition",
704 data: { name: "if" },
705 type: "IfStatement"
706 }
707 ]
708 },
709 {
710 code: "do \n foo(); \n while (bar)",
711 output: "do \n {foo();} \n while (bar)",
712 options: ["multi-line"],
713 errors: [
714 {
715 messageId: "missingCurlyAfter",
716 data: { name: "do" },
717 type: "DoWhileStatement"
718 }
719 ]
720 },
721 {
722 code: "for (var foo in bar) \n console.log(foo)",
723 output: "for (var foo in bar) \n {console.log(foo)}",
724 options: ["multi-line"],
725 errors: [
726 {
727 messageId: "missingCurlyAfter",
728 data: { name: "for-in" },
729 type: "ForInStatement"
730 }
731 ]
732 },
733 {
734 code: "for (var foo in bar) \n console.log(1); \n console.log(2)",
735 output: "for (var foo in bar) \n {console.log(1);} \n console.log(2)",
736 options: ["multi-line"],
737 errors: [
738 {
739 messageId: "missingCurlyAfter",
740 data: { name: "for-in" },
741 type: "ForInStatement"
742 }
743 ]
744 },
745 {
746 code: "for (var foo of bar) \n console.log(foo)",
747 output: "for (var foo of bar) \n {console.log(foo)}",
748 options: ["multi-line"],
749 parserOptions: { ecmaVersion: 6 },
750 errors: [
751 {
752 messageId: "missingCurlyAfter",
753 data: { name: "for-of" },
754 type: "ForOfStatement"
755 }
756 ]
757 },
758 {
759 code: "for (var foo of bar) \n console.log(1); \n console.log(2)",
760 output: "for (var foo of bar) \n {console.log(1);} \n console.log(2)",
761 options: ["multi-line"],
762 parserOptions: { ecmaVersion: 6 },
763 errors: [
764 {
765 messageId: "missingCurlyAfter",
766 data: { name: "for-of" },
767 type: "ForOfStatement"
768 }
769 ]
770 },
771 {
772 code: "if (foo) \n quz = { \n bar: baz, \n qux: foo \n };",
773 output: "if (foo) \n {quz = { \n bar: baz, \n qux: foo \n };}",
774 options: ["multi-or-nest"],
775 errors: [
776 {
777 messageId: "missingCurlyAfterCondition",
778 data: { name: "if" },
779 type: "IfStatement"
780 }
781 ]
782 },
783 {
784 code: "while (true) \n if (foo) \n doSomething(); \n else \n doSomethingElse(); \n",
785 output: "while (true) \n {if (foo) \n doSomething(); \n else \n doSomethingElse();} \n",
786 options: ["multi-or-nest"],
787 errors: [
788 {
789 messageId: "missingCurlyAfterCondition",
790 data: { name: "while" },
791 type: "WhileStatement"
792 }
793 ]
794 },
795 {
796 code: "if (foo) { \n quz = true; \n }",
797 output: "if (foo) \n quz = true; \n ",
798 options: ["multi-or-nest"],
799 errors: [
800 {
801 messageId: "unexpectedCurlyAfterCondition",
802 data: { name: "if" },
803 type: "IfStatement"
804 }
805 ]
806 },
807 {
808 code: "if (foo) { var bar = 'baz'; }",
809 output: "if (foo) var bar = 'baz'; ",
810 options: ["multi"],
811 errors: [
812 {
813 messageId: "unexpectedCurlyAfterCondition",
814 data: { name: "if" },
815 type: "IfStatement"
816 }
817 ]
818 },
819 {
820 code: "if (foo) { let bar; } else baz();",
821 output: "if (foo) { let bar; } else {baz();}",
822 options: ["multi", "consistent"],
823 parserOptions: { ecmaVersion: 6 },
824 errors: [
825 {
826 messageId: "missingCurlyAfter",
827 data: { name: "else" },
828 type: "IfStatement"
829 }
830 ]
831 },
832 {
833 code: "if (foo) bar(); else { const baz = 'quux' }",
834 output: "if (foo) {bar();} else { const baz = 'quux' }",
835 options: ["multi", "consistent"],
836 parserOptions: { ecmaVersion: 6 },
837 errors: [
838 {
839 messageId: "missingCurlyAfterCondition",
840 data: { name: "if" },
841 type: "IfStatement"
842 }
843 ]
844 },
845 {
846 code: "if (foo) { \n var bar = 'baz'; \n }",
847 output: "if (foo) \n var bar = 'baz'; \n ",
848 options: ["multi-or-nest"],
849 errors: [
850 {
851 messageId: "unexpectedCurlyAfterCondition",
852 data: { name: "if" },
853 type: "IfStatement"
854 }
855 ]
856 },
857 {
858 code: "while (true) { \n doSomething(); \n }",
859 output: "while (true) \n doSomething(); \n ",
860 options: ["multi-or-nest"],
861 errors: [
862 {
863 messageId: "unexpectedCurlyAfterCondition",
864 data: { name: "while" },
865 type: "WhileStatement"
866 }
867 ]
868 },
869 {
870 code: "for (var i = 0; foo; i++) { \n doSomething(); \n }",
871 output: "for (var i = 0; foo; i++) \n doSomething(); \n ",
872 options: ["multi-or-nest"],
873 errors: [
874 {
875 messageId: "unexpectedCurlyAfterCondition",
876 data: { name: "for" },
877 type: "ForStatement"
878 }
879 ]
880 },
881 {
882 code: "for (var foo in bar) \n if (foo) console.log(1); \n else console.log(2);",
883 output: "for (var foo in bar) \n {if (foo) console.log(1); \n else console.log(2);}",
884 options: ["multi-or-nest"],
885 errors: [
886 {
887 messageId: "missingCurlyAfter",
888 data: { name: "for-in" },
889 type: "ForInStatement"
890 }
891 ]
892 },
893 {
894 code: "for (var foo in bar) { if (foo) console.log(1) }",
895 output: "for (var foo in bar) if (foo) console.log(1) ",
896 options: ["multi-or-nest"],
897 errors: [
898 {
899 messageId: "unexpectedCurlyAfter",
900 data: { name: "for-in" },
901 type: "ForInStatement"
902 }
903 ]
904 },
905 {
906 code: "for (var foo of bar) \n if (foo) console.log(1); \n else console.log(2);",
907 output: "for (var foo of bar) \n {if (foo) console.log(1); \n else console.log(2);}",
908 options: ["multi-or-nest"],
909 parserOptions: { ecmaVersion: 6 },
910 errors: [
911 {
912 messageId: "missingCurlyAfter",
913 data: { name: "for-of" },
914 type: "ForOfStatement"
915 }
916 ]
917 },
918 {
919 code: "for (var foo of bar) { if (foo) console.log(1) }",
920 output: "for (var foo of bar) if (foo) console.log(1) ",
921 options: ["multi-or-nest"],
922 parserOptions: { ecmaVersion: 6 },
923 errors: [
924 {
925 messageId: "unexpectedCurlyAfter",
926 data: { name: "for-of" },
927 type: "ForOfStatement"
928 }
929 ]
930 },
931 {
932 code: "if (true) foo(); \n else { \n bar(); \n baz(); \n }",
933 output: "if (true) {foo();} \n else { \n bar(); \n baz(); \n }",
934 options: ["multi", "consistent"],
935 errors: [
936 {
937 messageId: "missingCurlyAfterCondition",
938 data: { name: "if" },
939 type: "IfStatement"
940 }
941 ]
942 },
943 {
944 code: "if (true) { foo(); faa(); }\n else bar();",
945 output: "if (true) { foo(); faa(); }\n else {bar();}",
946 options: ["multi", "consistent"],
947 errors: [
948 {
949 messageId: "missingCurlyAfter",
950 data: { name: "else" },
951 type: "IfStatement"
952 }
953 ]
954 },
955 {
956 code: "if (true) foo(); else { baz(); }",
957 output: "if (true) foo(); else baz(); ",
958 options: ["multi", "consistent"],
959 errors: [
960 {
961 messageId: "unexpectedCurlyAfter",
962 data: { name: "else" },
963 type: "IfStatement"
964 }
965 ]
966 },
967 {
968 code: "if (true) foo(); else if (true) faa(); else { bar(); baz(); }",
969 output: "if (true) {foo();} else if (true) {faa();} else { bar(); baz(); }",
970 options: ["multi", "consistent"],
971 errors: [
972 {
973 messageId: "missingCurlyAfterCondition",
974 data: { name: "if" },
975 type: "IfStatement"
976 },
977 {
978 messageId: "missingCurlyAfterCondition",
979 data: { name: "if" },
980 type: "IfStatement"
981 }
982 ]
983 },
984 {
985 code: "do{foo();} while (bar)",
986 output: "do foo(); while (bar)",
987 options: ["multi"],
988 errors: [
989 {
990 messageId: "unexpectedCurlyAfter",
991 data: { name: "do" },
992 type: "DoWhileStatement"
993 }
994 ]
995 },
996 {
997 code: "do{[1, 2, 3].map(bar);} while (bar)",
998 output: "do[1, 2, 3].map(bar); while (bar)",
999 options: ["multi"],
1000 errors: [
1001 {
1002 messageId: "unexpectedCurlyAfter",
1003 data: { name: "do" },
1004 type: "DoWhileStatement"
1005 }
1006 ]
1007 },
1008 {
1009 code: "if (foo) {bar()} baz()",
1010 output: null,
1011 options: ["multi"],
1012 errors: [
1013 {
1014 messageId: "unexpectedCurlyAfterCondition",
1015 data: { name: "if" },
1016 type: "IfStatement"
1017 }
1018 ]
1019 },
1020 {
1021 code: "do {foo();} while (bar)",
1022 output: "do foo(); while (bar)",
1023 options: ["multi"],
1024 errors: [
1025 {
1026 messageId: "unexpectedCurlyAfter",
1027 data: { name: "do" },
1028 type: "DoWhileStatement"
1029 }
1030 ]
1031 },
1032
1033 // Don't remove curly braces if it would cause issues due to ASI.
1034 {
1035 code: "if (foo) { bar }\n++baz;",
1036 output: null,
1037 options: ["multi"],
1038 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1039 },
1040 {
1041 code: "if (foo) { bar; }\n++baz;",
1042 output: "if (foo) bar; \n++baz;",
1043 options: ["multi"],
1044 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1045 },
1046 {
1047 code: "if (foo) { bar++ }\nbaz;",
1048 output: null,
1049 options: ["multi"],
1050 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1051 },
1052 {
1053 code: "if (foo) { bar }\n[1, 2, 3].map(foo);",
1054 output: null,
1055 options: ["multi"],
1056 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1057 },
1058 {
1059 code: "if (foo) { bar }\n(1).toString();",
1060 output: null,
1061 options: ["multi"],
1062 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1063 },
1064 {
1065 code: "if (foo) { bar }\n/regex/.test('foo');",
1066 output: null,
1067 options: ["multi"],
1068 parserOptions: { ecmaVersion: 6 },
1069 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1070 },
1071 {
1072 code: "if (foo) { bar }\nBaz();",
1073 output: "if (foo) bar \nBaz();",
1074 options: ["multi"],
1075 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1076 },
1077 {
1078 code:
1079 "if (a) {\n" +
1080 " while (b) {\n" +
1081 " c();\n" +
1082 " d();\n" +
1083 " }\n" +
1084 "} else e();",
1085 output:
1086 "if (a) \n" +
1087 " while (b) {\n" +
1088 " c();\n" +
1089 " d();\n" +
1090 " }\n" +
1091 " else e();",
1092 options: ["multi"],
1093 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1094 },
1095 {
1096 code: "if (foo) { while (bar) {} } else {}",
1097 output: "if (foo) while (bar) {} else {}",
1098 options: ["multi"],
1099 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1100 },
1101 {
1102 code: "if (foo) { var foo = () => {} } else {}",
1103 output: null,
1104 options: ["multi"],
1105 parserOptions: { ecmaVersion: 6 },
1106 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1107 },
1108 {
1109 code: "if (foo) { var foo = function() {} } else {}",
1110 output: null,
1111 options: ["multi"],
1112 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1113 },
1114 {
1115 code: "if (foo) { var foo = function*() {} } else {}",
1116 output: null,
1117 options: ["multi"],
1118 parserOptions: { ecmaVersion: 6 },
1119 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1120 },
1121 {
1122 code: "if (true)\nfoo()\n;[1, 2, 3].bar()",
1123 output: "if (true)\n{foo()\n;}[1, 2, 3].bar()",
1124 options: ["multi-line"],
1125 errors: [{ messageId: "missingCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1126 },
1127
1128 // https://github.com/eslint/eslint/issues/12370
1129 {
1130 code: "if (foo) {\ndoSomething()\n;\n}",
1131 output: "if (foo) \ndoSomething()\n;\n",
1132 options: ["multi-or-nest"],
1133 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1134 },
1135 {
1136 code: "if (foo) doSomething();\nelse if (bar) {\ndoSomethingElse()\n;\n}",
1137 output: "if (foo) doSomething();\nelse if (bar) \ndoSomethingElse()\n;\n",
1138 options: ["multi-or-nest"],
1139 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1140 },
1141 {
1142 code: "if (foo) doSomething();\nelse {\ndoSomethingElse()\n;\n}",
1143 output: "if (foo) doSomething();\nelse \ndoSomethingElse()\n;\n",
1144 options: ["multi-or-nest"],
1145 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "else" }, type: "IfStatement" }]
1146 },
1147 {
1148 code: "for (var i = 0; foo; i++) {\ndoSomething()\n;\n}",
1149 output: "for (var i = 0; foo; i++) \ndoSomething()\n;\n",
1150 options: ["multi-or-nest"],
1151 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "for" }, type: "ForStatement" }]
1152 },
1153 {
1154 code: "for (var foo in bar) {\ndoSomething()\n;\n}",
1155 output: "for (var foo in bar) \ndoSomething()\n;\n",
1156 options: ["multi-or-nest"],
1157 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-in" }, type: "ForInStatement" }]
1158 },
1159 {
1160 code: "for (var foo of bar) {\ndoSomething()\n;\n}",
1161 output: "for (var foo of bar) \ndoSomething()\n;\n",
1162 options: ["multi-or-nest"],
1163 parserOptions: { ecmaVersion: 6 },
1164 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-of" }, type: "ForOfStatement" }]
1165 },
1166 {
1167 code: "while (foo) {\ndoSomething()\n;\n}",
1168 output: "while (foo) \ndoSomething()\n;\n",
1169 options: ["multi-or-nest"],
1170 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1171 },
1172 {
1173 code: "do {\ndoSomething()\n;\n} while (foo)",
1174 output: "do \ndoSomething()\n;\n while (foo)",
1175 options: ["multi-or-nest"],
1176 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "do" }, type: "DoWhileStatement" }]
1177 },
1178
1179 // https://github.com/eslint/eslint/issues/12928 (also in valid[])
1180 {
1181 code: "if (a) { if (b) foo(); }",
1182 output: "if (a) if (b) foo(); ",
1183 options: ["multi"],
1184 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1185 },
1186 {
1187 code: "if (a) { if (b) foo(); else bar(); }",
1188 output: "if (a) if (b) foo(); else bar(); ",
1189 options: ["multi"],
1190 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1191 },
1192 {
1193 code: "if (a) { if (b) foo(); else bar(); } baz();",
1194 output: "if (a) if (b) foo(); else bar(); baz();",
1195 options: ["multi"],
1196 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1197 },
1198 {
1199 code: "if (a) { while (cond) if (b) foo(); }",
1200 output: "if (a) while (cond) if (b) foo(); ",
1201 options: ["multi"],
1202 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1203 },
1204 {
1205 code: "if (a) while (cond) { if (b) foo(); }",
1206 output: "if (a) while (cond) if (b) foo(); ",
1207 options: ["multi"],
1208 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1209 },
1210 {
1211 code: "if (a) while (cond) { if (b) foo(); else bar(); }",
1212 output: "if (a) while (cond) if (b) foo(); else bar(); ",
1213 options: ["multi"],
1214 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1215 },
1216 {
1217 code: "if (a) { while (cond) { if (b) foo(); } bar(); baz() } else quux();",
1218 output: "if (a) { while (cond) if (b) foo(); bar(); baz() } else quux();",
1219 options: ["multi"],
1220 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1221 },
1222 {
1223 code: "if (a) { if (b) foo(); } bar();",
1224 output: "if (a) if (b) foo(); bar();",
1225 options: ["multi"],
1226 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1227 },
1228 {
1229 code: "if(a) { if (b) foo(); } if (c) bar(); else baz();",
1230 output: "if(a) if (b) foo(); if (c) bar(); else baz();",
1231 options: ["multi-or-nest"],
1232 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1233 },
1234 {
1235 code: "if (a) { do if (b) foo(); while (cond); } else bar();",
1236 output: "if (a) do if (b) foo(); while (cond); else bar();",
1237 options: ["multi"],
1238 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1239 },
1240 {
1241 code: "if (a) do { if (b) foo(); } while (cond); else bar();",
1242 output: "if (a) do if (b) foo(); while (cond); else bar();",
1243 options: ["multi"],
1244 errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "do" }, type: "DoWhileStatement" }]
1245 },
1246 {
1247 code: "if (a) { if (b) foo(); else bar(); } else baz();",
1248 output: "if (a) if (b) foo(); else bar(); else baz();",
1249 options: ["multi"],
1250 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1251 },
1252 {
1253 code: "if (a) while (cond) { bar(); } else baz();",
1254 output: "if (a) while (cond) bar(); else baz();",
1255 options: ["multi"],
1256 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1257 },
1258 {
1259 code: "if (a) { for (;;); } else bar();",
1260 output: "if (a) for (;;); else bar();",
1261 options: ["multi"],
1262 errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
1263 },
1264 {
1265 code: "if (a) { while (cond) if (b) foo() } else bar();",
1266 output: "if (a) { while (cond) if (b) foo() } else {bar();}",
1267 options: ["multi", "consistent"],
1268 errors: [{ messageId: "missingCurlyAfter", data: { name: "else" }, type: "IfStatement" }]
1269 },
1270 {
1271
1272 /**
1273 * Reports 2 errors, but one pair of braces is necessary if the other pair gets removed.
1274 * Auto-fix will remove only outer braces in the first iteration.
1275 * After that, the inner braces will become valid and won't be removed in the second iteration.
1276 * If user manually removes inner braces first, the outer braces will become valid.
1277 */
1278 code: "if (a) { while (cond) { if (b) foo(); } } else bar();",
1279 output: "if (a) while (cond) { if (b) foo(); } else bar();",
1280 options: ["multi"],
1281 errors: [
1282 { messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" },
1283 { messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }
1284 ]
1285 }
1286 ]
1287 });