Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add ignorePattern to no-inline-comments #13029

Merged
merged 1 commit into from Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we clarify that this accepts a string rather than a RegExp literal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it's implemented the same way as in lines-around-comment. It's referenced to as a "regular expression" there as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, @EdieLemoine. Since that's the only change @kaicataldo requested, let's merge this to get it into today's release, and I'll put together a quick PR to change this in both places.


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 }
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
},
{
code: "var foo = 2; // Note: This comment is legal.",
options: [
{
ignorePattern: "Note: "
}
]
}
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
],

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