]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # enforce position of line comments (line-comment-position) |
2 | ||
3 | Line comments can be positioned above or beside code. This rule helps teams maintain a consistent style. | |
4 | ||
5 | ```js | |
6 | // above comment | |
7 | var foo = "bar"; // beside comment | |
8 | ``` | |
9 | ||
10 | ## Rule Details | |
11 | ||
12 | This rule enforces consistent position of line comments. Block comments are not affected by this rule. By default, this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`, `falls through`. | |
13 | ||
eb39fafa DC |
14 | ## Options |
15 | ||
16 | This rule takes one argument, which can be a string or an object. The string settings are the same as those of the `position` property (explained below). The object option has the following properties: | |
17 | ||
18 | ### position | |
19 | ||
20 | The `position` option has two settings: | |
21 | ||
22 | * `above` (default) enforces line comments only above code, in its own line. | |
23 | * `beside` enforces line comments only at the end of code lines. | |
24 | ||
25 | #### position: above | |
26 | ||
27 | Examples of **correct** code for the `{ "position": "above" }` option: | |
28 | ||
29 | ```js | |
30 | /*eslint line-comment-position: ["error", { "position": "above" }]*/ | |
31 | // valid comment | |
32 | 1 + 1; | |
33 | ``` | |
34 | ||
eb39fafa DC |
35 | Examples of **incorrect** code for the `{ "position": "above" }` option: |
36 | ||
37 | ```js | |
38 | /*eslint line-comment-position: ["error", { "position": "above" }]*/ | |
39 | 1 + 1; // invalid comment | |
40 | ``` | |
41 | ||
42 | #### position: beside | |
43 | ||
44 | Examples of **correct** code for the `{ "position": "beside" }` option: | |
45 | ||
46 | ```js | |
47 | /*eslint line-comment-position: ["error", { "position": "beside" }]*/ | |
48 | 1 + 1; // valid comment | |
49 | ``` | |
50 | ||
eb39fafa DC |
51 | Examples of **incorrect** code for the `{ "position": "beside" }` option: |
52 | ||
53 | ```js | |
54 | /*eslint line-comment-position: ["error", { "position": "beside" }]*/ | |
55 | // invalid comment | |
56 | 1 + 1; | |
57 | ``` | |
58 | ||
59 | ### ignorePattern | |
60 | ||
61 | By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`, `falls through`. An alternative regular expression can be provided. | |
62 | ||
63 | Examples of **correct** code for the `ignorePattern` option: | |
64 | ||
65 | ```js | |
66 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/ | |
67 | 1 + 1; // pragma valid comment | |
68 | ``` | |
69 | ||
70 | Examples of **incorrect** code for the `ignorePattern` option: | |
71 | ||
72 | ```js | |
73 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/ | |
74 | 1 + 1; // invalid comment | |
75 | ``` | |
76 | ||
77 | ### applyDefaultIgnorePatterns | |
78 | ||
79 | Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`. | |
80 | ||
81 | Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option: | |
82 | ||
83 | ```js | |
84 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/ | |
85 | 1 + 1; // pragma valid comment | |
86 | ``` | |
87 | ||
88 | Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option: | |
89 | ||
90 | ```js | |
91 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/ | |
92 | 1 + 1; // falls through | |
93 | ``` | |
94 | ||
95 | **Deprecated:** the object property `applyDefaultPatterns` is deprecated. Please use the property `applyDefaultIgnorePatterns` instead. | |
96 | ||
97 | ## When Not To Use It | |
98 | ||
99 | If you aren't concerned about having different line comment styles, then you can turn off this rule. | |
100 | ||
101 | ## Compatibility | |
102 | ||
103 | **JSCS**: [validateCommentPosition](https://jscs-dev.github.io/rule/validateCommentPosition) |