]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/spaced-comment.js
5d3d68fc06b4ebb2219d0215b386edf079e0799c
[pve-eslint.git] / eslint / tests / lib / rules / spaced-comment.js
1 /**
2 * @fileoverview Test for spaced-comments
3 * @author Gyandeep Singh
4 */
5 "use strict";
6
7 const rule = require("../../../lib/rules/spaced-comment"),
8 { RuleTester } = require("../../../lib/rule-tester");
9
10 const ruleTester = new RuleTester(),
11 validShebangProgram = "#!/path/to/node\nvar a = 3;";
12
13 ruleTester.run("spaced-comment", rule, {
14
15 valid: [
16 {
17 code: "// A valid comment starting with space\nvar a = 1;",
18 options: ["always"]
19 },
20 {
21 code: "// A valid comment starting with tab\nvar a = 1;",
22 options: ["always"]
23 },
24 {
25 code: "//A valid comment NOT starting with space\nvar a = 2;",
26 options: ["never"]
27 },
28
29 // exceptions - line comments
30 {
31 code: "//-----------------------\n// A comment\n//-----------------------",
32 options: ["always", {
33 exceptions: ["-", "=", "*", "#", "!@#"]
34 }]
35 },
36 {
37 code: "//-----------------------\n// A comment\n//-----------------------",
38 options: ["always", {
39 line: { exceptions: ["-", "=", "*", "#", "!@#"] }
40 }]
41 },
42 {
43 code: "//===========\n// A comment\n//*************",
44 options: ["always", {
45 exceptions: ["-", "=", "*", "#", "!@#"]
46 }]
47 },
48 {
49 code: "//######\n// A comment",
50 options: ["always", {
51 exceptions: ["-", "=", "*", "#", "!@#"]
52 }]
53 },
54 {
55 code: "//!@#!@#!@#\n// A comment\n//!@#",
56 options: ["always", {
57 exceptions: ["-", "=", "*", "#", "!@#"]
58 }]
59 },
60
61 // exceptions - block comments
62 {
63 code: "var a = 1; /*######*/",
64 options: ["always", {
65 exceptions: ["-", "=", "*", "#", "!@#"]
66 }]
67 },
68 {
69 code: "var a = 1; /*######*/",
70 options: ["always", {
71 block: { exceptions: ["-", "=", "*", "#", "!@#"] }
72 }]
73 },
74 {
75 code: "/*****************\n * A comment\n *****************/",
76 options: ["always", {
77 exceptions: ["*"]
78 }]
79 },
80 {
81 code: "/*++++++++++++++\n * A comment\n +++++++++++++++++*/",
82 options: ["always", {
83 exceptions: ["+"]
84 }]
85 },
86 {
87 code: "/*++++++++++++++\n + A comment\n * B comment\n - C comment\n----------------*/",
88 options: ["always", {
89 exceptions: ["+", "-"]
90 }]
91 },
92
93 // markers - line comments
94 {
95 code: "//!< docblock style comment",
96 options: ["always", {
97 markers: ["/", "!<"]
98 }]
99 },
100 {
101 code: "//!< docblock style comment",
102 options: ["always", {
103 line: { markers: ["/", "!<"] }
104 }]
105 },
106 {
107 code: "//----\n// a comment\n//----\n/// xmldoc style comment\n//!< docblock style comment",
108 options: ["always", {
109 exceptions: ["-"],
110 markers: ["/", "!<"]
111 }]
112 },
113 {
114 code: "/*\u2028x*/",
115 options: ["always", {
116 markers: ["/", "!<"]
117 }]
118 },
119 {
120 code: "///xmldoc style comment",
121 options: ["never", {
122 markers: ["/", "!<"]
123 }]
124 },
125
126 // markers - block comments
127 {
128 code: "var a = 1; /*# This is an example of a marker in a block comment\nsubsequent lines do not count*/",
129 options: ["always", {
130 markers: ["#"]
131 }]
132 },
133 {
134 code: "/*!\n *comment\n */",
135 options: ["always", { markers: ["!"] }]
136 },
137 {
138 code: "/*!\n *comment\n */",
139 options: ["always", { block: { markers: ["!"] } }]
140 },
141 {
142 code: "/**\n *jsdoc\n */",
143 options: ["always", { markers: ["*"] }]
144 },
145 {
146 code: "/*global ABC*/",
147 options: ["always", { markers: ["global"] }]
148 },
149 {
150 code: "/*eslint-env node*/",
151 options: ["always", { markers: ["eslint-env"] }]
152 },
153 {
154 code: "/*eslint eqeqeq:0, curly: 2*/",
155 options: ["always", { markers: ["eslint"] }]
156 },
157 {
158 code: "/*eslint-disable no-alert, no-console */\nalert()\nconsole.log()\n/*eslint-enable no-alert */",
159 options: ["always", { markers: ["eslint-enable", "eslint-disable"] }]
160 },
161
162 // misc. variations
163 {
164 code: validShebangProgram,
165 options: ["always"]
166 },
167 {
168 code: validShebangProgram,
169 options: ["never"]
170 },
171 {
172 code: "//",
173 options: ["always"]
174 },
175 {
176 code: "//\n",
177 options: ["always"]
178 },
179 {
180 code: "// space only at start; valid since balanced doesn't apply to line comments",
181 options: ["always", { block: { balanced: true } }]
182 },
183 {
184 code: "//space only at end; valid since balanced doesn't apply to line comments ",
185 options: ["never", { block: { balanced: true } }]
186 },
187
188 // block comments
189 {
190 code: "var a = 1; /* A valid comment starting with space */",
191 options: ["always"]
192 },
193 {
194 code: "var a = 1; /*A valid comment NOT starting with space */",
195 options: ["never"]
196 },
197 {
198 code: "function foo(/* height */a) { \n }",
199 options: ["always"]
200 },
201 {
202 code: "function foo(/*height */a) { \n }",
203 options: ["never"]
204 },
205 {
206 code: "function foo(a/* height */) { \n }",
207 options: ["always"]
208 },
209 {
210 code: "/*\n * Test\n */",
211 options: ["always"]
212 },
213 {
214 code: "/*\n *Test\n */",
215 options: ["never"]
216 },
217 {
218 code: "/* \n *Test\n */",
219 options: ["always"]
220 },
221 {
222 code: "/*\r\n *Test\r\n */",
223 options: ["never"]
224 },
225 {
226 code: "/* \r\n *Test\r\n */",
227 options: ["always"]
228 },
229 {
230 code: "/**\n *jsdoc\n */",
231 options: ["always"]
232 },
233 {
234 code: "/**\r\n *jsdoc\r\n */",
235 options: ["always"]
236 },
237 {
238 code: "/**\n *jsdoc\n */",
239 options: ["never"]
240 },
241 {
242 code: "/** \n *jsdoc \n */",
243 options: ["always"]
244 },
245
246 // balanced block comments
247 {
248 code: "var a = 1; /* comment */",
249 options: ["always", { block: { balanced: true } }]
250 },
251 {
252 code: "var a = 1; /*comment*/",
253 options: ["never", { block: { balanced: true } }]
254 },
255 {
256 code: "function foo(/* height */a) { \n }",
257 options: ["always", { block: { balanced: true } }]
258 },
259 {
260 code: "function foo(/*height*/a) { \n }",
261 options: ["never", { block: { balanced: true } }]
262 },
263 {
264 code: "var a = 1; /*######*/",
265 options: ["always", {
266 exceptions: ["-", "=", "*", "#", "!@#"],
267 block: { balanced: true }
268 }]
269 },
270 {
271 code: "/*****************\n * A comment\n *****************/",
272 options: ["always", {
273 exceptions: ["*"],
274 block: { balanced: true }
275 }]
276 },
277 {
278 code: "/*! comment */",
279 options: ["always", { markers: ["!"], block: { balanced: true } }]
280 },
281 {
282 code: "/*!comment*/",
283 options: ["never", { markers: ["!"], block: { balanced: true } }]
284 },
285 {
286 code: "/*!\n *comment\n */",
287 options: ["always", { markers: ["!"], block: { balanced: true } }]
288 },
289 {
290 code: "/*global ABC */",
291 options: ["always", { markers: ["global"], block: { balanced: true } }]
292 },
293
294 // markers & exceptions
295 {
296 code: "///--------\r\n/// test\r\n///--------",
297 options: ["always", { markers: ["/"], exceptions: ["-"] }]
298 },
299 {
300 code: "///--------\r\n/// test\r\n///--------\r\n/* blah */",
301 options: ["always", { markers: ["/"], exceptions: ["-"], block: { markers: [] } }]
302 },
303 {
304 code: "/***\u2028*/",
305 options: ["always", { exceptions: ["*"] }]
306 },
307
308 // ignore marker-only comments, https://github.com/eslint/eslint/issues/12036
309 {
310 code: "//#endregion",
311 options: ["always", { line: { markers: ["#endregion"] } }]
312 },
313 {
314 code: "/*foo*/",
315 options: ["always", { block: { markers: ["foo"] } }]
316 },
317 {
318 code: "/*foo*/",
319 options: ["always", { block: { markers: ["foo"], balanced: true } }]
320 },
321 {
322 code: "/*foo*/ /*bar*/",
323 options: ["always", { markers: ["foo", "bar"] }]
324 },
325 {
326 code: "//foo\n//bar",
327 options: ["always", { markers: ["foo", "bar"] }]
328 },
329 {
330 code: "/* foo */",
331 options: ["never", { markers: [" foo "] }]
332 },
333 {
334 code: "// foo ",
335 options: ["never", { markers: [" foo "] }]
336 },
337 {
338 code: "//*", // "*" is a marker by default
339 options: ["always"]
340 },
341 {
342 code: "/***/", // "*" is a marker by default
343 options: ["always"]
344 }
345 ],
346
347 invalid: [
348 {
349 code: "//An invalid comment NOT starting with space\nvar a = 1;",
350 output: "// An invalid comment NOT starting with space\nvar a = 1;",
351 options: ["always"],
352 errors: [{
353 messageId: "expectedSpaceAfter",
354 data: { refChar: "//" },
355 type: "Line"
356 }]
357 },
358 {
359 code: "// An invalid comment starting with space\nvar a = 2;",
360 output: "//An invalid comment starting with space\nvar a = 2;",
361 options: ["never"],
362 errors: [{
363 messageId: "unexpectedSpaceAfter",
364 data: { refChar: "//" },
365 type: "Line"
366 }]
367 },
368 {
369 code: "// An invalid comment starting with tab\nvar a = 2;",
370 output: "//An invalid comment starting with tab\nvar a = 2;",
371 options: ["never"],
372 errors: [{
373 messageId: "unexpectedSpaceAfter",
374 data: { refChar: "//" },
375 type: "Line"
376 }]
377 },
378 {
379
380 /*
381 * note that the first line in the comment is not a valid exception
382 * block pattern because of the minus sign at the end of the line:
383 * `//*********************-`
384 */
385 code: "//*********************-\n// Comment Block 3\n//***********************",
386 output: "//* ********************-\n// Comment Block 3\n//***********************",
387 options: ["always", {
388 exceptions: ["-", "=", "*", "#", "!@#"]
389 }],
390 errors: [{
391 messageId: "expectedExceptionAfter",
392 data: { refChar: "//*" },
393 type: "Line"
394 }]
395 },
396 {
397 code: "//-=-=-=-=-=-=\n// A comment\n//-=-=-=-=-=-=",
398 output: "// -=-=-=-=-=-=\n// A comment\n// -=-=-=-=-=-=",
399 options: ["always", {
400 exceptions: ["-", "=", "*", "#", "!@#"]
401 }],
402 errors: [
403 {
404 messageId: "expectedExceptionAfter",
405 data: { refChar: "//" },
406 type: "Line"
407 },
408 {
409 messageId: "expectedExceptionAfter",
410 data: { refChar: "//" },
411 type: "Line"
412 }
413 ]
414 },
415 {
416 code: "//!<docblock style comment",
417 output: "//!< docblock style comment",
418 options: ["always", {
419 markers: ["/", "!<"]
420 }],
421 errors: 1
422 },
423 {
424 code: "//!< docblock style comment",
425 output: "//!<docblock style comment",
426 options: ["never", {
427 markers: ["/", "!<"]
428 }],
429 errors: 1
430 },
431 {
432 code: "var a = 1; /* A valid comment starting with space */",
433 output: "var a = 1; /*A valid comment starting with space */",
434 options: ["never"],
435 errors: [{
436 messageId: "unexpectedSpaceAfter",
437 data: { refChar: "/*" },
438 type: "Block"
439 }]
440 },
441 {
442 code: "var a = 1; /*######*/",
443 output: "var a = 1; /* ######*/",
444 options: ["always", {
445 exceptions: ["-", "=", "*", "!@#"]
446 }],
447 errors: [{
448 messageId: "expectedExceptionAfter",
449 data: { refChar: "/*" },
450 type: "Block"
451 }]
452 },
453 {
454 code: "var a = 1; /*A valid comment NOT starting with space */",
455 output: "var a = 1; /* A valid comment NOT starting with space */",
456 options: ["always"],
457 errors: [{
458 messageId: "expectedSpaceAfter",
459 data: { refChar: "/*" },
460 type: "Block"
461 }]
462 },
463 {
464 code: "function foo(/* height */a) { \n }",
465 output: "function foo(/*height */a) { \n }",
466 options: ["never"],
467 errors: [{
468 messageId: "unexpectedSpaceAfter",
469 data: { refChar: "/*" },
470 type: "Block"
471 }]
472 },
473 {
474 code: "function foo(/*height */a) { \n }",
475 output: "function foo(/* height */a) { \n }",
476 options: ["always"],
477 errors: [{
478 messageId: "expectedSpaceAfter",
479 data: { refChar: "/*" },
480 type: "Block"
481 }]
482 },
483 {
484 code: "function foo(a/*height */) { \n }",
485 output: "function foo(a/* height */) { \n }",
486 options: ["always"],
487 errors: [{
488 messageId: "expectedSpaceAfter",
489 data: { refChar: "/*" },
490 type: "Block"
491 }]
492 },
493 {
494 code: "/* \n *Test\n */",
495 output: "/*\n *Test\n */",
496 options: ["never"],
497 errors: [{
498 messageId: "unexpectedSpaceAfter",
499 data: { refChar: "/*" },
500 type: "Block"
501 }]
502 },
503 {
504 code: "//-----------------------\n// A comment\n//-----------------------",
505 output: "// -----------------------\n// A comment\n// -----------------------",
506 options: ["always", {
507 block: { exceptions: ["-", "=", "*", "#", "!@#"] }
508 }],
509 errors: [
510 { messageId: "expectedSpaceAfter", data: { refChar: "//" }, type: "Line" },
511 { messageId: "expectedSpaceAfter", data: { refChar: "//" }, type: "Line" }
512 ]
513 },
514 {
515 code: "var a = 1; /*######*/",
516 output: "var a = 1; /* ######*/",
517 options: ["always", {
518 line: { exceptions: ["-", "=", "*", "#", "!@#"] }
519 }],
520 errors: [{
521 messageId: "expectedSpaceAfter",
522 data: { refChar: "/*" },
523 type: "Block"
524 }]
525 },
526 {
527 code: "//!< docblock style comment",
528 output: "// !< docblock style comment",
529 options: ["always", {
530 block: { markers: ["/", "!<"] }
531 }],
532 errors: [{
533 messageId: "expectedSpaceAfter",
534 data: { refChar: "//" },
535 type: "Line"
536 }]
537 },
538 {
539 code: "/*!\n *comment\n */",
540 output: "/* !\n *comment\n */",
541 options: ["always", { line: { markers: ["!"] } }],
542 errors: [{
543 messageId: "expectedSpaceAfter",
544 data: { refChar: "/*" },
545 type: "Block"
546 }]
547 },
548 {
549 code: "///--------\r\n/// test\r\n///--------\r\n/*/ blah *//*-----*/",
550 output: "///--------\r\n/// test\r\n///--------\r\n/* / blah *//*-----*/",
551 options: ["always", { markers: ["/"], exceptions: ["-"], block: { markers: [] } }],
552 errors: [{
553 messageId: "expectedExceptionAfter",
554 data: { refChar: "/*" },
555 type: "Block"
556 }]
557 },
558 {
559 code: "///--------\r\n/// test\r\n///--------\r\n/*/ blah */ /*-----*/",
560 output: "///--------\r\n/// test\r\n///--------\r\n/* / blah */ /* -----*/",
561 options: ["always", { line: { markers: ["/"], exceptions: ["-"] } }],
562 errors: [{
563 messageId: "expectedSpaceAfter",
564 data: { refChar: "/*" },
565 type: "Block",
566 line: 4,
567 column: 1
568 }, {
569 messageId: "expectedSpaceAfter",
570 data: { refChar: "/*" },
571 type: "Block",
572 line: 4,
573 column: 13
574 }]
575 },
576
577 // balanced block comments
578 {
579 code: "var a = 1; /* A balanced comment starting with space*/",
580 output: "var a = 1; /* A balanced comment starting with space */",
581 options: ["always", { block: { balanced: true } }],
582 errors: [{
583 messageId: "expectedSpaceBefore",
584 data: { refChar: "/**" },
585 type: "Block"
586 }]
587 },
588 {
589 code: "var a = 1; /*A balanced comment NOT starting with space */",
590 output: "var a = 1; /*A balanced comment NOT starting with space*/",
591 options: ["never", { block: { balanced: true } }],
592 errors: [{
593 messageId: "unexpectedSpaceBefore",
594 data: { refChar: "*/" },
595 type: "Block"
596 }]
597 },
598 {
599 code: "function foo(/* height*/a) { \n }",
600 output: "function foo(/* height */a) { \n }",
601 options: ["always", { block: { balanced: true } }],
602 errors: [{
603 messageId: "expectedSpaceBefore",
604 data: { refChar: "/**" },
605 type: "Block"
606 }]
607 },
608 {
609 code: "function foo(/*height */a) { \n }",
610 output: "function foo(/*height*/a) { \n }",
611 options: ["never", { block: { balanced: true } }],
612 errors: [{
613 messageId: "unexpectedSpaceBefore",
614 data: { refChar: "*/" },
615 type: "Block"
616 }]
617 },
618 {
619 code: "/*! comment*/",
620 output: "/*! comment */",
621 options: ["always", { markers: ["!"], block: { balanced: true } }],
622 errors: [{
623 messageId: "expectedSpaceBefore",
624 data: { refChar: "/**" },
625 type: "Block"
626 }]
627 },
628 {
629 code: "/*!comment */",
630 output: "/*!comment*/",
631 options: ["never", { markers: ["!"], block: { balanced: true } }],
632 errors: [{
633 messageId: "unexpectedSpaceBefore",
634 data: { refChar: "*/" },
635 type: "Block"
636 }]
637 },
638
639 // not a marker-only comment, regression tests for https://github.com/eslint/eslint/issues/12036
640 {
641 code: "//#endregionfoo",
642 output: "//#endregion foo",
643 options: ["always", { line: { markers: ["#endregion"] } }],
644 errors: [{
645 messageId: "expectedSpaceAfter",
646 data: { refChar: "//#endregion" },
647 type: "Line"
648 }]
649 },
650 {
651 code: "/*#endregion*/",
652 output: "/* #endregion*/", // not an allowed marker for block comments
653 options: ["always", { line: { markers: ["#endregion"] } }],
654 errors: [{
655 messageId: "expectedSpaceAfter",
656 data: { refChar: "/*" },
657 type: "Block"
658 }]
659 },
660 {
661 code: "/****/",
662 output: "/** **/",
663 options: ["always"],
664 errors: [{
665 messageId: "expectedSpaceAfter",
666 data: { refChar: "/**" },
667 type: "Block"
668 }]
669 },
670 {
671 code: "/****/",
672 output: "/** * */",
673 options: ["always", { block: { balanced: true } }],
674 errors: [
675 {
676 messageId: "expectedSpaceAfter",
677 data: { refChar: "/**" },
678 type: "Block"
679 },
680 {
681 messageId: "expectedSpaceBefore",
682 data: { refChar: "*/" },
683 type: "Block"
684 }
685 ]
686 },
687 {
688 code: "/* foo */",
689 output: "/*foo*/",
690 options: ["never", { block: { markers: ["foo"], balanced: true } }], // not " foo "
691 errors: [
692 {
693 messageId: "unexpectedSpaceAfter",
694 data: { refChar: "/*" },
695 type: "Block"
696 },
697 {
698 messageId: "unexpectedSpaceBefore",
699 data: { refChar: "*/" },
700 type: "Block"
701 }
702 ]
703 }
704 ]
705
706 });