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
19 * `"allowBlockEnd": true` allows comments to appear at the end of block statements
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 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowBlockStart": true }` options:
141 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/
144 /* what a great and wonderful day */
152 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowBlockEnd": true }` option:
155 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/
160 // what a great and wonderful day
164 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowBlockEnd": true }` option:
167 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/
173 /* what a great and wonderful day */
179 Examples of **incorrect** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": false }` option:
182 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/
185 // what a great and wonderful day
190 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": false }` option:
193 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/
197 // what a great and wonderful day
202 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": true }` option:
205 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/
208 // what a great and wonderful day
213 Examples of **incorrect** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": false }` option:
216 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/
219 /* what a great and wonderful day */
224 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": false }` option:
227 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/
231 /* what a great and wonderful day */
236 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": true }` option:
239 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/
242 /* what a great and wonderful day */
249 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowClassEnd": true }` option:
252 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/
256 // what a great and wonderful day
260 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowClassEnd": true }` option:
263 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/
268 /* what a great and wonderful day */
274 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowObjectStart": true }` option:
277 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/
280 // what a great and wonderful day
285 // what a great and wonderful day
290 // what a great and wonderful day
295 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowObjectStart": true }` option:
298 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/
301 /* what a great and wonderful day */
306 /* what a great and wonderful day */
311 /* what a great and wonderful day */
318 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowObjectEnd": true }` option:
321 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/
325 // what a great and wonderful day
330 // what a great and wonderful day
335 // what a great and wonderful day
339 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowObjectEnd": true }` option:
342 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/
347 /* what a great and wonderful day */
353 /* what a great and wonderful day */
359 /* what a great and wonderful day */
365 Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowArrayStart": true }` option:
368 /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/
371 // what a great and wonderful day
377 // what a great and wonderful day
379 ] = ["great", "not great"];
382 Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowArrayStart": true }` option:
385 /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/
388 /* what a great and wonderful day */
394 /* what a great and wonderful day */
396 ] = ["great", "not great"];
401 Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowArrayEnd": true }` option:
404 /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/
409 // what a great and wonderful day
414 // what a great and wonderful day
415 ] = ["great", "not great"];
418 Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowArrayEnd": true }` option:
421 /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/
427 /* what a great and wonderful day */
433 /* what a great and wonderful day */
434 ] = ["great", "not great"];
440 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).
442 Examples of **correct** code for the `ignorePattern` option:
445 /*eslint lines-around-comment: ["error"]*/
448 /* eslint mentioned in this comment */,
452 /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
455 /* a valid comment using pragma in it */
458 Examples of **incorrect** code for the `ignorePattern` option:
461 /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
467 ### applyDefaultIgnorePatterns
469 Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`.
471 Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option:
474 /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */
477 /* a valid comment using pragma in it */
480 Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option:
483 /*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */
486 /* eslint mentioned in comment */
491 ## When Not To Use It
493 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.
497 * [space-before-blocks](space-before-blocks.md)
498 * [spaced-comment](spaced-comment.md)