Skip to content

Commit

Permalink
Update: Add filtering to no-inline-comments
Browse files Browse the repository at this point in the history
Also adds Webpack magic comments to all comment rules using astUtils.COMMENTS_IGNORE_PATTERN
  • Loading branch information
EdieLemoine committed Mar 11, 2020
1 parent 0243549 commit fc1b5c4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/rules/capitalized-comments.md
Expand Up @@ -52,7 +52,7 @@ This rule has two options: a string value `"always"` or `"never"` which determin
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"]`.
* Note that the following words are always ignored by this rule: `["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]` and all [Webpack magic comments](https://webpack.js.org/api/module-methods/#magic-comments).
* `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 another comment. By default, this is `false`.

Expand Down
5 changes: 3 additions & 2 deletions docs/rules/line-comment-position.md
Expand Up @@ -9,7 +9,7 @@ var foo = "bar"; // beside comment

## Rule Details

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`.
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` and all [Webpack magic comments](https://webpack.js.org/api/module-methods/#magic-comments).


## Options
Expand Down Expand Up @@ -61,7 +61,8 @@ Examples of **incorrect** code for the `{ "position": "beside" }` option:

### ignorePattern

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.
By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`, `falls through`
and all [Webpack magic comments](https://webpack.js.org/api/module-methods/#magic-comments). An alternative regular expression can be provided.

Examples of **correct** code for the `ignorePattern` option:

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/lines-around-comment.md
Expand Up @@ -437,7 +437,7 @@ const [

### ignorePattern

By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`. An alternative regular expression can be provided.
By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs` and all [Webpack magic comments](https://webpack.js.org/api/module-methods/#magic-comments). An alternative regular expression can be provided.

Examples of **correct** code for the `ignorePattern` option:

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-inline-comments.md
Expand Up @@ -3,6 +3,8 @@
Some style guides disallow comments on the same line as code. Code can become difficult to read if comments immediately follow the code on the same line.
On the other hand, it is sometimes faster and more obvious to put comments immediately following code.

By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`, and all [Webpack magic comments](https://webpack.js.org/api/module-methods/#magic-comments).

## Rule Details

This rule disallows comments on the same line as code.
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-inline-comments.js
Expand Up @@ -80,9 +80,9 @@ module.exports = {

return {
Program() {
const comments = sourceCode.getAllComments();

comments.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);
return sourceCode.getAllComments()
.filter(comment => !astUtils.COMMENTS_IGNORE_PATTERN.test(comment.value))
.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/utils/ast-utils.js
Expand Up @@ -31,7 +31,7 @@ const bindOrCallOrApplyPattern = /^(?:bind|call|apply)$/u;
const thisTagPattern = /^[\s*]*@this/mu;


const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs)/u;
const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs|webpackInclude:|webpackExclude:|webpackChunkName:|webpackMode:|webpackPrefetch:|webpackPreload:)/u;
const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);

// A set of node types that can contain a list of statements
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/capitalized-comments.js
Expand Up @@ -71,6 +71,12 @@ ruleTester.run("capitalized-comments", rule, {
"/* globals var1, var2 */",
"/* globals var1:true, var2 */",
"/* exported myVar */",
"/* webpackInclude: /\\.json$/ */",
"/* webpackExclude: /\\.noimport\\.json$/ */",
"/* webpackChunkName: \"my-chunk-name\" */",
"/* webpackMode: \"lazy\" */",
"/* webpackPrefetch: true */",
"/* webpackPreload: true */",

// Ignores shebangs
"#!foo",
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/rules/no-inline-comments.js
Expand Up @@ -88,7 +88,13 @@ ruleTester.run("no-inline-comments", rule, {
comment
*/}
</div>
)`
)`,

// Webpack
{
code: "import(/* webpackChunkName: \"my-chunk-name\" */'./locale/language');",
parserOptions: { ecmaVersion: 2020 }
}
],

invalid: [
Expand Down

0 comments on commit fc1b5c4

Please sign in to comment.