]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/spaced-comment.js
5d3d68fc06b4ebb2219d0215b386edf079e0799c
2 * @fileoverview Test for spaced-comments
3 * @author Gyandeep Singh
7 const rule
= require("../../../lib/rules/spaced-comment"),
8 { RuleTester
} = require("../../../lib/rule-tester");
10 const ruleTester
= new RuleTester(),
11 validShebangProgram
= "#!/path/to/node\nvar a = 3;";
13 ruleTester
.run("spaced-comment", rule
, {
17 code
: "// A valid comment starting with space\nvar a = 1;",
21 code
: "// A valid comment starting with tab\nvar a = 1;",
25 code
: "//A valid comment NOT starting with space\nvar a = 2;",
29 // exceptions - line comments
31 code
: "//-----------------------\n// A comment\n//-----------------------",
33 exceptions
: ["-", "=", "*", "#", "!@#"]
37 code
: "//-----------------------\n// A comment\n//-----------------------",
39 line
: { exceptions
: ["-", "=", "*", "#", "!@#"] }
43 code
: "//===========\n// A comment\n//*************",
45 exceptions
: ["-", "=", "*", "#", "!@#"]
49 code
: "//######\n// A comment",
51 exceptions
: ["-", "=", "*", "#", "!@#"]
55 code
: "//!@#!@#!@#\n// A comment\n//!@#",
57 exceptions
: ["-", "=", "*", "#", "!@#"]
61 // exceptions - block comments
63 code
: "var a = 1; /*######*/",
65 exceptions
: ["-", "=", "*", "#", "!@#"]
69 code
: "var a = 1; /*######*/",
71 block
: { exceptions
: ["-", "=", "*", "#", "!@#"] }
75 code
: "/*****************\n * A comment\n *****************/",
81 code
: "/*++++++++++++++\n * A comment\n +++++++++++++++++*/",
87 code
: "/*++++++++++++++\n + A comment\n * B comment\n - C comment\n----------------*/",
89 exceptions
: ["+", "-"]
93 // markers - line comments
95 code
: "//!< docblock style comment",
101 code
: "//!< docblock style comment",
102 options
: ["always", {
103 line
: { markers
: ["/", "!<"] }
107 code
: "//----\n// a comment\n//----\n/// xmldoc style comment\n//!< docblock style comment",
108 options
: ["always", {
115 options
: ["always", {
120 code
: "///xmldoc style comment",
126 // markers - block comments
128 code
: "var a = 1; /*# This is an example of a marker in a block comment\nsubsequent lines do not count*/",
129 options
: ["always", {
134 code
: "/*!\n *comment\n */",
135 options
: ["always", { markers
: ["!"] }]
138 code
: "/*!\n *comment\n */",
139 options
: ["always", { block
: { markers
: ["!"] } }]
142 code
: "/**\n *jsdoc\n */",
143 options
: ["always", { markers
: ["*"] }]
146 code
: "/*global ABC*/",
147 options
: ["always", { markers
: ["global"] }]
150 code
: "/*eslint-env node*/",
151 options
: ["always", { markers
: ["eslint-env"] }]
154 code
: "/*eslint eqeqeq:0, curly: 2*/",
155 options
: ["always", { markers
: ["eslint"] }]
158 code
: "/*eslint-disable no-alert, no-console */\nalert()\nconsole.log()\n/*eslint-enable no-alert */",
159 options
: ["always", { markers
: ["eslint-enable", "eslint-disable"] }]
164 code
: validShebangProgram
,
168 code
: validShebangProgram
,
180 code
: "// space only at start; valid since balanced doesn't apply to line comments",
181 options
: ["always", { block
: { balanced
: true } }]
184 code
: "//space only at end; valid since balanced doesn't apply to line comments ",
185 options
: ["never", { block
: { balanced
: true } }]
190 code
: "var a = 1; /* A valid comment starting with space */",
194 code
: "var a = 1; /*A valid comment NOT starting with space */",
198 code
: "function foo(/* height */a) { \n }",
202 code
: "function foo(/*height */a) { \n }",
206 code
: "function foo(a/* height */) { \n }",
210 code
: "/*\n * Test\n */",
214 code
: "/*\n *Test\n */",
218 code
: "/* \n *Test\n */",
222 code
: "/*\r\n *Test\r\n */",
226 code
: "/* \r\n *Test\r\n */",
230 code
: "/**\n *jsdoc\n */",
234 code
: "/**\r\n *jsdoc\r\n */",
238 code
: "/**\n *jsdoc\n */",
242 code
: "/** \n *jsdoc \n */",
246 // balanced block comments
248 code
: "var a = 1; /* comment */",
249 options
: ["always", { block
: { balanced
: true } }]
252 code
: "var a = 1; /*comment*/",
253 options
: ["never", { block
: { balanced
: true } }]
256 code
: "function foo(/* height */a) { \n }",
257 options
: ["always", { block
: { balanced
: true } }]
260 code
: "function foo(/*height*/a) { \n }",
261 options
: ["never", { block
: { balanced
: true } }]
264 code
: "var a = 1; /*######*/",
265 options
: ["always", {
266 exceptions
: ["-", "=", "*", "#", "!@#"],
267 block
: { balanced
: true }
271 code
: "/*****************\n * A comment\n *****************/",
272 options
: ["always", {
274 block
: { balanced
: true }
278 code
: "/*! comment */",
279 options
: ["always", { markers
: ["!"], block
: { balanced
: true } }]
282 code
: "/*!comment*/",
283 options
: ["never", { markers
: ["!"], block
: { balanced
: true } }]
286 code
: "/*!\n *comment\n */",
287 options
: ["always", { markers
: ["!"], block
: { balanced
: true } }]
290 code
: "/*global ABC */",
291 options
: ["always", { markers
: ["global"], block
: { balanced
: true } }]
294 // markers & exceptions
296 code
: "///--------\r\n/// test\r\n///--------",
297 options
: ["always", { markers
: ["/"], exceptions
: ["-"] }]
300 code
: "///--------\r\n/// test\r\n///--------\r\n/* blah */",
301 options
: ["always", { markers
: ["/"], exceptions
: ["-"], block
: { markers
: [] } }]
304 code
: "/***\u2028*/",
305 options
: ["always", { exceptions
: ["*"] }]
308 // ignore marker-only comments, https://github.com/eslint/eslint/issues/12036
310 code
: "//#endregion",
311 options
: ["always", { line
: { markers
: ["#endregion"] } }]
315 options
: ["always", { block
: { markers
: ["foo"] } }]
319 options
: ["always", { block
: { markers
: ["foo"], balanced
: true } }]
322 code
: "/*foo*/ /*bar*/",
323 options
: ["always", { markers
: ["foo", "bar"] }]
326 code
: "//foo\n//bar",
327 options
: ["always", { markers
: ["foo", "bar"] }]
331 options
: ["never", { markers
: [" foo "] }]
335 options
: ["never", { markers
: [" foo "] }]
338 code
: "//*", // "*" is a marker by default
342 code
: "/***/", // "*" is a marker by default
349 code
: "//An invalid comment NOT starting with space\nvar a = 1;",
350 output
: "// An invalid comment NOT starting with space\nvar a = 1;",
353 messageId
: "expectedSpaceAfter",
354 data
: { refChar
: "//" },
359 code
: "// An invalid comment starting with space\nvar a = 2;",
360 output
: "//An invalid comment starting with space\nvar a = 2;",
363 messageId
: "unexpectedSpaceAfter",
364 data
: { refChar
: "//" },
369 code
: "// An invalid comment starting with tab\nvar a = 2;",
370 output
: "//An invalid comment starting with tab\nvar a = 2;",
373 messageId
: "unexpectedSpaceAfter",
374 data
: { refChar
: "//" },
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 * `//*********************-`
385 code
: "//*********************-\n// Comment Block 3\n//***********************",
386 output
: "//* ********************-\n// Comment Block 3\n//***********************",
387 options
: ["always", {
388 exceptions
: ["-", "=", "*", "#", "!@#"]
391 messageId
: "expectedExceptionAfter",
392 data
: { refChar
: "//*" },
397 code
: "//-=-=-=-=-=-=\n// A comment\n//-=-=-=-=-=-=",
398 output
: "// -=-=-=-=-=-=\n// A comment\n// -=-=-=-=-=-=",
399 options
: ["always", {
400 exceptions
: ["-", "=", "*", "#", "!@#"]
404 messageId
: "expectedExceptionAfter",
405 data
: { refChar
: "//" },
409 messageId
: "expectedExceptionAfter",
410 data
: { refChar
: "//" },
416 code
: "//!<docblock style comment",
417 output
: "//!< docblock style comment",
418 options
: ["always", {
424 code
: "//!< docblock style comment",
425 output
: "//!<docblock style comment",
432 code
: "var a = 1; /* A valid comment starting with space */",
433 output
: "var a = 1; /*A valid comment starting with space */",
436 messageId
: "unexpectedSpaceAfter",
437 data
: { refChar
: "/*" },
442 code
: "var a = 1; /*######*/",
443 output
: "var a = 1; /* ######*/",
444 options
: ["always", {
445 exceptions
: ["-", "=", "*", "!@#"]
448 messageId
: "expectedExceptionAfter",
449 data
: { refChar
: "/*" },
454 code
: "var a = 1; /*A valid comment NOT starting with space */",
455 output
: "var a = 1; /* A valid comment NOT starting with space */",
458 messageId
: "expectedSpaceAfter",
459 data
: { refChar
: "/*" },
464 code
: "function foo(/* height */a) { \n }",
465 output
: "function foo(/*height */a) { \n }",
468 messageId
: "unexpectedSpaceAfter",
469 data
: { refChar
: "/*" },
474 code
: "function foo(/*height */a) { \n }",
475 output
: "function foo(/* height */a) { \n }",
478 messageId
: "expectedSpaceAfter",
479 data
: { refChar
: "/*" },
484 code
: "function foo(a/*height */) { \n }",
485 output
: "function foo(a/* height */) { \n }",
488 messageId
: "expectedSpaceAfter",
489 data
: { refChar
: "/*" },
494 code
: "/* \n *Test\n */",
495 output
: "/*\n *Test\n */",
498 messageId
: "unexpectedSpaceAfter",
499 data
: { refChar
: "/*" },
504 code
: "//-----------------------\n// A comment\n//-----------------------",
505 output
: "// -----------------------\n// A comment\n// -----------------------",
506 options
: ["always", {
507 block
: { exceptions
: ["-", "=", "*", "#", "!@#"] }
510 { messageId
: "expectedSpaceAfter", data
: { refChar
: "//" }, type
: "Line" },
511 { messageId
: "expectedSpaceAfter", data
: { refChar
: "//" }, type
: "Line" }
515 code
: "var a = 1; /*######*/",
516 output
: "var a = 1; /* ######*/",
517 options
: ["always", {
518 line
: { exceptions
: ["-", "=", "*", "#", "!@#"] }
521 messageId
: "expectedSpaceAfter",
522 data
: { refChar
: "/*" },
527 code
: "//!< docblock style comment",
528 output
: "// !< docblock style comment",
529 options
: ["always", {
530 block
: { markers
: ["/", "!<"] }
533 messageId
: "expectedSpaceAfter",
534 data
: { refChar
: "//" },
539 code
: "/*!\n *comment\n */",
540 output
: "/* !\n *comment\n */",
541 options
: ["always", { line
: { markers
: ["!"] } }],
543 messageId
: "expectedSpaceAfter",
544 data
: { refChar
: "/*" },
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
: [] } }],
553 messageId
: "expectedExceptionAfter",
554 data
: { refChar
: "/*" },
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
: ["-"] } }],
563 messageId
: "expectedSpaceAfter",
564 data
: { refChar
: "/*" },
569 messageId
: "expectedSpaceAfter",
570 data
: { refChar
: "/*" },
577 // balanced block comments
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 } }],
583 messageId
: "expectedSpaceBefore",
584 data
: { refChar
: "/**" },
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 } }],
593 messageId
: "unexpectedSpaceBefore",
594 data
: { refChar
: "*/" },
599 code
: "function foo(/* height*/a) { \n }",
600 output
: "function foo(/* height */a) { \n }",
601 options
: ["always", { block
: { balanced
: true } }],
603 messageId
: "expectedSpaceBefore",
604 data
: { refChar
: "/**" },
609 code
: "function foo(/*height */a) { \n }",
610 output
: "function foo(/*height*/a) { \n }",
611 options
: ["never", { block
: { balanced
: true } }],
613 messageId
: "unexpectedSpaceBefore",
614 data
: { refChar
: "*/" },
619 code
: "/*! comment*/",
620 output
: "/*! comment */",
621 options
: ["always", { markers
: ["!"], block
: { balanced
: true } }],
623 messageId
: "expectedSpaceBefore",
624 data
: { refChar
: "/**" },
629 code
: "/*!comment */",
630 output
: "/*!comment*/",
631 options
: ["never", { markers
: ["!"], block
: { balanced
: true } }],
633 messageId
: "unexpectedSpaceBefore",
634 data
: { refChar
: "*/" },
639 // not a marker-only comment, regression tests for https://github.com/eslint/eslint/issues/12036
641 code
: "//#endregionfoo",
642 output
: "//#endregion foo",
643 options
: ["always", { line
: { markers
: ["#endregion"] } }],
645 messageId
: "expectedSpaceAfter",
646 data
: { refChar
: "//#endregion" },
651 code
: "/*#endregion*/",
652 output
: "/* #endregion*/", // not an allowed marker for block comments
653 options
: ["always", { line
: { markers
: ["#endregion"] } }],
655 messageId
: "expectedSpaceAfter",
656 data
: { refChar
: "/*" },
665 messageId
: "expectedSpaceAfter",
666 data
: { refChar
: "/**" },
673 options
: ["always", { block
: { balanced
: true } }],
676 messageId
: "expectedSpaceAfter",
677 data
: { refChar
: "/**" },
681 messageId
: "expectedSpaceBefore",
682 data
: { refChar
: "*/" },
690 options
: ["never", { block
: { markers
: ["foo"], balanced
: true } }], // not " foo "
693 messageId
: "unexpectedSpaceAfter",
694 data
: { refChar
: "/*" },
698 messageId
: "unexpectedSpaceBefore",
699 data
: { refChar
: "*/" },