Skip to content

Commit

Permalink
Fix: capitalized-comments: Ignore consec. comments if first is invalid (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
platinumazure authored and gyandeeps committed Jan 4, 2017
1 parent 616611a commit bf3ea3a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
21 changes: 15 additions & 6 deletions docs/rules/capitalized-comments.md
Expand Up @@ -56,7 +56,7 @@ Here are the supported object options:
* `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.
* Note that the following words are always ignored by this rule: `["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]`.
* `ignoreInlineComments`: If this is `true`, the rule will not report on comments in the middle of code. By default, this is `false`.
* `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`.
* `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`.

Here is an example configuration:

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

#### `ignoreConsecutiveComments`

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.
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.

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

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

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

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

Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`:

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

// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
```

### Using Different Options for Line and Block Comments

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):
Expand Down
14 changes: 5 additions & 9 deletions lib/rules/capitalized-comments.js
Expand Up @@ -137,8 +137,7 @@ module.exports = {

const capitalize = context.options[0] || "always",
normalizedOptions = getAllNormalizedOptions(context.options[1]),
sourceCode = context.getSourceCode(),
validCommentMap = new Map();
sourceCode = context.getSourceCode();

createRegExpForIgnorePatterns(normalizedOptions);

Expand Down Expand Up @@ -176,18 +175,17 @@ module.exports = {
}

/**
* Determine if a comment follows another valid comment.
* Determine if a comment follows another comment.
*
* @param {ASTNode} comment The comment to check.
* @returns {boolean} True if the comment follows a valid comment.
*/
function isConsecutiveValidComment(comment) {
function isConsecutiveComment(comment) {
const previousTokenOrComment = sourceCode.getTokenOrCommentBefore(comment);

return Boolean(
previousTokenOrComment &&
["Block", "Line"].indexOf(previousTokenOrComment.type) !== -1 &&
validCommentMap.get(previousTokenOrComment)
["Block", "Line"].indexOf(previousTokenOrComment.type) !== -1
);
}

Expand Down Expand Up @@ -219,7 +217,7 @@ module.exports = {
}

// 4. Is this a consecutive comment (and are we tolerating those)?
if (options.ignoreConsecutiveComments && isConsecutiveValidComment(comment)) {
if (options.ignoreConsecutiveComments && isConsecutiveComment(comment)) {
return true;
}

Expand Down Expand Up @@ -286,8 +284,6 @@ module.exports = {
}
});
}

validCommentMap.set(comment, commentValid);
}

//----------------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/rules/capitalized-comments.js
Expand Up @@ -832,6 +832,40 @@ ruleTester.run("capitalized-comments", rule, {
}]
},

// Only the initial comment should warn if ignoreConsecutiveComments:true
{
code: [
"// this comment is invalid since it is not capitalized,",
"// but this one is ignored since it is consecutive.",
].join("\n"),
output: [
"// This comment is invalid since it is not capitalized,",
"// but this one is ignored since it is consecutive.",
].join("\n"),
options: ["always", { ignoreConsecutiveComments: true }],
errors: [{
message: ALWAYS_MESSAGE,
line: 1,
column: 1
}]
},
{
code: [
"// This comment is invalid since it is not capitalized,",
"// But this one is ignored since it is consecutive.",
].join("\n"),
output: [
"// this comment is invalid since it is not capitalized,",
"// But this one is ignored since it is consecutive.",
].join("\n"),
options: ["never", { ignoreConsecutiveComments: true }],
errors: [{
message: NEVER_MESSAGE,
line: 1,
column: 1
}]
},

// Consecutive comments should warn if ignoreConsecutiveComments:false
{
code: [
Expand Down

0 comments on commit bf3ea3a

Please sign in to comment.