Skip to content

Commit

Permalink
Update: Add ignorePattern to no-inline-comments (#13029)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 26, 2020
1 parent d79bbe9 commit 07d9bea
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
22 changes: 22 additions & 0 deletions docs/rules/no-inline-comments.md
Expand Up @@ -87,3 +87,25 @@ var quux = (
</div>
)
```

## Options

### ignorePattern

A regular expression can be provided to make this rule ignore specific comments.

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

```js
/*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/

import(/* webpackChunkName: "my-chunk-name" */ './locale/en');
```

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

```js
/*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */

var foo = 4; // other thing
```
29 changes: 25 additions & 4 deletions lib/rules/no-inline-comments.js
Expand Up @@ -21,7 +21,17 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-inline-comments"
},

schema: [],
schema: [
{
type: "object",
properties: {
ignorePattern: {
type: "string"
}
},
additionalProperties: false
}
],

messages: {
unexpectedInlineComment: "Unexpected comment inline with code."
Expand All @@ -30,6 +40,12 @@ module.exports = {

create(context) {
const sourceCode = context.getSourceCode();
const options = context.options[0];
let customIgnoreRegExp;

if (options && options.ignorePattern) {
customIgnoreRegExp = new RegExp(options.ignorePattern, "u");
}

/**
* Will check that comments are not on lines starting with or ending with code
Expand All @@ -51,6 +67,11 @@ module.exports = {
return;
}

// Matches the ignore pattern
if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) {
return;
}

// JSX Exception
if (
(isPreambleEmpty || preamble === "{") &&
Expand Down Expand Up @@ -80,9 +101,9 @@ module.exports = {

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

comments.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);
sourceCode.getAllComments()
.filter(token => token.type !== "Shebang")
.forEach(testCodeAroundComment);
}
};
}
Expand Down
37 changes: 36 additions & 1 deletion tests/lib/rules/no-inline-comments.js
Expand Up @@ -88,7 +88,24 @@ ruleTester.run("no-inline-comments", rule, {
comment
*/}
</div>
)`
)`,
{
code: "import(/* webpackChunkName: \"my-chunk-name\" */ './locale/en');",
options: [
{
ignorePattern: "(?:webpackChunkName):\\s.+"
}
],
parserOptions: { ecmaVersion: 2020 }
},
{
code: "var foo = 2; // Note: This comment is legal.",
options: [
{
ignorePattern: "Note: "
}
]
}
],

invalid: [
Expand All @@ -100,6 +117,15 @@ ruleTester.run("no-inline-comments", rule, {
code: "/*A block comment inline before code*/ var a = 2;",
errors: [blockError]
},
{
code: "/* something */ var a = 2;",
options: [
{
ignorePattern: "otherthing"
}
],
errors: [blockError]
},
{
code: "var a = 3; //A comment inline with code",
errors: [lineError]
Expand All @@ -108,6 +134,15 @@ ruleTester.run("no-inline-comments", rule, {
code: "var a = 3; // someday use eslint-disable-line here",
errors: [lineError]
},
{
code: "var a = 3; // other line comment",
options: [
{
ignorePattern: "something"
}
],
errors: [lineError]
},
{
code: "var a = 4;\n/**A\n * block\n * comment\n * inline\n * between\n * code*/ var foo = a;",
errors: [blockError]
Expand Down

0 comments on commit 07d9bea

Please sign in to comment.