]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: line-comment-position | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | --- | |
5 | ||
eb39fafa DC |
6 | |
7 | Line comments can be positioned above or beside code. This rule helps teams maintain a consistent style. | |
8 | ||
9 | ```js | |
10 | // above comment | |
11 | var foo = "bar"; // beside comment | |
12 | ``` | |
13 | ||
14 | ## Rule Details | |
15 | ||
16 | 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`. | |
17 | ||
eb39fafa DC |
18 | ## Options |
19 | ||
20 | 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: | |
21 | ||
22 | ### position | |
23 | ||
24 | The `position` option has two settings: | |
25 | ||
26 | * `above` (default) enforces line comments only above code, in its own line. | |
27 | * `beside` enforces line comments only at the end of code lines. | |
28 | ||
29 | #### position: above | |
30 | ||
31 | Examples of **correct** code for the `{ "position": "above" }` option: | |
32 | ||
8f9d1d4d DC |
33 | ::: correct |
34 | ||
eb39fafa DC |
35 | ```js |
36 | /*eslint line-comment-position: ["error", { "position": "above" }]*/ | |
37 | // valid comment | |
38 | 1 + 1; | |
39 | ``` | |
40 | ||
8f9d1d4d DC |
41 | ::: |
42 | ||
eb39fafa DC |
43 | Examples of **incorrect** code for the `{ "position": "above" }` option: |
44 | ||
8f9d1d4d DC |
45 | ::: incorrect |
46 | ||
eb39fafa DC |
47 | ```js |
48 | /*eslint line-comment-position: ["error", { "position": "above" }]*/ | |
49 | 1 + 1; // invalid comment | |
50 | ``` | |
51 | ||
8f9d1d4d DC |
52 | ::: |
53 | ||
eb39fafa DC |
54 | #### position: beside |
55 | ||
56 | Examples of **correct** code for the `{ "position": "beside" }` option: | |
57 | ||
8f9d1d4d DC |
58 | ::: correct |
59 | ||
eb39fafa DC |
60 | ```js |
61 | /*eslint line-comment-position: ["error", { "position": "beside" }]*/ | |
62 | 1 + 1; // valid comment | |
63 | ``` | |
64 | ||
8f9d1d4d DC |
65 | ::: |
66 | ||
eb39fafa DC |
67 | Examples of **incorrect** code for the `{ "position": "beside" }` option: |
68 | ||
8f9d1d4d DC |
69 | ::: incorrect |
70 | ||
eb39fafa DC |
71 | ```js |
72 | /*eslint line-comment-position: ["error", { "position": "beside" }]*/ | |
73 | // invalid comment | |
74 | 1 + 1; | |
75 | ``` | |
76 | ||
8f9d1d4d DC |
77 | ::: |
78 | ||
eb39fafa DC |
79 | ### ignorePattern |
80 | ||
81 | 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. | |
82 | ||
83 | Examples of **correct** code for the `ignorePattern` option: | |
84 | ||
8f9d1d4d DC |
85 | ::: correct |
86 | ||
eb39fafa DC |
87 | ```js |
88 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/ | |
89 | 1 + 1; // pragma valid comment | |
90 | ``` | |
91 | ||
8f9d1d4d DC |
92 | ::: |
93 | ||
eb39fafa DC |
94 | Examples of **incorrect** code for the `ignorePattern` option: |
95 | ||
8f9d1d4d DC |
96 | ::: incorrect |
97 | ||
eb39fafa DC |
98 | ```js |
99 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/ | |
100 | 1 + 1; // invalid comment | |
101 | ``` | |
102 | ||
8f9d1d4d DC |
103 | ::: |
104 | ||
eb39fafa DC |
105 | ### applyDefaultIgnorePatterns |
106 | ||
107 | Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`. | |
108 | ||
109 | Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option: | |
110 | ||
8f9d1d4d DC |
111 | ::: correct |
112 | ||
eb39fafa DC |
113 | ```js |
114 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/ | |
115 | 1 + 1; // pragma valid comment | |
116 | ``` | |
117 | ||
8f9d1d4d DC |
118 | ::: |
119 | ||
eb39fafa DC |
120 | Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option: |
121 | ||
8f9d1d4d DC |
122 | ::: incorrect |
123 | ||
eb39fafa DC |
124 | ```js |
125 | /*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/ | |
126 | 1 + 1; // falls through | |
127 | ``` | |
128 | ||
8f9d1d4d DC |
129 | ::: |
130 | ||
eb39fafa DC |
131 | **Deprecated:** the object property `applyDefaultPatterns` is deprecated. Please use the property `applyDefaultIgnorePatterns` instead. |
132 | ||
133 | ## When Not To Use It | |
134 | ||
135 | If you aren't concerned about having different line comment styles, then you can turn off this rule. | |
136 | ||
137 | ## Compatibility | |
138 | ||
139 | **JSCS**: [validateCommentPosition](https://jscs-dev.github.io/rule/validateCommentPosition) |