Skip to content

Commit bf3ea3a

Browse files
platinumazuregyandeeps
authored andcommittedJan 4, 2017
Fix: capitalized-comments: Ignore consec. comments if first is invalid (#7835)
* Fix: capitalized-comments: Ignore consec. comments if first is invalid Only applies when `ignoreConsecutiveComments` option is enabled. * Adding "never" test case. * Docs: Fix documentation for consecutive comments * Docs: Clarify examples
1 parent 616611a commit bf3ea3a

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed
 

‎docs/rules/capitalized-comments.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Here are the supported object options:
5656
* `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.
5757
* Note that the following words are always ignored by this rule: `["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]`.
5858
* `ignoreInlineComments`: If this is `true`, the rule will not report on comments in the middle of code. By default, this is `false`.
59-
* `ignoreConsecutiveComments`: If this is `true`, the rule will not report on a comment which violates the rule, as long as the comment immediately follows a comment which is also not reported. By default, this is `false`.
59+
* `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`.
6060

6161
Here is an example configuration:
6262

@@ -172,24 +172,33 @@ function foo(/* ignored */ a) {
172172

173173
#### `ignoreConsecutiveComments`
174174

175-
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 a comment which did not violate the rule. This can be applied more than once.
175+
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.
176176

177177
Examples of **correct** code with `ignoreConsecutiveComments` set to `true`:
178178

179179
```js
180180
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
181181

182-
// This comment is valid since it has the correct capitalization,
183-
// and so is this one because it immediately follows a valid comment,
184-
// and this one as well because it follows the previous valid comment.
182+
// This comment is valid since it has the correct capitalization.
183+
// this comment is ignored since it follows another comment,
184+
// and this one as well because it follows yet another comment.
185185

186186
/* Here is a block comment which has the correct capitalization, */
187-
/* and this one is valid as well; */
187+
/* but this one is ignored due to being consecutive; */
188188
/*
189189
* in fact, even if any of these are multi-line, that is fine too.
190190
*/
191191
```
192192

193+
Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`:
194+
195+
```js
196+
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
197+
198+
// this comment is invalid, but only on this line.
199+
// this comment does NOT get reported, since it is a consecutive comment.
200+
```
201+
193202
### Using Different Options for Line and Block Comments
194203

195204
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):

‎lib/rules/capitalized-comments.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ module.exports = {
137137

138138
const capitalize = context.options[0] || "always",
139139
normalizedOptions = getAllNormalizedOptions(context.options[1]),
140-
sourceCode = context.getSourceCode(),
141-
validCommentMap = new Map();
140+
sourceCode = context.getSourceCode();
142141

143142
createRegExpForIgnorePatterns(normalizedOptions);
144143

@@ -176,18 +175,17 @@ module.exports = {
176175
}
177176

178177
/**
179-
* Determine if a comment follows another valid comment.
178+
* Determine if a comment follows another comment.
180179
*
181180
* @param {ASTNode} comment The comment to check.
182181
* @returns {boolean} True if the comment follows a valid comment.
183182
*/
184-
function isConsecutiveValidComment(comment) {
183+
function isConsecutiveComment(comment) {
185184
const previousTokenOrComment = sourceCode.getTokenOrCommentBefore(comment);
186185

187186
return Boolean(
188187
previousTokenOrComment &&
189-
["Block", "Line"].indexOf(previousTokenOrComment.type) !== -1 &&
190-
validCommentMap.get(previousTokenOrComment)
188+
["Block", "Line"].indexOf(previousTokenOrComment.type) !== -1
191189
);
192190
}
193191

@@ -219,7 +217,7 @@ module.exports = {
219217
}
220218

221219
// 4. Is this a consecutive comment (and are we tolerating those)?
222-
if (options.ignoreConsecutiveComments && isConsecutiveValidComment(comment)) {
220+
if (options.ignoreConsecutiveComments && isConsecutiveComment(comment)) {
223221
return true;
224222
}
225223

@@ -286,8 +284,6 @@ module.exports = {
286284
}
287285
});
288286
}
289-
290-
validCommentMap.set(comment, commentValid);
291287
}
292288

293289
//----------------------------------------------------------------------

‎tests/lib/rules/capitalized-comments.js

+34
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,40 @@ ruleTester.run("capitalized-comments", rule, {
832832
}]
833833
},
834834

835+
// Only the initial comment should warn if ignoreConsecutiveComments:true
836+
{
837+
code: [
838+
"// this comment is invalid since it is not capitalized,",
839+
"// but this one is ignored since it is consecutive.",
840+
].join("\n"),
841+
output: [
842+
"// This comment is invalid since it is not capitalized,",
843+
"// but this one is ignored since it is consecutive.",
844+
].join("\n"),
845+
options: ["always", { ignoreConsecutiveComments: true }],
846+
errors: [{
847+
message: ALWAYS_MESSAGE,
848+
line: 1,
849+
column: 1
850+
}]
851+
},
852+
{
853+
code: [
854+
"// This comment is invalid since it is not capitalized,",
855+
"// But this one is ignored since it is consecutive.",
856+
].join("\n"),
857+
output: [
858+
"// this comment is invalid since it is not capitalized,",
859+
"// But this one is ignored since it is consecutive.",
860+
].join("\n"),
861+
options: ["never", { ignoreConsecutiveComments: true }],
862+
errors: [{
863+
message: NEVER_MESSAGE,
864+
line: 1,
865+
column: 1
866+
}]
867+
},
868+
835869
// Consecutive comments should warn if ignoreConsecutiveComments:false
836870
{
837871
code: [

0 commit comments

Comments
 (0)