1 # require empty lines around comments (lines-around-comment)
3 Many style guides require empty lines before or after comments. The primary goal
4 of these rules is to make the comments easier to read and improve readability of the code.
8 This rule requires empty lines before and/or after comments. It can be enabled separately for both block (`/*`) and line (`//`) comments. This rule does not apply to comments that appear on the same line as code and does not require empty lines at the beginning or end of a file.
12 This rule has an object option:
14 * `"beforeBlockComment": true` (default) requires an empty line before block comments
15 * `"afterBlockComment": true` requires an empty line after block comments
16 * `"beforeLineComment": true` requires an empty line before line comments
17 * `"afterLineComment": true` requires an empty line after line comments
18 * `"allowBlockStart": true` allows comments to appear at the start of block statements, function bodies, classes, and class static blocks
19 * `"allowBlockEnd": true` allows comments to appear at the end of block statements, function bodies, classes, and class static blocks
20 * `"allowObjectStart": true` allows comments to appear at the start of object literals
21 * `"allowObjectEnd": true` allows comments to appear at the end of object literals
22 * `"allowArrayStart": true` allows comments to appear at the start of array literals
23 * `"allowArrayEnd": true` allows comments to appear at the end of array literals
24 * `"allowClassStart": true` allows comments to appear at the start of classes
25 * `"allowClassEnd": true` allows comments to appear at the end of classes
26 * `"applyDefaultIgnorePatterns"` enables or disables the default comment patterns to be ignored by the rule
27 * `"ignorePattern"` custom patterns to be ignored by the rule
30 ### beforeBlockComment
32 Examples of **incorrect** code for this rule with the default `{ "beforeBlockComment": true }` option:
35 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
38 /* what a great and wonderful day */
42 Examples of **correct** code for this rule with the default `{ "beforeBlockComment": true }` option:
45 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
49 /* what a great and wonderful day */
55 Examples of **incorrect** code for this rule with the `{ "afterBlockComment": true }` option:
58 /*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/
62 /* what a great and wonderful day */
66 Examples of **correct** code for this rule with the `{ "afterBlockComment": true }` option:
69 /*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/
73 /* what a great and wonderful day */
80 Examples of **incorrect** code for this rule with the `{ "beforeLineComment": true }` option:
83 /*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/
86 // what a great and wonderful day
90 Examples of **correct** code for this rule with the `{ "beforeLineComment": true }` option:
93 /*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/
97 // what a great and wonderful day
103 Examples of **incorrect** code for this rule with the `{ "afterLineComment": true }` option:
106 /*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/
109 // what a great and wonderful day
113 Examples of **correct** code for this rule with the `{ "afterLineComment": true }` option:
116 /*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/
119 // what a great and wonderful day
126 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowBlockStart": true }` options:
129 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/
132 // what a great and wonderful day
138 // what a great and wonderful day
143 // what a great and wonderful day
146 // what a great and wonderful day
151 // what a great and wonderful day
157 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowBlockStart": true }` options:
160 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/
163 /* what a great and wonderful day */
169 /* what a great and wonderful day */
174 /* what a great and wonderful day */
177 /* what a great and wonderful day */
182 /* what a great and wonderful day */
190 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowBlockEnd": true }` option:
193 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/
198 // what a great and wonderful day
203 // what a great and wonderful day
210 // what a great and wonderful day
215 // what a great and wonderful day
218 // what a great and wonderful day
222 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowBlockEnd": true }` option:
225 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/
231 /* what a great and wonderful day */
237 /* what a great and wonderful day */
245 /* what a great and wonderful day */
251 /* what a great and wonderful day */
254 /* what a great and wonderful day */
260 Examples of **incorrect** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": false }` option:
263 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/
266 // what a great and wonderful day
271 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": false }` option:
274 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/
278 // what a great and wonderful day
283 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": true }` option:
286 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/
289 // what a great and wonderful day
294 Examples of **incorrect** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": false }` option:
297 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/
300 /* what a great and wonderful day */
305 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": false }` option:
308 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/
312 /* what a great and wonderful day */
317 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": true }` option:
320 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/
323 /* what a great and wonderful day */
330 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowClassEnd": true }` option:
333 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/
337 // what a great and wonderful day
341 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowClassEnd": true }` option:
344 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/
349 /* what a great and wonderful day */
355 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowObjectStart": true }` option:
358 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/
361 // what a great and wonderful day
366 // what a great and wonderful day
371 // what a great and wonderful day
376 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowObjectStart": true }` option:
379 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/
382 /* what a great and wonderful day */
387 /* what a great and wonderful day */
392 /* what a great and wonderful day */
399 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowObjectEnd": true }` option:
402 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/
406 // what a great and wonderful day
411 // what a great and wonderful day
416 // what a great and wonderful day
420 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowObjectEnd": true }` option:
423 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/
428 /* what a great and wonderful day */
434 /* what a great and wonderful day */
440 /* what a great and wonderful day */
446 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowArrayStart": true }` option:
449 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/
452 // what a great and wonderful day
458 // what a great and wonderful day
460 ] = ["great", "not great"];
463 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowArrayStart": true }` option:
466 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/
469 /* what a great and wonderful day */
475 /* what a great and wonderful day */
477 ] = ["great", "not great"];
482 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowArrayEnd": true }` option:
485 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/
490 // what a great and wonderful day
495 // what a great and wonderful day
496 ] = ["great", "not great"];
499 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowArrayEnd": true }` option:
502 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/
508 /* what a great and wonderful day */
514 /* what a great and wonderful day */
515 ] = ["great", "not great"];
521 By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`. To ignore more comments in addition to the defaults, set the `ignorePattern` option to a string pattern that will be passed to the [`RegExp` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp).
523 Examples of **correct** code for the `ignorePattern` option:
526 /*eslint lines-around-comment: ["error"]*/
529 /* eslint mentioned in this comment */,
533 /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
536 /* a valid comment using pragma in it */
539 Examples of **incorrect** code for the `ignorePattern` option:
542 /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
548 ### applyDefaultIgnorePatterns
550 Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`.
552 Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option:
555 /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */
558 /* a valid comment using pragma in it */
561 Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option:
564 /*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */
567 /* eslint mentioned in comment */
572 ## When Not To Use It
574 Many people enjoy a terser code style and don't mind comments bumping up against code. If you fall into that category this rule is not for you.
578 * [space-before-blocks](space-before-blocks.md)
579 * [spaced-comment](spaced-comment.md)