Skip to content

Commit

Permalink
Update: Add ignorePattern to no-inline-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Aug 12, 2020
1 parent c44306e commit 36e4a2c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
20 changes: 20 additions & 0 deletions docs/rules/no-inline-comments.md
Expand Up @@ -36,6 +36,26 @@ var bar = 5;
//This is a comment below a line of code
```

### 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
```

### JSX exception

Comments inside the curly braces in JSX are allowed to be on the same line as the braces, but only if they are not on the same line with other code, and the braces do not enclose an actual expression.
Expand Down
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);
return sourceCode.getAllComments()
.filter(token => token.type !== "Shebang")
.forEach(testCodeAroundComment);
}
};
}
Expand Down
29 changes: 28 additions & 1 deletion tests/lib/rules/no-inline-comments.js
Expand Up @@ -88,7 +88,16 @@ ruleTester.run("no-inline-comments", rule, {
comment
*/}
</div>
)`
)`,
{
code: "import(/* webpackChunkName: \"my-chunk-name\" */ './locale/en');",
options: [
{
ignorePattern: "(?:webpackChunkName):\\s.+"
}
],
parserOptions: { ecmaVersion: 2020 }
}
],

invalid: [
Expand All @@ -100,6 +109,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 +126,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 36e4a2c

Please sign in to comment.