]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/lines-around-comment.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / lines-around-comment.js
1 /**
2 * @fileoverview Test enforcement of lines around comments.
3 * @author Jamund Ferguson
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/lines-around-comment"),
12 { RuleTester } = require("../../../lib/rule-tester"),
13 { unIndent } = require("../../_utils");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("lines-around-comment", rule, {
22
23 valid: [
24
25 // default rules
26 "bar()\n\n/** block block block\n * block \n */\n\nvar a = 1;",
27 "bar()\n\n/** block block block\n * block \n */\nvar a = 1;",
28 "bar()\n// line line line \nvar a = 1;",
29 "bar()\n\n// line line line\nvar a = 1;",
30 "bar()\n// line line line\n\nvar a = 1;",
31
32 // line comments
33 {
34 code: "bar()\n// line line line\n\nvar a = 1;",
35 options: [{ afterLineComment: true }]
36 },
37 {
38 code: "foo()\n\n// line line line\nvar a = 1;",
39 options: [{ beforeLineComment: true }]
40 },
41 {
42 code: "foo()\n\n// line line line\n\nvar a = 1;",
43 options: [{ beforeLineComment: true, afterLineComment: true }]
44 },
45 {
46 code: "foo()\n\n// line line line\n// line line\n\nvar a = 1;",
47 options: [{ beforeLineComment: true, afterLineComment: true }]
48 },
49 {
50 code: "// line line line\n// line line",
51 options: [{ beforeLineComment: true, afterLineComment: true }]
52 },
53
54 // block comments
55 {
56 code: "bar()\n\n/** A Block comment with a an empty line after\n *\n */\nvar a = 1;",
57 options: [{ afterBlockComment: false, beforeBlockComment: true }]
58 },
59 {
60 code: "bar()\n\n/** block block block\n * block \n */\nvar a = 1;",
61 options: [{ afterBlockComment: false }]
62 },
63 {
64 code: "/** \nblock \nblock block\n */\n/* block \n block \n */",
65 options: [{ afterBlockComment: true, beforeBlockComment: true }]
66 },
67 {
68 code: "bar()\n\n/** block block block\n * block \n */\n\nvar a = 1;",
69 options: [{ afterBlockComment: true, beforeBlockComment: true }]
70 },
71
72 // inline comments (should not ever warn)
73 {
74 code: "foo() // An inline comment with a an empty line after\nvar a = 1;",
75 options: [{ afterLineComment: true, beforeLineComment: true }]
76 },
77 {
78 code: "foo();\nbar() /* An inline block comment with a an empty line after\n *\n */\nvar a = 1;",
79 options: [{ beforeBlockComment: true }]
80 },
81
82 // mixed comment (some block & some line)
83 {
84 code: "bar()\n\n/** block block block\n * block \n */\n//line line line\nvar a = 1;",
85 options: [{ afterBlockComment: true }]
86 },
87 {
88 code: "bar()\n\n/** block block block\n * block \n */\n//line line line\nvar a = 1;",
89 options: [{ beforeLineComment: true }]
90 },
91
92 // check for block start comments
93 {
94 code: "var a,\n\n// line\nb;",
95 options: [{
96 beforeLineComment: true,
97 allowBlockStart: true
98 }]
99 },
100 {
101 code: "function foo(){ \n// line at block start\nvar g = 1;\n}",
102 options: [{
103 beforeLineComment: true,
104 allowBlockStart: true
105 }]
106 },
107 {
108 code: "function foo(){// line at block start\nvar g = 1;\n}",
109 options: [{
110 beforeLineComment: true,
111 allowBlockStart: true
112 }]
113 },
114 {
115 code: "var foo = function(){\n// line at block start\nvar g = 1;\n}",
116 options: [{
117 beforeLineComment: true,
118 allowBlockStart: true
119 }]
120 },
121 {
122 code: "var foo = function(){\n// line at block start\n}",
123 options: [{
124 beforeLineComment: true,
125 allowBlockStart: true
126 }]
127 },
128 {
129 code: "if(true){\n// line at block start\nvar g = 1;\n}",
130 options: [{
131 beforeLineComment: true,
132 allowBlockStart: true
133 }]
134 },
135 {
136 code: "if(true){\n\n// line at block start\nvar g = 1;\n}",
137 options: [{
138 beforeLineComment: true,
139 allowBlockStart: true
140 }]
141 },
142 {
143 code: "if(true){\n// line at block start\n}",
144 options: [{
145 beforeLineComment: true,
146 allowBlockStart: true
147 }]
148 },
149 {
150 code: "if(true){ bar(); } else {\n// line at block start\n}",
151 options: [{
152 beforeLineComment: true,
153 allowBlockStart: true
154 }]
155 },
156 {
157 code: "switch ('foo'){\ncase 'foo':\n// line at switch case start\nbreak;\n}",
158 options: [{
159 beforeLineComment: true,
160 allowBlockStart: true
161 }]
162 },
163 {
164 code: "switch ('foo'){\ncase 'foo':\n\n// line at switch case start\nbreak;\n}",
165 options: [{
166 beforeLineComment: true,
167 allowBlockStart: true
168 }]
169 },
170 {
171 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\n// line at switch case start\nbreak;\n}",
172 options: [{
173 beforeLineComment: true,
174 allowBlockStart: true
175 }]
176 },
177 {
178 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\n\n// line at switch case start\nbreak;\n}",
179 options: [{
180 beforeLineComment: true,
181 allowBlockStart: true
182 }]
183 },
184 {
185 code: "function foo(){ \n/* block comment at block start */\nvar g = 1;\n}",
186 options: [{
187 allowBlockStart: true
188 }]
189 },
190 {
191 code: "function foo(){/* block comment at block start */\nvar g = 1;\n}",
192 options: [{
193 allowBlockStart: true
194 }]
195 },
196 {
197 code: "var foo = function(){\n/* block comment at block start */\nvar g = 1;\n}",
198 options: [{
199 allowBlockStart: true
200 }]
201 },
202 {
203 code: "if(true){\n/* block comment at block start */\nvar g = 1;\n}",
204 options: [{
205 allowBlockStart: true
206 }]
207 },
208 {
209 code: "if(true){\n\n/* block comment at block start */\nvar g = 1;\n}",
210 options: [{
211 allowBlockStart: true
212 }]
213 },
214 {
215 code: "while(true){\n\n/* \nblock comment at block start\n */\nvar g = 1;\n}",
216 options: [{
217 allowBlockStart: true
218 }]
219 },
220 {
221 code: "class A {\n/**\n* hi\n */\nconstructor() {}\n}",
222 options: [{
223 allowBlockStart: true
224 }],
225 parserOptions: { ecmaVersion: 6 }
226 },
227 {
228 code: "class A {\n/**\n* hi\n */\nconstructor() {}\n}",
229 options: [{
230 allowClassStart: true
231 }],
232 parserOptions: { ecmaVersion: 6 }
233 },
234 {
235 code: "class A {\n/**\n* hi\n */\nconstructor() {}\n}",
236 options: [{
237 allowBlockStart: false,
238 allowClassStart: true
239 }],
240 parserOptions: { ecmaVersion: 6 }
241 },
242 {
243 code: "switch ('foo'){\ncase 'foo':\n/* block comment at switch case start */\nbreak;\n}",
244 options: [{
245 allowBlockStart: true
246 }]
247 },
248 {
249 code: "switch ('foo'){\ncase 'foo':\n\n/* block comment at switch case start */\nbreak;\n}",
250 options: [{
251 allowBlockStart: true
252 }]
253 },
254 {
255 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\n/* block comment at switch case start */\nbreak;\n}",
256 options: [{
257 allowBlockStart: true
258 }]
259 },
260 {
261 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\n\n/* block comment at switch case start */\nbreak;\n}",
262 options: [{
263 allowBlockStart: true
264 }]
265 },
266 {
267 code: unIndent`
268 class C {
269 static {
270 // line comment
271 }
272
273 static {
274 // line comment
275 foo();
276 }
277 }`,
278 options: [{
279 beforeLineComment: true,
280 allowBlockStart: true
281 }],
282 parserOptions: { ecmaVersion: 2022 }
283 },
284 {
285 code: unIndent`
286 class C {
287 static
288 {
289 // line comment
290 }
291
292 static
293 {
294 // line comment
295 foo();
296 }
297 }`,
298 options: [{
299 beforeLineComment: true,
300 allowBlockStart: true
301 }],
302 parserOptions: { ecmaVersion: 2022 }
303 },
304 {
305 code: unIndent`
306 class C {
307 static {
308 /* block comment */
309 }
310
311 static {
312 /* block
313 comment */
314 }
315
316 static {
317 /* block comment */
318 foo();
319 }
320
321 static {
322 /* block
323 comment */
324 foo();
325 }
326 }`,
327 options: [{
328 beforeBlockComment: true,
329 allowBlockStart: true
330 }],
331 parserOptions: { ecmaVersion: 2022 }
332 },
333 {
334 code: unIndent`
335 class C {
336 static
337 {
338 /* block comment */
339 }
340
341 static
342 {
343 /* block
344 comment */
345 }
346
347 static
348 {
349 /* block comment */
350 foo();
351 }
352
353 static
354 {
355 /* block
356 comment */
357 foo();
358 }
359 }`,
360 options: [{
361 beforeBlockComment: true,
362 allowBlockStart: true
363 }],
364 parserOptions: { ecmaVersion: 2022 }
365 },
366
367 // https://github.com/eslint/eslint/issues/16131
368 {
369 code: `
370 switch (foo) {
371 // this comment is allowed by allowBlockStart: true
372
373 case 1:
374 bar();
375 break;
376
377 // this comment is allowed by allowBlockEnd: true
378 }
379 `,
380 options: [{
381 allowBlockStart: true,
382 beforeLineComment: true,
383 afterLineComment: true,
384 allowBlockEnd: true
385 }]
386 },
387 {
388 code: `
389 switch (foo)
390 {
391 // this comment is allowed by allowBlockStart: true
392
393 case 1:
394 bar();
395 break;
396 }
397 `,
398 options: [{
399 allowBlockStart: true,
400 beforeLineComment: true,
401 afterLineComment: true
402 }]
403 },
404 {
405 code: `
406 switch (
407 function(){}()
408 )
409 {
410 // this comment is allowed by allowBlockStart: true
411 case foo:
412 break;
413 }
414 `,
415 options: [{
416 allowBlockStart: true,
417 beforeLineComment: true
418 }]
419 },
420
421 // check for block end comments
422 {
423 code: "var a,\n// line\n\nb;",
424 options: [{
425 afterLineComment: true,
426 allowBlockEnd: true
427 }]
428 },
429 {
430 code: "function foo(){\nvar g = 91;\n// line at block end\n}",
431 options: [{
432 afterLineComment: true,
433 allowBlockEnd: true
434 }]
435 },
436 {
437 code: "function foo(){\nvar g = 61;\n\n\n// line at block end\n}",
438 options: [{
439 afterLineComment: true,
440 allowBlockEnd: true
441 }]
442 },
443 {
444 code: "var foo = function(){\nvar g = 1;\n\n\n// line at block end\n}",
445 options: [{
446 afterLineComment: true,
447 allowBlockEnd: true
448 }]
449 },
450 {
451 code: "if(true){\nvar g = 1;\n// line at block end\n}",
452 options: [{
453 afterLineComment: true,
454 allowBlockEnd: true
455 }]
456 },
457 {
458 code: "if(true){\nvar g = 1;\n\n// line at block end\n}",
459 options: [{
460 afterLineComment: true,
461 allowBlockEnd: true
462 }]
463 },
464 {
465 code: "switch ('foo'){\ncase 'foo':\nvar g = 1;\n\n// line at switch case end\n}",
466 options: [{
467 afterLineComment: true,
468 allowBlockEnd: true
469 }]
470 },
471 {
472 code: "switch ('foo'){\ncase 'foo':\nvar g = 1;\n\n// line at switch case end\n\n}",
473 options: [{
474 afterLineComment: true,
475 allowBlockEnd: true
476 }]
477 },
478 {
479 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\nvar g = 1;\n\n// line at switch case end\n}",
480 options: [{
481 afterLineComment: true,
482 allowBlockEnd: true
483 }]
484 },
485 {
486 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\nvar g = 1;\n\n// line at switch case end\n\n}",
487 options: [{
488 afterLineComment: true,
489 allowBlockEnd: true
490 }]
491 },
492 {
493 code: "while(true){\n// line at block start and end\n}",
494 options: [{
495 afterLineComment: true,
496 allowBlockEnd: true
497 }]
498 },
499 {
500 code: "while(true){\n// line at block start and end\n}",
501 options: [{
502 afterLineComment: true,
503 allowBlockStart: true,
504 allowBlockEnd: true
505 }]
506 },
507 {
508 code: "while(true){\n// line at block start and end\n}",
509 options: [{
510 beforeLineComment: true,
511 allowBlockStart: true,
512 allowBlockEnd: true
513 }]
514 },
515 {
516 code: "while(true){\n// line at block start and end\n}",
517 options: [{
518 afterLineComment: true,
519 beforeLineComment: true,
520 allowBlockStart: true,
521 allowBlockEnd: true
522 }]
523 },
524 {
525 code: "while(true){\n// line at block start and end\n}",
526 options: [{
527 beforeLineComment: true,
528 allowBlockStart: true
529 }]
530 },
531 {
532 code: "function foo(){ \nvar g = 1;\n/* block comment at block end */\n}",
533 options: [{
534 beforeBlockComment: false,
535 afterBlockComment: true,
536 allowBlockEnd: true
537 }]
538 },
539 {
540 code: "function foo(){\nvar g = 1;\n/* block comment at block end */}",
541 options: [{
542 beforeBlockComment: false,
543 afterBlockComment: true,
544 allowBlockEnd: true
545 }]
546 },
547 {
548 code: "var foo = function(){\nvar g = 1;\n/* block comment at block end */\n}",
549 options: [{
550 beforeBlockComment: false,
551 afterBlockComment: true,
552 allowBlockEnd: true
553 }]
554 },
555 {
556 code: "if(true){\nvar g = 1;\n/* block comment at block end */\n}",
557 options: [{
558 beforeBlockComment: false,
559 afterBlockComment: true,
560 allowBlockEnd: true
561 }]
562 },
563 {
564 code: "if(true){\nvar g = 1;\n\n/* block comment at block end */\n}",
565 options: [{
566 afterBlockComment: true,
567 allowBlockEnd: true
568 }]
569 },
570 {
571 code: "while(true){\n\nvar g = 1;\n\n/* \nblock comment at block end\n */}",
572 options: [{
573 afterBlockComment: true,
574 allowBlockEnd: true
575 }]
576 },
577 {
578 code: "class B {\nconstructor() {}\n\n/**\n* hi\n */\n}",
579 options: [{
580 afterBlockComment: true,
581 allowBlockEnd: true
582 }],
583 parserOptions: { ecmaVersion: 6 }
584 },
585 {
586 code: "class B {\nconstructor() {}\n\n/**\n* hi\n */\n}",
587 options: [{
588 afterBlockComment: true,
589 allowClassEnd: true
590 }],
591 parserOptions: { ecmaVersion: 6 }
592 },
593 {
594 code: "class B {\nconstructor() {}\n\n/**\n* hi\n */\n}",
595 options: [{
596 afterBlockComment: true,
597 allowBlockEnd: false,
598 allowClassEnd: true
599 }],
600 parserOptions: { ecmaVersion: 6 }
601 },
602 {
603 code: "switch ('foo'){\ncase 'foo':\nvar g = 1;\n\n/* block comment at switch case end */\n}",
604 options: [{
605 afterBlockComment: true,
606 allowBlockEnd: true
607 }]
608 },
609 {
610 code: "switch ('foo'){\ncase 'foo':\nvar g = 1;\n\n/* block comment at switch case end */\n\n}",
611 options: [{
612 afterBlockComment: true,
613 allowBlockEnd: true
614 }]
615 },
616 {
617 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\nvar g = 1;\n\n/* block comment at switch case end */\n}",
618 options: [{
619 afterBlockComment: true,
620 allowBlockEnd: true
621 }]
622 },
623 {
624 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\nvar g = 1;\n\n/* block comment at switch case end */\n\n}",
625 options: [{
626 afterBlockComment: true,
627 allowBlockEnd: true
628 }]
629 },
630 {
631 code: unIndent`
632 class C {
633 static {
634 // line comment
635 }
636
637 static {
638 foo();
639 // line comment
640 }
641 }`,
642 options: [{
643 afterLineComment: true,
644 allowBlockEnd: true
645 }],
646 parserOptions: { ecmaVersion: 2022 }
647 },
648 {
649 code: unIndent`
650 class C {
651 static {
652 /* block comment */
653 }
654
655 static {
656 /* block
657 comment */
658 }
659
660 static {
661 foo();
662 /* block comment */
663 }
664
665 static {
666 foo();
667 /* block
668 comment */
669 }
670 }`,
671 options: [{
672 beforeBlockComment: false, // default is `true`
673 afterBlockComment: true,
674 allowBlockEnd: true
675 }],
676 parserOptions: { ecmaVersion: 2022 }
677 },
678
679 // check for object start comments
680 {
681 code:
682 "var a,\n\n" +
683 "// line\n" +
684 "b;",
685 options: [{
686 beforeLineComment: true,
687 allowObjectStart: true
688 }]
689 },
690 {
691 code:
692 "var obj = {\n" +
693 " // line at object start\n" +
694 " g: 1\n" +
695 "};",
696 options: [{
697 beforeLineComment: true,
698 allowObjectStart: true
699 }]
700 },
701 {
702 code:
703 "function hi() {\n" +
704 " return {\n" +
705 " // hi\n" +
706 " test: function() {\n" +
707 " }\n" +
708 " }\n" +
709 "}",
710 options: [{
711 beforeLineComment: true,
712 allowObjectStart: true
713 }]
714 },
715 {
716 code:
717 "var obj = {\n" +
718 " /* block comment at object start*/\n" +
719 " g: 1\n" +
720 "};",
721 options: [{
722 beforeBlockComment: true,
723 allowObjectStart: true
724 }]
725 },
726 {
727 code:
728 "function hi() {\n" +
729 " return {\n" +
730 " /**\n" +
731 " * hi\n" +
732 " */\n" +
733 " test: function() {\n" +
734 " }\n" +
735 " }\n" +
736 "}",
737 options: [{
738 beforeLineComment: true,
739 allowObjectStart: true
740 }]
741 },
742 {
743 code:
744 "const {\n" +
745 " // line at object start\n" +
746 " g: a\n" +
747 "} = {};",
748 options: [{
749 beforeLineComment: true,
750 allowObjectStart: true
751 }],
752 parserOptions: { ecmaVersion: 6 }
753 },
754 {
755 code:
756 "const {\n" +
757 " // line at object start\n" +
758 " g\n" +
759 "} = {};",
760 options: [{
761 beforeLineComment: true,
762 allowObjectStart: true
763 }],
764 parserOptions: { ecmaVersion: 6 }
765 },
766 {
767 code:
768 "const {\n" +
769 " /* block comment at object-like start*/\n" +
770 " g: a\n" +
771 "} = {};",
772 options: [{
773 beforeBlockComment: true,
774 allowObjectStart: true
775 }],
776 parserOptions: { ecmaVersion: 6 }
777 },
778 {
779 code:
780 "const {\n" +
781 " /* block comment at object-like start*/\n" +
782 " g\n" +
783 "} = {};",
784 options: [{
785 beforeBlockComment: true,
786 allowObjectStart: true
787 }],
788 parserOptions: { ecmaVersion: 6 }
789 },
790
791 // check for object end comments
792 {
793 code:
794 "var a,\n" +
795 "// line\n\n" +
796 "b;",
797 options: [{
798 afterLineComment: true,
799 allowObjectEnd: true
800 }]
801 },
802 {
803 code:
804 "var obj = {\n" +
805 " g: 1\n" +
806 " // line at object end\n" +
807 "};",
808 options: [{
809 afterLineComment: true,
810 allowObjectEnd: true
811 }]
812 },
813 {
814 code:
815 "function hi() {\n" +
816 " return {\n" +
817 " test: function() {\n" +
818 " }\n" +
819 " // hi\n" +
820 " }\n" +
821 "}",
822 options: [{
823 afterLineComment: true,
824 allowObjectEnd: true
825 }]
826 },
827 {
828 code:
829 "var obj = {\n" +
830 " g: 1\n" +
831 " \n" +
832 " /* block comment at object end*/\n" +
833 "};",
834 options: [{
835 afterBlockComment: true,
836 allowObjectEnd: true
837 }]
838 },
839 {
840 code:
841 "function hi() {\n" +
842 " return {\n" +
843 " test: function() {\n" +
844 " }\n" +
845 " \n" +
846 " /**\n" +
847 " * hi\n" +
848 " */\n" +
849 " }\n" +
850 "}",
851 options: [{
852 afterBlockComment: true,
853 allowObjectEnd: true
854 }]
855 },
856 {
857 code:
858 "const {\n" +
859 " g: a\n" +
860 " // line at object end\n" +
861 "} = {};",
862 options: [{
863 afterLineComment: true,
864 allowObjectEnd: true
865 }],
866 parserOptions: { ecmaVersion: 6 }
867 },
868 {
869 code:
870 "const {\n" +
871 " g\n" +
872 " // line at object end\n" +
873 "} = {};",
874 options: [{
875 afterLineComment: true,
876 allowObjectEnd: true
877 }],
878 parserOptions: { ecmaVersion: 6 }
879 },
880 {
881 code:
882 "const {\n" +
883 " g: a\n" +
884 " \n" +
885 " /* block comment at object-like end*/\n" +
886 "} = {};",
887 options: [{
888 afterBlockComment: true,
889 allowObjectEnd: true
890 }],
891 parserOptions: { ecmaVersion: 6 }
892 },
893 {
894 code:
895 "const {\n" +
896 " g\n" +
897 " \n" +
898 " /* block comment at object-like end*/\n" +
899 "} = {};",
900 options: [{
901 afterBlockComment: true,
902 allowObjectEnd: true
903 }],
904 parserOptions: { ecmaVersion: 6 }
905 },
906
907 // check for array start comments
908 {
909 code:
910 "var a,\n\n" +
911 "// line\n" +
912 "b;",
913 options: [{
914 beforeLineComment: true,
915 allowArrayStart: true
916 }]
917 },
918 {
919 code:
920 "var arr = [\n" +
921 " // line at array start\n" +
922 " 1\n" +
923 "];",
924 options: [{
925 beforeLineComment: true,
926 allowArrayStart: true
927 }]
928 },
929 {
930 code:
931 "var arr = [\n" +
932 " /* block comment at array start*/\n" +
933 " 1\n" +
934 "];",
935 options: [{
936 beforeBlockComment: true,
937 allowArrayStart: true
938 }]
939 },
940 {
941 code:
942 "const [\n" +
943 " // line at array start\n" +
944 " a\n" +
945 "] = [];",
946 options: [{
947 beforeLineComment: true,
948 allowArrayStart: true
949 }],
950 parserOptions: { ecmaVersion: 6 }
951 },
952 {
953 code:
954 "const [\n" +
955 " /* block comment at array start*/\n" +
956 " a\n" +
957 "] = [];",
958 options: [{
959 beforeBlockComment: true,
960 allowArrayStart: true
961 }],
962 parserOptions: { ecmaVersion: 6 }
963 },
964
965 // check for array end comments
966 {
967 code:
968 "var a,\n" +
969 "// line\n\n" +
970 "b;",
971 options: [{
972 afterLineComment: true,
973 allowArrayEnd: true
974 }]
975 },
976 {
977 code:
978 "var arr = [\n" +
979 " 1\n" +
980 " // line at array end\n" +
981 "];",
982 options: [{
983 afterLineComment: true,
984 allowArrayEnd: true
985 }]
986 },
987 {
988 code:
989 "var arr = [\n" +
990 " 1\n" +
991 " \n" +
992 " /* block comment at array end*/\n" +
993 "];",
994 options: [{
995 afterBlockComment: true,
996 allowArrayEnd: true
997 }]
998 },
999 {
1000 code:
1001 "const [\n" +
1002 " a\n" +
1003 " // line at array end\n" +
1004 "] = [];",
1005 options: [{
1006 afterLineComment: true,
1007 allowArrayEnd: true
1008 }],
1009 parserOptions: { ecmaVersion: 6 }
1010 },
1011 {
1012 code:
1013 "const [\n" +
1014 " a\n" +
1015 " \n" +
1016 " /* block comment at array end*/\n" +
1017 "] = [];",
1018 options: [{
1019 afterBlockComment: true,
1020 allowArrayEnd: true
1021 }],
1022 parserOptions: { ecmaVersion: 6 }
1023 },
1024
1025 // ignorePattern
1026 {
1027 code:
1028 "foo;\n\n" +
1029 "/* eslint-disable no-underscore-dangle */\n\n" +
1030 "this._values = values;\n" +
1031 "this._values2 = true;\n" +
1032 "/* eslint-enable no-underscore-dangle */\n" +
1033 "bar",
1034 options: [{
1035 beforeBlockComment: true,
1036 afterBlockComment: true
1037 }]
1038 },
1039 "foo;\n/* eslint */",
1040 "foo;\n/* jshint */",
1041 "foo;\n/* jslint */",
1042 "foo;\n/* istanbul */",
1043 "foo;\n/* global */",
1044 "foo;\n/* globals */",
1045 "foo;\n/* exported */",
1046 "foo;\n/* jscs */",
1047 {
1048 code: "foo\n/* this is pragmatic */",
1049 options: [{ ignorePattern: "pragma" }]
1050 },
1051 {
1052 code: "foo\n/* this is pragmatic */",
1053 options: [{ applyDefaultIgnorePatterns: false, ignorePattern: "pragma" }]
1054 },
1055
1056 // Hashbang comment
1057 {
1058 code: "#!comment\n\nvar a = 1;",
1059 options: [{ afterHashbangComment: true }]
1060 },
1061 "#!comment\nvar a = 1;",
1062 {
1063 code: "#!comment\nvar a = 1;",
1064 options: [{}]
1065 },
1066 {
1067 code: "#!comment\nvar a = 1;",
1068 options: [{ afterHashbangComment: false }]
1069 },
1070 {
1071 code: "#!comment\nvar a = 1;",
1072 options: [{ afterLineComment: true, afterBlockComment: true }]
1073 }
1074 ],
1075
1076 invalid: [
1077
1078 // default rules
1079 {
1080 code: "bar()\n/** block block block\n * block \n */\nvar a = 1;",
1081 output: "bar()\n\n/** block block block\n * block \n */\nvar a = 1;",
1082 errors: [{ messageId: "before", type: "Block" }]
1083 },
1084
1085 // line comments
1086 {
1087 code: "baz()\n// A line comment with no empty line after\nvar a = 1;",
1088 output: "baz()\n// A line comment with no empty line after\n\nvar a = 1;",
1089 options: [{ afterLineComment: true }],
1090 errors: [{ messageId: "after", type: "Line" }]
1091 },
1092 {
1093 code: "baz()\n// A line comment with no empty line after\nvar a = 1;",
1094 output: "baz()\n\n// A line comment with no empty line after\nvar a = 1;",
1095 options: [{ beforeLineComment: true, afterLineComment: false }],
1096 errors: [{ messageId: "before", type: "Line" }]
1097 },
1098 {
1099 code: "// A line comment with no empty line after\nvar a = 1;",
1100 output: "// A line comment with no empty line after\n\nvar a = 1;",
1101 options: [{ beforeLineComment: true, afterLineComment: true }],
1102 errors: [{ messageId: "after", type: "Line", line: 1, column: 1 }]
1103 },
1104 {
1105 code: "baz()\n// A line comment with no empty line after\nvar a = 1;",
1106 output: "baz()\n\n// A line comment with no empty line after\n\nvar a = 1;",
1107 options: [{ beforeLineComment: true, afterLineComment: true }],
1108 errors: [{ messageId: "before", type: "Line", line: 2 }, { messageId: "after", type: "Line", line: 2 }]
1109 },
1110
1111 // block comments
1112 {
1113 code: "bar()\n/**\n * block block block\n */\nvar a = 1;",
1114 output: "bar()\n\n/**\n * block block block\n */\n\nvar a = 1;",
1115 options: [{ afterBlockComment: true, beforeBlockComment: true }],
1116 errors: [{ messageId: "before", type: "Block", line: 2 }, { messageId: "after", type: "Block", line: 2 }]
1117 },
1118 {
1119 code: "bar()\n/* first block comment */ /* second block comment */\nvar a = 1;",
1120 output: "bar()\n\n/* first block comment */ /* second block comment */\n\nvar a = 1;",
1121 options: [{ afterBlockComment: true, beforeBlockComment: true }],
1122 errors: [
1123 { messageId: "before", type: "Block", line: 2 },
1124 { messageId: "after", type: "Block", line: 2 }
1125 ]
1126 },
1127 {
1128 code: "bar()\n/* first block comment */ /* second block\n comment */\nvar a = 1;",
1129 output: "bar()\n\n/* first block comment */ /* second block\n comment */\n\nvar a = 1;",
1130 options: [{ afterBlockComment: true, beforeBlockComment: true }],
1131 errors: [
1132 { messageId: "before", type: "Block", line: 2 },
1133 { messageId: "after", type: "Block", line: 2 }
1134 ]
1135 },
1136 {
1137 code: "bar()\n/**\n * block block block\n */\nvar a = 1;",
1138 output: "bar()\n/**\n * block block block\n */\n\nvar a = 1;",
1139 options: [{ afterBlockComment: true, beforeBlockComment: false }],
1140 errors: [{ messageId: "after", type: "Block", line: 2 }]
1141 },
1142 {
1143 code: "bar()\n/**\n * block block block\n */\nvar a = 1;",
1144 output: "bar()\n\n/**\n * block block block\n */\nvar a = 1;",
1145 options: [{ afterBlockComment: false, beforeBlockComment: true }],
1146 errors: [{ messageId: "before", type: "Block", line: 2 }]
1147 },
1148 {
1149 code: "var a,\n// line\nb;",
1150 output: "var a,\n\n// line\nb;",
1151 options: [{
1152 beforeLineComment: true,
1153 allowBlockStart: true
1154 }],
1155 errors: [{ messageId: "before", type: "Line", line: 2 }]
1156 },
1157 {
1158 code: "function foo(){\nvar a = 1;\n// line at block start\nvar g = 1;\n}",
1159 output: "function foo(){\nvar a = 1;\n\n// line at block start\nvar g = 1;\n}",
1160 options: [{
1161 beforeLineComment: true,
1162 allowBlockStart: true
1163 }],
1164 errors: [{ messageId: "before", type: "Line", line: 3 }]
1165 },
1166 {
1167 code: "var a,\n// line\nb;",
1168 output: "var a,\n// line\n\nb;",
1169 options: [{
1170 afterLineComment: true,
1171 allowBlockEnd: true
1172 }],
1173 errors: [{ messageId: "after", type: "Line", line: 2 }]
1174 },
1175 {
1176 code: "function foo(){\nvar a = 1;\n\n// line at block start\nvar g = 1;\n}",
1177 output: "function foo(){\nvar a = 1;\n\n// line at block start\n\nvar g = 1;\n}",
1178 options: [{
1179 afterLineComment: true,
1180 allowBlockEnd: true
1181 }],
1182 errors: [{ messageId: "after", type: "Line", line: 4 }]
1183 },
1184 {
1185 code: "switch ('foo'){\ncase 'foo':\n// line at switch case start\nbreak;\n}",
1186 output: "switch ('foo'){\ncase 'foo':\n\n// line at switch case start\nbreak;\n}",
1187 options: [{
1188 beforeLineComment: true
1189 }],
1190 errors: [{ messageId: "before", type: "Line", line: 3 }]
1191 },
1192 {
1193 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\n// line at switch case start\nbreak;\n}",
1194 output: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\n\n// line at switch case start\nbreak;\n}",
1195 options: [{
1196 beforeLineComment: true
1197 }],
1198 errors: [{ messageId: "before", type: "Line", line: 6 }]
1199 },
1200 {
1201 code: "while(true){\n// line at block start and end\n}",
1202 output: "while(true){\n// line at block start and end\n\n}",
1203 options: [{
1204 afterLineComment: true,
1205 allowBlockStart: true
1206 }],
1207 errors: [{ messageId: "after", type: "Line", line: 2 }]
1208 },
1209 {
1210 code: "while(true){\n// line at block start and end\n}",
1211 output: "while(true){\n\n// line at block start and end\n}",
1212 options: [{
1213 beforeLineComment: true,
1214 allowBlockEnd: true
1215 }],
1216 errors: [{ messageId: "before", type: "Line", line: 2 }]
1217 },
1218 {
1219 code: "class A {\n// line at class start\nconstructor() {}\n}",
1220 output: "class A {\n\n// line at class start\nconstructor() {}\n}",
1221 options: [{
1222 beforeLineComment: true
1223 }],
1224 parserOptions: { ecmaVersion: 6 },
1225 errors: [{ messageId: "before", type: "Line", line: 2 }]
1226 },
1227 {
1228 code: "class A {\n// line at class start\nconstructor() {}\n}",
1229 output: "class A {\n\n// line at class start\nconstructor() {}\n}",
1230 options: [{
1231 allowBlockStart: true,
1232 allowClassStart: false,
1233 beforeLineComment: true
1234 }],
1235 parserOptions: { ecmaVersion: 6 },
1236 errors: [{ messageId: "before", type: "Line", line: 2 }]
1237 },
1238 {
1239 code: "class B {\nconstructor() {}\n\n// line at class end\n}",
1240 output: "class B {\nconstructor() {}\n\n// line at class end\n\n}",
1241 options: [{
1242 afterLineComment: true
1243 }],
1244 parserOptions: { ecmaVersion: 6 },
1245 errors: [{ messageId: "after", type: "Line", line: 4 }]
1246 },
1247 {
1248 code: "class B {\nconstructor() {}\n\n// line at class end\n}",
1249 output: "class B {\nconstructor() {}\n\n// line at class end\n\n}",
1250 options: [{
1251 afterLineComment: true,
1252 allowBlockEnd: true,
1253 allowClassEnd: false
1254 }],
1255 parserOptions: { ecmaVersion: 6 },
1256 errors: [{ messageId: "after", type: "Line", line: 4 }]
1257 },
1258 {
1259 code: "switch ('foo'){\ncase 'foo':\nvar g = 1;\n\n// line at switch case end\n}",
1260 output: "switch ('foo'){\ncase 'foo':\nvar g = 1;\n\n// line at switch case end\n\n}",
1261 options: [{
1262 afterLineComment: true
1263 }],
1264 errors: [{ messageId: "after", type: "Line", line: 5 }]
1265 },
1266 {
1267 code: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\nvar g = 1;\n\n// line at switch case end\n}",
1268 output: "switch ('foo'){\ncase 'foo':\nbreak;\n\ndefault:\nvar g = 1;\n\n// line at switch case end\n\n}",
1269 options: [{
1270 afterLineComment: true
1271 }],
1272 errors: [{ messageId: "after", type: "Line", line: 8 }]
1273 },
1274 {
1275 code: unIndent`
1276 class C {
1277 // line comment
1278 static{}
1279 }`,
1280 output: unIndent`
1281 class C {
1282 // line comment
1283
1284 static{}
1285 }`,
1286 options: [{
1287 beforeLineComment: true,
1288 afterLineComment: true,
1289 allowBlockStart: true,
1290 allowBlockEnd: true,
1291 allowClassStart: true,
1292 allowClassEnd: true
1293 }],
1294 parserOptions: { ecmaVersion: 2022 },
1295 errors: [
1296 { messageId: "after", type: "Line", line: 2 }
1297 ]
1298 },
1299 {
1300 code: unIndent`
1301 class C {
1302 /* block
1303 comment */
1304 static{}
1305 }`,
1306 output: unIndent`
1307 class C {
1308 /* block
1309 comment */
1310
1311 static{}
1312 }`,
1313 options: [{
1314 beforeBlockComment: true,
1315 afterBlockComment: true,
1316 allowBlockStart: true,
1317 allowBlockEnd: true,
1318 allowClassStart: true,
1319 allowClassEnd: true
1320 }],
1321 parserOptions: { ecmaVersion: 2022 },
1322 errors: [
1323 { messageId: "after", type: "Block", line: 2 }
1324 ]
1325 },
1326 {
1327 code: unIndent`
1328 class C {
1329 static
1330 // line comment
1331 {}
1332 }`,
1333 output: unIndent`
1334 class C {
1335 static
1336
1337 // line comment
1338
1339 {}
1340 }`,
1341 options: [{
1342 beforeLineComment: true,
1343 afterLineComment: true,
1344 allowBlockStart: true,
1345 allowBlockEnd: true,
1346 allowClassStart: true,
1347 allowClassEnd: true
1348 }],
1349 parserOptions: { ecmaVersion: 2022 },
1350 errors: [
1351 { messageId: "before", type: "Line", line: 3 },
1352 { messageId: "after", type: "Line", line: 3 }
1353 ]
1354 },
1355 {
1356 code: unIndent`
1357 class C {
1358 static
1359 /* block
1360 comment */
1361 {}
1362 }`,
1363 output: unIndent`
1364 class C {
1365 static
1366
1367 /* block
1368 comment */
1369
1370 {}
1371 }`,
1372 options: [{
1373 beforeBlockComment: true,
1374 afterBlockComment: true,
1375 allowBlockStart: true,
1376 allowBlockEnd: true,
1377 allowClassStart: true,
1378 allowClassEnd: true
1379 }],
1380 parserOptions: { ecmaVersion: 2022 },
1381 errors: [
1382 { messageId: "before", type: "Block", line: 3 },
1383 { messageId: "after", type: "Block", line: 3 }
1384 ]
1385 },
1386 {
1387 code: unIndent`
1388 class C {
1389 static {
1390 // line comment
1391 foo();
1392 }
1393 }`,
1394 output: unIndent`
1395 class C {
1396 static {
1397 // line comment
1398
1399 foo();
1400 }
1401 }`,
1402 options: [{
1403 beforeLineComment: true,
1404 afterLineComment: true,
1405 allowBlockStart: true,
1406 allowBlockEnd: true
1407 }],
1408 parserOptions: { ecmaVersion: 2022 },
1409 errors: [
1410 { messageId: "after", type: "Line", line: 3 }
1411 ]
1412 },
1413 {
1414 code: unIndent`
1415 class C {
1416 static {
1417 /* block
1418 comment */
1419 foo();
1420 }
1421 }`,
1422 output: unIndent`
1423 class C {
1424 static {
1425 /* block
1426 comment */
1427
1428 foo();
1429 }
1430 }`,
1431 options: [{
1432 beforeBlockComment: true,
1433 afterBlockComment: true,
1434 allowBlockStart: true,
1435 allowBlockEnd: true
1436 }],
1437 parserOptions: { ecmaVersion: 2022 },
1438 errors: [
1439 { messageId: "after", type: "Block", line: 3 }
1440 ]
1441 },
1442 {
1443 code: unIndent`
1444 class C {
1445 static {
1446 foo();
1447 // line comment
1448 }
1449 }`,
1450 output: unIndent`
1451 class C {
1452 static {
1453 foo();
1454
1455 // line comment
1456 }
1457 }`,
1458 options: [{
1459 beforeLineComment: true,
1460 afterLineComment: true,
1461 allowBlockStart: true,
1462 allowBlockEnd: true
1463 }],
1464 parserOptions: { ecmaVersion: 2022 },
1465 errors: [
1466 { messageId: "before", type: "Line", line: 4 }
1467 ]
1468 },
1469 {
1470 code: unIndent`
1471 class C {
1472 static {
1473 foo();
1474 /* block
1475 comment */
1476 }
1477 }`,
1478 output: unIndent`
1479 class C {
1480 static {
1481 foo();
1482
1483 /* block
1484 comment */
1485 }
1486 }`,
1487 options: [{
1488 beforeBlockComment: true,
1489 afterBlockComment: true,
1490 allowBlockStart: true,
1491 allowBlockEnd: true
1492 }],
1493 parserOptions: { ecmaVersion: 2022 },
1494 errors: [
1495 { messageId: "before", type: "Block", line: 4 }
1496 ]
1497 },
1498 {
1499 code: unIndent`
1500 class C {
1501 static {
1502 foo();
1503 // line comment
1504 bar();
1505 }
1506 }`,
1507 output: unIndent`
1508 class C {
1509 static {
1510 foo();
1511
1512 // line comment
1513
1514 bar();
1515 }
1516 }`,
1517 options: [{
1518 beforeLineComment: true,
1519 afterLineComment: true,
1520 allowBlockStart: true,
1521 allowBlockEnd: true
1522 }],
1523 parserOptions: { ecmaVersion: 2022 },
1524 errors: [
1525 { messageId: "before", type: "Line", line: 4 },
1526 { messageId: "after", type: "Line", line: 4 }
1527 ]
1528 },
1529 {
1530 code: unIndent`
1531 class C {
1532 static {
1533 foo();
1534 /* block
1535 comment */
1536 bar();
1537 }
1538 }`,
1539 output: unIndent`
1540 class C {
1541 static {
1542 foo();
1543
1544 /* block
1545 comment */
1546
1547 bar();
1548 }
1549 }`,
1550 options: [{
1551 beforeBlockComment: true,
1552 afterBlockComment: true,
1553 allowBlockStart: true,
1554 allowBlockEnd: true
1555 }],
1556 parserOptions: { ecmaVersion: 2022 },
1557 errors: [
1558 { messageId: "before", type: "Block", line: 4 },
1559 { messageId: "after", type: "Block", line: 4 }
1560 ]
1561 },
1562 {
1563 code: unIndent`
1564 class C {
1565 static{}
1566 // line comment
1567 }`,
1568 output: unIndent`
1569 class C {
1570 static{}
1571
1572 // line comment
1573 }`,
1574 options: [{
1575 beforeLineComment: true,
1576 afterLineComment: true,
1577 allowBlockStart: true,
1578 allowBlockEnd: true,
1579 allowClassStart: true,
1580 allowClassEnd: true
1581 }],
1582 parserOptions: { ecmaVersion: 2022 },
1583 errors: [
1584 { messageId: "before", type: "Line", line: 3 }
1585 ]
1586 },
1587 {
1588 code: unIndent`
1589 class C {
1590 static{}
1591 /* block
1592 comment */
1593 }`,
1594 output: unIndent`
1595 class C {
1596 static{}
1597
1598 /* block
1599 comment */
1600 }`,
1601 options: [{
1602 beforeBlockComment: true,
1603 afterBlockComment: true,
1604 allowBlockStart: true,
1605 allowBlockEnd: true,
1606 allowClassStart: true,
1607 allowClassEnd: true
1608 }],
1609 parserOptions: { ecmaVersion: 2022 },
1610 errors: [
1611 { messageId: "before", type: "Block", line: 3 }
1612 ]
1613 },
1614
1615 // object start comments
1616 {
1617 code:
1618 "var obj = {\n" +
1619 " // line at object start\n" +
1620 " g: 1\n" +
1621 "};",
1622 output:
1623 "var obj = {\n" +
1624 "\n" +
1625 " // line at object start\n" +
1626 " g: 1\n" +
1627 "};",
1628 options: [{
1629 beforeLineComment: true
1630 }],
1631 errors: [{ messageId: "before", type: "Line", line: 2 }]
1632 },
1633 {
1634 code:
1635 "function hi() {\n" +
1636 " return {\n" +
1637 " // hi\n" +
1638 " test: function() {\n" +
1639 " }\n" +
1640 " }\n" +
1641 "}",
1642 output:
1643 "function hi() {\n" +
1644 " return {\n" +
1645 "\n" +
1646 " // hi\n" +
1647 " test: function() {\n" +
1648 " }\n" +
1649 " }\n" +
1650 "}",
1651 options: [{
1652 beforeLineComment: true
1653 }],
1654 errors: [{ messageId: "before", type: "Line", line: 3 }]
1655 },
1656 {
1657 code:
1658 "var obj = {\n" +
1659 " /* block comment at object start*/\n" +
1660 " g: 1\n" +
1661 "};",
1662 output:
1663 "var obj = {\n" +
1664 "\n" +
1665 " /* block comment at object start*/\n" +
1666 " g: 1\n" +
1667 "};",
1668 options: [{
1669 beforeBlockComment: true
1670 }],
1671 errors: [{ messageId: "before", type: "Block", line: 2 }]
1672 },
1673 {
1674 code:
1675 "function hi() {\n" +
1676 " return {\n" +
1677 " /**\n" +
1678 " * hi\n" +
1679 " */\n" +
1680 " test: function() {\n" +
1681 " }\n" +
1682 " }\n" +
1683 "}",
1684 output:
1685 "function hi() {\n" +
1686 " return {\n" +
1687 "\n" +
1688 " /**\n" +
1689 " * hi\n" +
1690 " */\n" +
1691 " test: function() {\n" +
1692 " }\n" +
1693 " }\n" +
1694 "}",
1695 options: [{
1696 beforeLineComment: true
1697 }],
1698 errors: [{ messageId: "before", type: "Block", line: 3 }]
1699 },
1700 {
1701 code:
1702 "const {\n" +
1703 " // line at object start\n" +
1704 " g: a\n" +
1705 "} = {};",
1706 output:
1707 "const {\n" +
1708 "\n" +
1709 " // line at object start\n" +
1710 " g: a\n" +
1711 "} = {};",
1712 options: [{
1713 beforeLineComment: true
1714 }],
1715 parserOptions: { ecmaVersion: 6 },
1716 errors: [{ messageId: "before", type: "Line", line: 2 }]
1717 },
1718 {
1719 code:
1720 "const {\n" +
1721 " // line at object start\n" +
1722 " g\n" +
1723 "} = {};",
1724 output:
1725 "const {\n" +
1726 "\n" +
1727 " // line at object start\n" +
1728 " g\n" +
1729 "} = {};",
1730 options: [{
1731 beforeLineComment: true
1732 }],
1733 parserOptions: { ecmaVersion: 6 },
1734 errors: [{ messageId: "before", type: "Line", line: 2 }]
1735 },
1736 {
1737 code:
1738 "const {\n" +
1739 " /* block comment at object-like start*/\n" +
1740 " g: a\n" +
1741 "} = {};",
1742 output:
1743 "const {\n" +
1744 "\n" +
1745 " /* block comment at object-like start*/\n" +
1746 " g: a\n" +
1747 "} = {};",
1748 options: [{
1749 beforeBlockComment: true
1750 }],
1751 parserOptions: { ecmaVersion: 6 },
1752 errors: [{ messageId: "before", type: "Block", line: 2 }]
1753 },
1754 {
1755 code:
1756 "const {\n" +
1757 " /* block comment at object-like start*/\n" +
1758 " g\n" +
1759 "} = {};",
1760 output:
1761 "const {\n" +
1762 "\n" +
1763 " /* block comment at object-like start*/\n" +
1764 " g\n" +
1765 "} = {};",
1766 options: [{
1767 beforeBlockComment: true
1768 }],
1769 parserOptions: { ecmaVersion: 6 },
1770 errors: [{ messageId: "before", type: "Block", line: 2 }]
1771 },
1772
1773 // object end comments
1774 {
1775 code:
1776 "var obj = {\n" +
1777 " g: 1\n" +
1778 " // line at object end\n" +
1779 "};",
1780 output:
1781 "var obj = {\n" +
1782 " g: 1\n" +
1783 " // line at object end\n" +
1784 "\n" +
1785 "};",
1786 options: [{
1787 afterLineComment: true
1788 }],
1789 errors: [{ messageId: "after", type: "Line", line: 3 }]
1790 },
1791 {
1792 code:
1793 "function hi() {\n" +
1794 " return {\n" +
1795 " test: function() {\n" +
1796 " }\n" +
1797 " // hi\n" +
1798 " }\n" +
1799 "}",
1800 output:
1801 "function hi() {\n" +
1802 " return {\n" +
1803 " test: function() {\n" +
1804 " }\n" +
1805 " // hi\n" +
1806 "\n" +
1807 " }\n" +
1808 "}",
1809 options: [{
1810 afterLineComment: true
1811 }],
1812 errors: [{ messageId: "after", type: "Line", line: 5 }]
1813 },
1814 {
1815 code:
1816 "var obj = {\n" +
1817 " g: 1\n" +
1818 " \n" +
1819 " /* block comment at object end*/\n" +
1820 "};",
1821 output:
1822 "var obj = {\n" +
1823 " g: 1\n" +
1824 " \n" +
1825 " /* block comment at object end*/\n" +
1826 "\n" +
1827 "};",
1828 options: [{
1829 afterBlockComment: true
1830 }],
1831 errors: [{ messageId: "after", type: "Block", line: 4 }]
1832 },
1833 {
1834 code:
1835 "function hi() {\n" +
1836 " return {\n" +
1837 " test: function() {\n" +
1838 " }\n" +
1839 " \n" +
1840 " /**\n" +
1841 " * hi\n" +
1842 " */\n" +
1843 " }\n" +
1844 "}",
1845 output:
1846 "function hi() {\n" +
1847 " return {\n" +
1848 " test: function() {\n" +
1849 " }\n" +
1850 " \n" +
1851 " /**\n" +
1852 " * hi\n" +
1853 " */\n" +
1854 "\n" +
1855 " }\n" +
1856 "}",
1857 options: [{
1858 afterBlockComment: true
1859 }],
1860 errors: [{ messageId: "after", type: "Block", line: 6 }]
1861 },
1862 {
1863 code:
1864 "const {\n" +
1865 " g: a\n" +
1866 " // line at object end\n" +
1867 "} = {};",
1868 output:
1869 "const {\n" +
1870 " g: a\n" +
1871 " // line at object end\n" +
1872 "\n" +
1873 "} = {};",
1874 options: [{
1875 afterLineComment: true
1876 }],
1877 parserOptions: { ecmaVersion: 6 },
1878 errors: [{ messageId: "after", type: "Line", line: 3 }]
1879 },
1880 {
1881 code:
1882 "const {\n" +
1883 " g\n" +
1884 " // line at object end\n" +
1885 "} = {};",
1886 output:
1887 "const {\n" +
1888 " g\n" +
1889 " // line at object end\n" +
1890 "\n" +
1891 "} = {};",
1892 options: [{
1893 afterLineComment: true
1894 }],
1895 parserOptions: { ecmaVersion: 6 },
1896 errors: [{ messageId: "after", type: "Line", line: 3 }]
1897 },
1898 {
1899 code:
1900 "const {\n" +
1901 " g: a\n" +
1902 " \n" +
1903 " /* block comment at object-like end*/\n" +
1904 "} = {};",
1905 output:
1906 "const {\n" +
1907 " g: a\n" +
1908 " \n" +
1909 " /* block comment at object-like end*/\n" +
1910 "\n" +
1911 "} = {};",
1912 options: [{
1913 afterBlockComment: true
1914 }],
1915 parserOptions: { ecmaVersion: 6 },
1916 errors: [{ messageId: "after", type: "Block", line: 4 }]
1917 },
1918 {
1919 code:
1920 "const {\n" +
1921 " g\n" +
1922 " \n" +
1923 " /* block comment at object-like end*/\n" +
1924 "} = {};",
1925 output:
1926 "const {\n" +
1927 " g\n" +
1928 " \n" +
1929 " /* block comment at object-like end*/\n" +
1930 "\n" +
1931 "} = {};",
1932 options: [{
1933 afterBlockComment: true
1934 }],
1935 parserOptions: { ecmaVersion: 6 },
1936 errors: [{ messageId: "after", type: "Block", line: 4 }]
1937 },
1938
1939 // array start comments
1940 {
1941 code:
1942 "var arr = [\n" +
1943 " // line at array start\n" +
1944 " 1\n" +
1945 "];",
1946 output:
1947 "var arr = [\n" +
1948 "\n" +
1949 " // line at array start\n" +
1950 " 1\n" +
1951 "];",
1952 options: [{
1953 beforeLineComment: true
1954 }],
1955 errors: [{ messageId: "before", type: "Line", line: 2 }]
1956 },
1957 {
1958 code:
1959 "var arr = [\n" +
1960 " /* block comment at array start*/\n" +
1961 " 1\n" +
1962 "];",
1963 output:
1964 "var arr = [\n" +
1965 "\n" +
1966 " /* block comment at array start*/\n" +
1967 " 1\n" +
1968 "];",
1969 options: [{
1970 beforeBlockComment: true
1971 }],
1972 errors: [{ messageId: "before", type: "Block", line: 2 }]
1973 },
1974 {
1975 code:
1976 "const [\n" +
1977 " // line at array start\n" +
1978 " a\n" +
1979 "] = [];",
1980 output:
1981 "const [\n" +
1982 "\n" +
1983 " // line at array start\n" +
1984 " a\n" +
1985 "] = [];",
1986 options: [{
1987 beforeLineComment: true
1988 }],
1989 parserOptions: { ecmaVersion: 6 },
1990 errors: [{ messageId: "before", type: "Line", line: 2 }]
1991 },
1992 {
1993 code:
1994 "const [\n" +
1995 " /* block comment at array start*/\n" +
1996 " a\n" +
1997 "] = [];",
1998 output:
1999 "const [\n" +
2000 "\n" +
2001 " /* block comment at array start*/\n" +
2002 " a\n" +
2003 "] = [];",
2004 options: [{
2005 beforeBlockComment: true
2006 }],
2007 parserOptions: { ecmaVersion: 6 },
2008 errors: [{ messageId: "before", type: "Block", line: 2 }]
2009 },
2010
2011 // array end comments
2012 {
2013 code:
2014 "var arr = [\n" +
2015 " 1\n" +
2016 " // line at array end\n" +
2017 "];",
2018 output:
2019 "var arr = [\n" +
2020 " 1\n" +
2021 " // line at array end\n" +
2022 "\n" +
2023 "];",
2024 options: [{
2025 afterLineComment: true
2026 }],
2027 errors: [{ messageId: "after", type: "Line", line: 3 }]
2028 },
2029 {
2030 code:
2031 "var arr = [\n" +
2032 " 1\n" +
2033 " \n" +
2034 " /* block comment at array end*/\n" +
2035 "];",
2036 output:
2037 "var arr = [\n" +
2038 " 1\n" +
2039 " \n" +
2040 " /* block comment at array end*/\n" +
2041 "\n" +
2042 "];",
2043 options: [{
2044 afterBlockComment: true
2045 }],
2046 errors: [{ messageId: "after", type: "Block", line: 4 }]
2047 },
2048 {
2049 code:
2050 "const [\n" +
2051 " a\n" +
2052 " // line at array end\n" +
2053 "] = [];",
2054 output:
2055 "const [\n" +
2056 " a\n" +
2057 " // line at array end\n" +
2058 "\n" +
2059 "] = [];",
2060 options: [{
2061 afterLineComment: true
2062 }],
2063 parserOptions: { ecmaVersion: 6 },
2064 errors: [{ messageId: "after", type: "Line", line: 3 }]
2065 },
2066 {
2067 code:
2068 "const [\n" +
2069 " a\n" +
2070 " \n" +
2071 " /* block comment at array end*/\n" +
2072 "] = [];",
2073 output:
2074 "const [\n" +
2075 " a\n" +
2076 " \n" +
2077 " /* block comment at array end*/\n" +
2078 "\n" +
2079 "] = [];",
2080 options: [{
2081 afterBlockComment: true
2082 }],
2083 parserOptions: { ecmaVersion: 6 },
2084 errors: [{ messageId: "after", type: "Block", line: 4 }]
2085 },
2086
2087 // ignorePattern
2088 {
2089 code:
2090 "foo;\n\n" +
2091 "/* eslint-disable no-underscore-dangle */\n\n" +
2092 "this._values = values;\n" +
2093 "this._values2 = true;\n" +
2094 "/* eslint-enable no-underscore-dangle */\n" +
2095 "bar",
2096 output:
2097 "foo;\n\n" +
2098 "/* eslint-disable no-underscore-dangle */\n\n" +
2099 "this._values = values;\n" +
2100 "this._values2 = true;\n" +
2101 "\n" +
2102 "/* eslint-enable no-underscore-dangle */\n" +
2103 "\n" +
2104 "bar",
2105 options: [{
2106 beforeBlockComment: true,
2107 afterBlockComment: true,
2108 applyDefaultIgnorePatterns: false
2109 }],
2110 errors: [
2111 { messageId: "before", type: "Block", line: 7 },
2112 { messageId: "after", type: "Block", line: 7 }
2113 ]
2114 },
2115 {
2116 code: "foo;\n/* eslint */",
2117 output: "foo;\n\n/* eslint */",
2118 options: [{ applyDefaultIgnorePatterns: false }],
2119 errors: [{ messageId: "before", type: "Block" }]
2120 },
2121 {
2122 code: "foo;\n/* jshint */",
2123 output: "foo;\n\n/* jshint */",
2124 options: [{ applyDefaultIgnorePatterns: false }],
2125 errors: [{ messageId: "before", type: "Block" }]
2126 },
2127 {
2128 code: "foo;\n/* jslint */",
2129 output: "foo;\n\n/* jslint */",
2130 options: [{ applyDefaultIgnorePatterns: false }],
2131 errors: [{ messageId: "before", type: "Block" }]
2132 },
2133 {
2134 code: "foo;\n/* istanbul */",
2135 output: "foo;\n\n/* istanbul */",
2136 options: [{ applyDefaultIgnorePatterns: false }],
2137 errors: [{ messageId: "before", type: "Block" }]
2138 },
2139 {
2140 code: "foo;\n/* global */",
2141 output: "foo;\n\n/* global */",
2142 options: [{ applyDefaultIgnorePatterns: false }],
2143 errors: [{ messageId: "before", type: "Block" }]
2144 },
2145 {
2146 code: "foo;\n/* globals */",
2147 output: "foo;\n\n/* globals */",
2148 options: [{ applyDefaultIgnorePatterns: false }],
2149 errors: [{ messageId: "before", type: "Block" }]
2150 },
2151 {
2152 code: "foo;\n/* exported */",
2153 output: "foo;\n\n/* exported */",
2154 options: [{ applyDefaultIgnorePatterns: false }],
2155 errors: [{ messageId: "before", type: "Block" }]
2156 },
2157 {
2158 code: "foo;\n/* jscs */",
2159 output: "foo;\n\n/* jscs */",
2160 options: [{ applyDefaultIgnorePatterns: false }],
2161 errors: [{ messageId: "before", type: "Block" }]
2162 },
2163 {
2164 code: "foo\n/* something else */",
2165 output: "foo\n\n/* something else */",
2166 options: [{ ignorePattern: "pragma" }],
2167 errors: [{ messageId: "before", type: "Block" }]
2168 },
2169 {
2170 code: "foo\n/* eslint */",
2171 output: "foo\n\n/* eslint */",
2172 options: [{ applyDefaultIgnorePatterns: false }],
2173 errors: [{ messageId: "before", type: "Block" }]
2174 },
2175
2176 // "fallthrough" patterns are not ignored by default
2177 {
2178 code: "foo;\n/* fallthrough */",
2179 output: "foo;\n\n/* fallthrough */",
2180 options: [],
2181 errors: [{ messageId: "before", type: "Block" }]
2182 },
2183 {
2184 code: `
2185 switch (
2186 // this comment is not allowed by allowBlockStart: true
2187
2188 foo
2189 )
2190 {
2191 case 1:
2192 bar();
2193 break;
2194 }
2195 `,
2196 output: `
2197 switch (
2198
2199 // this comment is not allowed by allowBlockStart: true
2200
2201 foo
2202 )
2203 {
2204 case 1:
2205 bar();
2206 break;
2207 }
2208 `,
2209 options: [{
2210 allowBlockStart: true,
2211 beforeLineComment: true,
2212 afterLineComment: true
2213 }],
2214 errors: [{ messageId: "before", type: "Line" }]
2215 },
2216
2217 // Hashbang comment
2218 {
2219 code: "#!foo\nvar a = 1;",
2220 output: "#!foo\n\nvar a = 1;",
2221 options: [{ afterHashbangComment: true }],
2222 errors: [{ messageId: "after", type: "Shebang" }]
2223 }
2224 ]
2225
2226 });