]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/block-spacing.js
update to 7.1.0 sources
[pve-eslint.git] / eslint / tests / lib / rules / block-spacing.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for block-spacing rule.
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/block-spacing");
13const { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("block-spacing", rule, {
22 valid: [
23
24 // default/always
25 { code: "{ foo(); }", options: ["always"] },
26 "{ foo(); }",
27 "{ foo();\n}",
28 "{\nfoo(); }",
29 "{\r\nfoo();\r\n}",
30 "if (a) { foo(); }",
31 "if (a) {} else { foo(); }",
32 "switch (a) {}",
33 "switch (a) { case 0: foo(); }",
34 "while (a) { foo(); }",
35 "do { foo(); } while (a);",
36 "for (;;) { foo(); }",
37 "for (var a in b) { foo(); }",
38 { code: "for (var a of b) { foo(); }", parserOptions: { ecmaVersion: 6 } },
39 "try { foo(); } catch (e) { foo(); }",
40 "function foo() { bar(); }",
41 "(function() { bar(); });",
42 { code: "(() => { bar(); });", parserOptions: { ecmaVersion: 6 } },
43 "if (a) { /* comment */ foo(); /* comment */ }",
44 "if (a) { //comment\n foo(); }",
45
46 // never
47 { code: "{foo();}", options: ["never"] },
48 { code: "{foo();\n}", options: ["never"] },
49 { code: "{\nfoo();}", options: ["never"] },
50 { code: "{\r\nfoo();\r\n}", options: ["never"] },
51 { code: "if (a) {foo();}", options: ["never"] },
52 { code: "if (a) {} else {foo();}", options: ["never"] },
53 { code: "switch (a) {}", options: ["never"] },
54 { code: "switch (a) {case 0: foo();}", options: ["never"] },
55 { code: "while (a) {foo();}", options: ["never"] },
56 { code: "do {foo();} while (a);", options: ["never"] },
57 { code: "for (;;) {foo();}", options: ["never"] },
58 { code: "for (var a in b) {foo();}", options: ["never"] },
59 { code: "for (var a of b) {foo();}", options: ["never"], parserOptions: { ecmaVersion: 6 } },
60 { code: "try {foo();} catch (e) {foo();}", options: ["never"] },
61 { code: "function foo() {bar();}", options: ["never"] },
62 { code: "(function() {bar();});", options: ["never"] },
63 { code: "(() => {bar();});", options: ["never"], parserOptions: { ecmaVersion: 6 } },
64 { code: "if (a) {/* comment */ foo(); /* comment */}", options: ["never"] },
65 { code: "if (a) { //comment\n foo();}", options: ["never"] }
66 ],
67
68 invalid: [
69
70 // default/always
71 {
72 code: "{foo();}",
73 output: "{ foo(); }",
74 options: ["always"],
75 errors: [
76 { type: "BlockStatement", line: 1, column: 1, messageId: "missing", data: { location: "after", token: "{" } },
77 { type: "BlockStatement", line: 1, column: 8, messageId: "missing", data: { location: "before", token: "}" } }
78 ]
79 },
80 {
81 code: "{foo();}",
82 output: "{ foo(); }",
83 errors: [
84 { type: "BlockStatement", line: 1, column: 1, messageId: "missing", data: { location: "after", token: "{" } },
85 { type: "BlockStatement", line: 1, column: 8, messageId: "missing", data: { location: "before", token: "}" } }
86 ]
87 },
88 {
89 code: "{ foo();}",
90 output: "{ foo(); }",
91 errors: [
92 { type: "BlockStatement", line: 1, column: 9, messageId: "missing", data: { location: "before", token: "}" } }
93 ]
94 },
95 {
96 code: "{foo(); }",
97 output: "{ foo(); }",
98 errors: [
99 { type: "BlockStatement", line: 1, column: 1, messageId: "missing", data: { location: "after", token: "{" } }
100 ]
101 },
102 {
103 code: "{\nfoo();}",
104 output: "{\nfoo(); }",
105 errors: [
106 { type: "BlockStatement", line: 2, column: 7, messageId: "missing", data: { location: "before", token: "}" } }
107 ]
108 },
109 {
110 code: "{foo();\n}",
111 output: "{ foo();\n}",
112 errors: [
113 { type: "BlockStatement", line: 1, column: 1, messageId: "missing", data: { location: "after", token: "{" } }
114 ]
115 },
116 {
117 code: "if (a) {foo();}",
118 output: "if (a) { foo(); }",
119 errors: [
120 { type: "BlockStatement", line: 1, column: 8, messageId: "missing", data: { location: "after", token: "{" } },
121 { type: "BlockStatement", line: 1, column: 15, messageId: "missing", data: { location: "before", token: "}" } }
122 ]
123 },
124 {
125 code: "if (a) {} else {foo();}",
126 output: "if (a) {} else { foo(); }",
127 errors: [
128 { type: "BlockStatement", line: 1, column: 16, messageId: "missing", data: { location: "after", token: "{" } },
129 { type: "BlockStatement", line: 1, column: 23, messageId: "missing", data: { location: "before", token: "}" } }
130 ]
131 },
132 {
133 code: "switch (a) {case 0: foo();}",
134 output: "switch (a) { case 0: foo(); }",
135 errors: [
136 { type: "SwitchStatement", line: 1, column: 12, messageId: "missing", data: { location: "after", token: "{" } },
137 { type: "SwitchStatement", line: 1, column: 27, messageId: "missing", data: { location: "before", token: "}" } }
138 ]
139 },
140 {
141 code: "while (a) {foo();}",
142 output: "while (a) { foo(); }",
143 errors: [
144 { type: "BlockStatement", line: 1, column: 11, messageId: "missing", data: { location: "after", token: "{" } },
145 { type: "BlockStatement", line: 1, column: 18, messageId: "missing", data: { location: "before", token: "}" } }
146 ]
147 },
148 {
149 code: "do {foo();} while (a);",
150 output: "do { foo(); } while (a);",
151 errors: [
152 { type: "BlockStatement", line: 1, column: 4, messageId: "missing", data: { location: "after", token: "{" } },
153 { type: "BlockStatement", line: 1, column: 11, messageId: "missing", data: { location: "before", token: "}" } }
154 ]
155 },
156 {
157 code: "for (;;) {foo();}",
158 output: "for (;;) { foo(); }",
159 errors: [
160 { type: "BlockStatement", line: 1, column: 10, messageId: "missing", data: { location: "after", token: "{" } },
161 { type: "BlockStatement", line: 1, column: 17, messageId: "missing", data: { location: "before", token: "}" } }
162 ]
163 },
164 {
165 code: "for (var a in b) {foo();}",
166 output: "for (var a in b) { foo(); }",
167 errors: [
168 { type: "BlockStatement", line: 1, column: 18, messageId: "missing", data: { location: "after", token: "{" } },
169 { type: "BlockStatement", line: 1, column: 25, messageId: "missing", data: { location: "before", token: "}" } }
170 ]
171 },
172 {
173 code: "for (var a of b) {foo();}",
174 output: "for (var a of b) { foo(); }",
175 parserOptions: { ecmaVersion: 6 },
176 errors: [
177 { type: "BlockStatement", line: 1, column: 18, messageId: "missing", data: { location: "after", token: "{" } },
178 { type: "BlockStatement", line: 1, column: 25, messageId: "missing", data: { location: "before", token: "}" } }
179 ]
180 },
181 {
182 code: "try {foo();} catch (e) {foo();} finally {foo();}",
183 output: "try { foo(); } catch (e) { foo(); } finally { foo(); }",
184 errors: [
ebb53d86
TL
185 {
186 type: "BlockStatement",
187 messageId: "missing",
188 data: { location: "after", token: "{" },
189 line: 1,
190 column: 5,
191 endLine: 1,
192 endColumn: 6
193 },
194 {
195 type: "BlockStatement",
196 messageId: "missing",
197 data: { location: "before", token: "}" },
198 line: 1,
199 column: 12,
200 endLine: 1,
201 endColumn: 13
202 },
203 {
204 type: "BlockStatement",
205 messageId: "missing",
206 data: { location: "after", token: "{" },
207 line: 1,
208 column: 24,
209 endLine: 1,
210 endColumn: 25
211 },
212 {
213 type: "BlockStatement",
214 messageId: "missing",
215 data: { location: "before", token: "}" },
216 line: 1,
217 column: 31,
218 endLine: 1,
219 endColumn: 32
220 },
221 {
222 type: "BlockStatement",
223 messageId: "missing",
224 data: { location: "after", token: "{" },
225 line: 1,
226 column: 41,
227 endLine: 1,
228 endColumn: 42
229 },
230 {
231 type: "BlockStatement",
232 messageId: "missing",
233 data: { location: "before", token: "}" },
234 line: 1,
235 column: 48,
236 endLine: 1,
237 endColumn: 49
238 }
eb39fafa
DC
239 ]
240 },
241 {
242 code: "function foo() {bar();}",
243 output: "function foo() { bar(); }",
244 errors: [
245 { type: "BlockStatement", line: 1, column: 16, messageId: "missing", data: { location: "after", token: "{" } },
246 { type: "BlockStatement", line: 1, column: 23, messageId: "missing", data: { location: "before", token: "}" } }
247 ]
248 },
249 {
250 code: "(function() {bar();});",
251 output: "(function() { bar(); });",
252 errors: [
253 { type: "BlockStatement", line: 1, column: 13, messageId: "missing", data: { location: "after", token: "{" } },
254 { type: "BlockStatement", line: 1, column: 20, messageId: "missing", data: { location: "before", token: "}" } }
255 ]
256 },
257 {
258 code: "(() => {bar();});",
259 output: "(() => { bar(); });",
260 parserOptions: { ecmaVersion: 6 },
261 errors: [
262 { type: "BlockStatement", line: 1, column: 8, messageId: "missing", data: { location: "after", token: "{" } },
263 { type: "BlockStatement", line: 1, column: 15, messageId: "missing", data: { location: "before", token: "}" } }
264 ]
265 },
266 {
267 code: "if (a) {/* comment */ foo(); /* comment */}",
268 output: "if (a) { /* comment */ foo(); /* comment */ }",
269 parserOptions: { ecmaVersion: 6 },
270 errors: [
271 { type: "BlockStatement", line: 1, column: 8, messageId: "missing", data: { location: "after", token: "{" } },
272 { type: "BlockStatement", line: 1, column: 43, messageId: "missing", data: { location: "before", token: "}" } }
273 ]
274 },
275 {
276 code: "if (a) {//comment\n foo(); }",
277 output: "if (a) { //comment\n foo(); }",
278 parserOptions: { ecmaVersion: 6 },
279 errors: [
ebb53d86
TL
280 {
281 type: "BlockStatement",
282 messageId: "missing",
283 data: {
284 location: "after",
285 token: "{"
286 },
287 line: 1,
288 column: 8,
289 endLine: 1,
290 endColumn: 9
291 }
eb39fafa
DC
292 ]
293 },
294
295 //----------------------------------------------------------------------
296 // never
297 {
298 code: "{ foo(); }",
299 output: "{foo();}",
300 options: ["never"],
301 errors: [
ebb53d86
TL
302 {
303 type: "BlockStatement",
304 messageId: "extra",
305 data: { location: "after", token: "{" },
306 line: 1,
307 column: 2,
308 endLine: 1,
309 endColumn: 3
310 },
311 {
312 type: "BlockStatement",
313 messageId: "extra",
314 data: { location: "before", token: "}" },
315 line: 1,
316 column: 9,
317 endLine: 1,
318 endColumn: 10
319 }
eb39fafa
DC
320 ]
321 },
322 {
323 code: "{ foo();}",
324 output: "{foo();}",
325 options: ["never"],
326 errors: [
ebb53d86
TL
327 {
328 type: "BlockStatement",
329 messageId: "extra",
330 data: {
331 location: "after",
332 token: "{"
333 },
334 line: 1,
335 column: 2,
336 endLine: 1,
337 endColumn: 3
338 }
eb39fafa
DC
339 ]
340 },
341 {
342 code: "{foo(); }",
343 output: "{foo();}",
344 options: ["never"],
345 errors: [
ebb53d86
TL
346 {
347 type: "BlockStatement",
348 messageId: "extra",
349 data: {
350 location: "before",
351 token: "}"
352 },
353 line: 1,
354 column: 8,
355 endLine: 1,
356 endColumn: 9
357 }
eb39fafa
DC
358 ]
359 },
360 {
361 code: "{\nfoo(); }",
362 output: "{\nfoo();}",
363 options: ["never"],
364 errors: [
ebb53d86
TL
365 {
366 type: "BlockStatement",
367 messageId: "extra",
368 data: {
369 location: "before",
370 token: "}"
371 },
372 line: 2,
373 column: 7,
374 endLine: 2,
375 endColumn: 8
376 }
eb39fafa
DC
377 ]
378 },
379 {
380 code: "{ foo();\n}",
381 output: "{foo();\n}",
382 options: ["never"],
383 errors: [
ebb53d86
TL
384 {
385 type: "BlockStatement",
386 messageId: "extra",
387 data: {
388 location: "after",
389 token: "{"
390 },
391 line: 1,
392 column: 2,
393 endLine: 1,
394 endColumn: 3
395 }
eb39fafa
DC
396 ]
397 },
398 {
399 code: "if (a) { foo(); }",
400 output: "if (a) {foo();}",
401 options: ["never"],
402 errors: [
ebb53d86
TL
403 {
404 type: "BlockStatement",
405 messageId: "extra",
406 data: { location: "after", token: "{" },
407 line: 1,
408 column: 9,
409 endLine: 1,
410 endColumn: 10
411 },
412 {
413 type: "BlockStatement",
414 messageId: "extra",
415 data: { location: "before", token: "}" },
416 line: 1,
417 column: 16,
418 endLine: 1,
419 endColumn: 17
420 }
eb39fafa
DC
421 ]
422 },
423 {
424 code: "if (a) {} else { foo(); }",
425 output: "if (a) {} else {foo();}",
426 options: ["never"],
427 errors: [
ebb53d86
TL
428 {
429 type: "BlockStatement",
430 messageId: "extra",
431 data: { location: "after", token: "{" },
432 line: 1,
433 column: 17,
434 endLine: 1,
435 endColumn: 18
436 },
437 {
438 type: "BlockStatement",
439 messageId: "extra",
440 data: { location: "before", token: "}" },
441 line: 1,
442 column: 24,
443 endLine: 1,
444 endColumn: 25
445 }
eb39fafa
DC
446 ]
447 },
448 {
449 code: "switch (a) { case 0: foo(); }",
450 output: "switch (a) {case 0: foo();}",
451 options: ["never"],
452 errors: [
ebb53d86
TL
453 {
454 type: "SwitchStatement",
455 messageId: "extra",
456 data: { location: "after", token: "{" },
457 line: 1,
458 column: 13,
459 endLine: 1,
460 endColumn: 14
461 },
462 {
463 type: "SwitchStatement",
464 messageId: "extra",
465 data: { location: "before", token: "}" },
466 line: 1,
467 column: 28,
468 endLine: 1,
469 endColumn: 29
470 }
eb39fafa
DC
471 ]
472 },
473 {
474 code: "while (a) { foo(); }",
475 output: "while (a) {foo();}",
476 options: ["never"],
477 errors: [
ebb53d86
TL
478 {
479 type: "BlockStatement",
480 messageId: "extra",
481 data: { location: "after", token: "{" },
482 line: 1,
483 column: 12,
484 endLine: 1,
485 endColumn: 13
486 },
487 {
488 type: "BlockStatement",
489 messageId: "extra",
490 data: { location: "before", token: "}" },
491 line: 1,
492 column: 19,
493 endLine: 1,
494 endColumn: 20
495 }
eb39fafa
DC
496 ]
497 },
498 {
499 code: "do { foo(); } while (a);",
500 output: "do {foo();} while (a);",
501 options: ["never"],
502 errors: [
ebb53d86
TL
503 {
504 type: "BlockStatement",
505 messageId: "extra",
506 data: { location: "after", token: "{" },
507 line: 1,
508 column: 5,
509 endLine: 1,
510 endColumn: 6
511 },
512 {
513 type: "BlockStatement",
514 messageId: "extra",
515 data: { location: "before", token: "}" },
516 line: 1,
517 column: 12,
518 endLine: 1,
519 endColumn: 13
520 }
eb39fafa
DC
521 ]
522 },
523 {
524 code: "for (;;) { foo(); }",
525 output: "for (;;) {foo();}",
526 options: ["never"],
527 errors: [
ebb53d86
TL
528 {
529 type: "BlockStatement",
530 messageId: "extra",
531 data: { location: "after", token: "{" },
532 line: 1,
533 column: 11,
534 endLine: 1,
535 endColumn: 12
536 },
537 {
538 type: "BlockStatement",
539 messageId: "extra",
540 data: { location: "before", token: "}" },
541 line: 1,
542 column: 18,
543 endLine: 1,
544 endColumn: 19
545 }
eb39fafa
DC
546 ]
547 },
548 {
549 code: "for (var a in b) { foo(); }",
550 output: "for (var a in b) {foo();}",
551 options: ["never"],
552 errors: [
ebb53d86
TL
553 {
554 type: "BlockStatement",
555 messageId: "extra",
556 data: { location: "after", token: "{" },
557 line: 1,
558 column: 19,
559 endLine: 1,
560 endColumn: 20
561 },
562 {
563 type: "BlockStatement",
564 messageId: "extra",
565 data: { location: "before", token: "}" },
566 line: 1,
567 column: 26,
568 endLine: 1,
569 endColumn: 27
570 }
eb39fafa
DC
571 ]
572 },
573 {
574 code: "for (var a of b) { foo(); }",
575 output: "for (var a of b) {foo();}",
576 options: ["never"],
577 parserOptions: { ecmaVersion: 6 },
578 errors: [
ebb53d86
TL
579 {
580 type: "BlockStatement",
581 messageId: "extra",
582 data: { location: "after", token: "{" },
583 line: 1,
584 column: 19,
585 endLine: 1,
586 endColumn: 20
587 },
588 {
589 type: "BlockStatement",
590 messageId: "extra",
591 data: { location: "before", token: "}" },
592 line: 1,
593 column: 26,
594 endLine: 1,
595 endColumn: 27
596 }
eb39fafa
DC
597 ]
598 },
599 {
600 code: "try { foo(); } catch (e) { foo(); } finally { foo(); }",
601 output: "try {foo();} catch (e) {foo();} finally {foo();}",
602 options: ["never"],
603 errors: [
ebb53d86
TL
604 {
605 type: "BlockStatement",
606 messageId: "extra",
607 data: { location: "after", token: "{" },
608 line: 1,
609 column: 6,
610 endLine: 1,
611 endColumn: 7
612 },
613 {
614 type: "BlockStatement",
615 messageId: "extra",
616 data: { location: "before", token: "}" },
617 line: 1,
618 column: 13,
619 endLine: 1,
620 endColumn: 14
621 },
622 {
623 type: "BlockStatement",
624 messageId: "extra",
625 data: { location: "after", token: "{" },
626 line: 1,
627 column: 27,
628 endLine: 1,
629 endColumn: 28
630 },
631 {
632 type: "BlockStatement",
633 line: 1,
634 column: 34,
635 messageId: "extra",
636 data: { location: "before", token: "}" },
637 endLine: 1,
638 endColumn: 35
639 },
640 {
641 type: "BlockStatement",
642 messageId: "extra",
643 data: { location: "after", token: "{" },
644 line: 1,
645 column: 46,
646 endLine: 1,
647 endColumn: 47
648 },
649 {
650 type: "BlockStatement",
651 messageId: "extra",
652 data: { location: "before", token: "}" },
653 line: 1,
654 column: 53,
655 endLine: 1,
656 endColumn: 54
657 }
eb39fafa
DC
658 ]
659 },
660 {
661 code: "function foo() { bar(); }",
662 output: "function foo() {bar();}",
663 options: ["never"],
664 errors: [
ebb53d86
TL
665 {
666 type: "BlockStatement",
667 messageId: "extra",
668 data: { location: "after", token: "{" },
669 line: 1,
670 column: 17,
671 endLine: 1,
672 endColumn: 18
673 },
674 {
675 type: "BlockStatement",
676 messageId: "extra",
677 data: { location: "before", token: "}" },
678 line: 1,
679 column: 24,
680 endLine: 1,
681 endColumn: 25
682 }
eb39fafa
DC
683 ]
684 },
685 {
686 code: "(function() { bar(); });",
687 output: "(function() {bar();});",
688 options: ["never"],
689 errors: [
ebb53d86
TL
690 {
691 type: "BlockStatement",
692 messageId: "extra",
693 data: { location: "after", token: "{" },
694 line: 1,
695 column: 14,
696 endLine: 1,
697 endColumn: 15
698 },
699 {
700 type: "BlockStatement",
701 messageId: "extra",
702 data: { location: "before", token: "}" },
703 line: 1,
704 column: 21,
705 endLine: 1,
706 endColumn: 22
707 }
eb39fafa
DC
708 ]
709 },
710 {
711 code: "(() => { bar(); });",
712 output: "(() => {bar();});",
713 options: ["never"],
714 parserOptions: { ecmaVersion: 6 },
715 errors: [
ebb53d86
TL
716 {
717 type: "BlockStatement",
718 messageId: "extra",
719 data: { location: "after", token: "{" },
720 line: 1,
721 column: 9,
722 endLine: 1,
723 endColumn: 10
724 },
725 {
726 type: "BlockStatement",
727 messageId: "extra",
728 data: { location: "before", token: "}" },
729 line: 1,
730 column: 16,
731 endLine: 1,
732 endColumn: 17
733 }
eb39fafa
DC
734 ]
735 },
736 {
737 code: "if (a) { /* comment */ foo(); /* comment */ }",
738 output: "if (a) {/* comment */ foo(); /* comment */}",
739 options: ["never"],
740 errors: [
ebb53d86
TL
741 {
742 type: "BlockStatement",
743 messageId: "extra",
744 data: { location: "after", token: "{" },
745 line: 1,
746 column: 9,
747 endLine: 1,
748 endColumn: 10
749 },
750 {
751 type: "BlockStatement",
752 messageId: "extra",
753 data: { location: "before", token: "}" },
754 line: 1,
755 column: 44,
756 endLine: 1,
757 endColumn: 45
758 }
759 ]
760 },
761 {
762 code: "(() => { bar();});",
763 output: "(() => {bar();});",
764 options: ["never"],
765 parserOptions: { ecmaVersion: 6 },
766 errors: [
767 {
768 type: "BlockStatement",
769 messageId: "extra",
770 data: { location: "after", token: "{" },
771 line: 1,
772 column: 9,
773 endLine: 1,
774 endColumn: 12
775 }
776 ]
777 },
778 {
779 code: "(() => {bar(); });",
780 output: "(() => {bar();});",
781 options: ["never"],
782 parserOptions: { ecmaVersion: 6 },
783 errors: [
784 {
785 type: "BlockStatement",
786 messageId: "extra",
787 data: { location: "before", token: "}" },
788 line: 1,
789 column: 15,
790 endLine: 1,
791 endColumn: 18
792 }
793 ]
794 },
795 {
796 code: "(() => { bar(); });",
797 output: "(() => {bar();});",
798 options: ["never"],
799 parserOptions: { ecmaVersion: 6 },
800 errors: [
801 {
802 type: "BlockStatement",
803 messageId: "extra",
804 data: { location: "after", token: "{" },
805 line: 1,
806 column: 9,
807 endLine: 1,
808 endColumn: 12
809 },
810 {
811 type: "BlockStatement",
812 messageId: "extra",
813 data: { location: "before", token: "}" },
814 line: 1,
815 column: 18,
816 endLine: 1,
817 endColumn: 21
818 }
eb39fafa
DC
819 ]
820 }
821 ]
822});