]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: capitalized-comments | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
6 | ||
eb39fafa DC |
7 | |
8 | Comments are useful for leaving information for future developers. In order for that information to be useful and not distracting, it is sometimes desirable for comments to follow a particular style. One element of comment formatting styles is whether the first word of a comment should be capitalized or lowercase. | |
9 | ||
10 | In general, no comment style is any more or less valid than any others, but many developers would agree that a consistent style can improve a project's maintainability. | |
11 | ||
12 | ## Rule Details | |
13 | ||
14 | This rule aims to enforce a consistent style of comments across your codebase, specifically by either requiring or disallowing a capitalized letter as the first word character in a comment. This rule will not issue warnings when non-cased letters are used. | |
15 | ||
16 | By default, this rule will require a non-lowercase letter at the beginning of comments. | |
17 | ||
18 | Examples of **incorrect** code for this rule: | |
19 | ||
8f9d1d4d DC |
20 | :::incorrect |
21 | ||
eb39fafa DC |
22 | ```js |
23 | /* eslint capitalized-comments: ["error"] */ | |
24 | ||
25 | // lowercase comment | |
26 | ||
27 | ``` | |
28 | ||
8f9d1d4d DC |
29 | ::: |
30 | ||
eb39fafa DC |
31 | Examples of **correct** code for this rule: |
32 | ||
8f9d1d4d DC |
33 | :::correct |
34 | ||
eb39fafa DC |
35 | ```js |
36 | ||
37 | // Capitalized comment | |
38 | ||
39 | // 1. Non-letter at beginning of comment | |
40 | ||
41 | // 丈 Non-Latin character at beginning of comment | |
42 | ||
43 | /* eslint semi:off */ | |
44 | /* eslint-env node */ | |
45 | /* eslint-disable */ | |
46 | /* eslint-enable */ | |
47 | /* istanbul ignore next */ | |
48 | /* jscs:enable */ | |
49 | /* jshint asi:true */ | |
50 | /* global foo */ | |
51 | /* globals foo */ | |
52 | /* exported myVar */ | |
53 | // eslint-disable-line | |
54 | // eslint-disable-next-line | |
55 | // https://github.com | |
56 | ||
57 | ``` | |
58 | ||
8f9d1d4d DC |
59 | ::: |
60 | ||
eb39fafa DC |
61 | ### Options |
62 | ||
63 | This rule has two options: a string value `"always"` or `"never"` which determines whether capitalization of the first word of a comment should be required or forbidden, and optionally an object containing more configuration parameters for the rule. | |
64 | ||
65 | Here are the supported object options: | |
66 | ||
67 | * `ignorePattern`: A string representing a regular expression pattern of words that should be ignored by this rule. If the first word of a comment matches the pattern, this rule will not report that comment. | |
68 | * Note that the following words are always ignored by this rule: `["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]`. | |
69 | * `ignoreInlineComments`: If this is `true`, the rule will not report on comments in the middle of code. By default, this is `false`. | |
70 | * `ignoreConsecutiveComments`: If this is `true`, the rule will not report on a comment which violates the rule, as long as the comment immediately follows another comment. By default, this is `false`. | |
71 | ||
72 | Here is an example configuration: | |
73 | ||
74 | ```json | |
75 | { | |
76 | "capitalized-comments": [ | |
77 | "error", | |
78 | "always", | |
79 | { | |
80 | "ignorePattern": "pragma|ignored", | |
81 | "ignoreInlineComments": true | |
82 | } | |
83 | ] | |
84 | } | |
85 | ``` | |
86 | ||
87 | #### `"always"` | |
88 | ||
89 | Using the `"always"` option means that this rule will report any comments which start with a lowercase letter. This is the default configuration for this rule. | |
90 | ||
91 | Note that configuration comments and comments which start with URLs are never reported. | |
92 | ||
93 | Examples of **incorrect** code for this rule: | |
94 | ||
8f9d1d4d DC |
95 | :::incorrect |
96 | ||
eb39fafa DC |
97 | ```js |
98 | /* eslint capitalized-comments: ["error", "always"] */ | |
99 | ||
100 | // lowercase comment | |
101 | ||
102 | ``` | |
103 | ||
8f9d1d4d DC |
104 | ::: |
105 | ||
eb39fafa DC |
106 | Examples of **correct** code for this rule: |
107 | ||
8f9d1d4d DC |
108 | :::correct |
109 | ||
eb39fafa DC |
110 | ```js |
111 | /* eslint capitalized-comments: ["error", "always"] */ | |
112 | ||
113 | // Capitalized comment | |
114 | ||
115 | // 1. Non-letter at beginning of comment | |
116 | ||
117 | // 丈 Non-Latin character at beginning of comment | |
118 | ||
119 | /* eslint semi:off */ | |
120 | /* eslint-env node */ | |
121 | /* eslint-disable */ | |
122 | /* eslint-enable */ | |
123 | /* istanbul ignore next */ | |
124 | /* jscs:enable */ | |
125 | /* jshint asi:true */ | |
126 | /* global foo */ | |
127 | /* globals foo */ | |
128 | /* exported myVar */ | |
129 | // eslint-disable-line | |
130 | // eslint-disable-next-line | |
131 | // https://github.com | |
132 | ||
133 | ``` | |
134 | ||
8f9d1d4d DC |
135 | ::: |
136 | ||
eb39fafa DC |
137 | #### `"never"` |
138 | ||
139 | Using the `"never"` option means that this rule will report any comments which start with an uppercase letter. | |
140 | ||
141 | Examples of **incorrect** code with the `"never"` option: | |
142 | ||
8f9d1d4d DC |
143 | :::incorrect |
144 | ||
eb39fafa DC |
145 | ```js |
146 | /* eslint capitalized-comments: ["error", "never"] */ | |
147 | ||
148 | // Capitalized comment | |
149 | ||
150 | ``` | |
151 | ||
8f9d1d4d DC |
152 | ::: |
153 | ||
eb39fafa DC |
154 | Examples of **correct** code with the `"never"` option: |
155 | ||
8f9d1d4d DC |
156 | :::correct |
157 | ||
eb39fafa DC |
158 | ```js |
159 | /* eslint capitalized-comments: ["error", "never"] */ | |
160 | ||
161 | // lowercase comment | |
162 | ||
163 | // 1. Non-letter at beginning of comment | |
164 | ||
165 | // 丈 Non-Latin character at beginning of comment | |
166 | ||
167 | ``` | |
168 | ||
8f9d1d4d DC |
169 | ::: |
170 | ||
eb39fafa DC |
171 | #### `ignorePattern` |
172 | ||
173 | The `ignorePattern` object takes a string value, which is used as a regular expression applied to the first word of a comment. | |
174 | ||
175 | Examples of **correct** code with the `"ignorePattern"` option set to `"pragma"`: | |
176 | ||
8f9d1d4d DC |
177 | :::correct |
178 | ||
eb39fafa DC |
179 | ```js |
180 | /* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */ | |
181 | ||
182 | function foo() { | |
183 | /* pragma wrap(true) */ | |
184 | } | |
185 | ||
186 | ``` | |
187 | ||
8f9d1d4d DC |
188 | ::: |
189 | ||
eb39fafa DC |
190 | #### `ignoreInlineComments` |
191 | ||
192 | Setting the `ignoreInlineComments` option to `true` means that comments in the middle of code (with a token on the same line as the beginning of the comment, and another token on the same line as the end of the comment) will not be reported by this rule. | |
193 | ||
194 | Examples of **correct** code with the `"ignoreInlineComments"` option set to `true`: | |
195 | ||
8f9d1d4d DC |
196 | :::correct |
197 | ||
eb39fafa DC |
198 | ```js |
199 | /* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */ | |
200 | ||
201 | function foo(/* ignored */ a) { | |
202 | } | |
203 | ||
204 | ``` | |
205 | ||
8f9d1d4d DC |
206 | ::: |
207 | ||
eb39fafa DC |
208 | #### `ignoreConsecutiveComments` |
209 | ||
210 | If the `ignoreConsecutiveComments` option is set to `true`, then comments which otherwise violate the rule will not be reported as long as they immediately follow another comment. This can be applied more than once. | |
211 | ||
212 | Examples of **correct** code with `ignoreConsecutiveComments` set to `true`: | |
213 | ||
8f9d1d4d DC |
214 | :::correct |
215 | ||
eb39fafa DC |
216 | ```js |
217 | /* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */ | |
218 | ||
219 | // This comment is valid since it has the correct capitalization. | |
220 | // this comment is ignored since it follows another comment, | |
221 | // and this one as well because it follows yet another comment. | |
222 | ||
223 | /* Here is a block comment which has the correct capitalization, */ | |
224 | /* but this one is ignored due to being consecutive; */ | |
225 | /* | |
226 | * in fact, even if any of these are multi-line, that is fine too. | |
227 | */ | |
228 | ``` | |
229 | ||
8f9d1d4d DC |
230 | ::: |
231 | ||
eb39fafa DC |
232 | Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`: |
233 | ||
8f9d1d4d DC |
234 | :::incorrect |
235 | ||
eb39fafa DC |
236 | ```js |
237 | /* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */ | |
238 | ||
239 | // this comment is invalid, but only on this line. | |
240 | // this comment does NOT get reported, since it is a consecutive comment. | |
241 | ``` | |
242 | ||
8f9d1d4d DC |
243 | ::: |
244 | ||
eb39fafa DC |
245 | ### Using Different Options for Line and Block Comments |
246 | ||
247 | If you wish to have a different configuration for line comments and block comments, you can do so by using two different object configurations (note that the capitalization option will be enforced consistently for line and block comments): | |
248 | ||
249 | ```json | |
250 | { | |
251 | "capitalized-comments": [ | |
252 | "error", | |
253 | "always", | |
254 | { | |
255 | "line": { | |
256 | "ignorePattern": "pragma|ignored", | |
257 | }, | |
258 | "block": { | |
259 | "ignoreInlineComments": true, | |
260 | "ignorePattern": "ignored" | |
261 | } | |
262 | } | |
263 | ] | |
264 | } | |
265 | ``` | |
266 | ||
267 | Examples of **incorrect** code with different line and block comment configuration: | |
268 | ||
8f9d1d4d DC |
269 | :::incorrect |
270 | ||
eb39fafa DC |
271 | ```js |
272 | /* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */ | |
273 | ||
274 | // capitalized line comment, this is incorrect, blockignore does not help here | |
275 | /* lowercased block comment, this is incorrect too */ | |
276 | ||
277 | ``` | |
278 | ||
8f9d1d4d DC |
279 | ::: |
280 | ||
eb39fafa DC |
281 | Examples of **correct** code with different line and block comment configuration: |
282 | ||
8f9d1d4d DC |
283 | :::correct |
284 | ||
eb39fafa DC |
285 | ```js |
286 | /* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */ | |
287 | ||
288 | // Uppercase line comment, this is correct | |
289 | /* blockignore lowercase block comment, this is correct due to ignorePattern */ | |
290 | ||
291 | ``` | |
292 | ||
8f9d1d4d DC |
293 | ::: |
294 | ||
eb39fafa DC |
295 | ## When Not To Use It |
296 | ||
297 | This rule can be disabled if you do not care about the grammatical style of comments in your codebase. | |
298 | ||
299 | ## Compatibility | |
300 | ||
301 | * **JSCS**: [requireCapitalizedComments](https://jscs-dev.github.io/rule/requireCapitalizedComments) | |
302 | * **JSCS**: [disallowCapitalizedComments](https://jscs-dev.github.io/rule/disallowCapitalizedComments) |