Skip to content

Commit 07d9bea

Browse files
authored
Update: Add ignorePattern to no-inline-comments (#13029)
1 parent d79bbe9 commit 07d9bea

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

docs/rules/no-inline-comments.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,25 @@ var quux = (
8787
</div>
8888
)
8989
```
90+
91+
## Options
92+
93+
### ignorePattern
94+
95+
A regular expression can be provided to make this rule ignore specific comments.
96+
97+
Examples of **correct** code for the `ignorePattern` option:
98+
99+
```js
100+
/*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/
101+
102+
import(/* webpackChunkName: "my-chunk-name" */ './locale/en');
103+
```
104+
105+
Examples of **incorrect** code for the `ignorePattern` option:
106+
107+
```js
108+
/*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */
109+
110+
var foo = 4; // other thing
111+
```

lib/rules/no-inline-comments.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ module.exports = {
2121
url: "https://eslint.org/docs/rules/no-inline-comments"
2222
},
2323

24-
schema: [],
24+
schema: [
25+
{
26+
type: "object",
27+
properties: {
28+
ignorePattern: {
29+
type: "string"
30+
}
31+
},
32+
additionalProperties: false
33+
}
34+
],
2535

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

3141
create(context) {
3242
const sourceCode = context.getSourceCode();
43+
const options = context.options[0];
44+
let customIgnoreRegExp;
45+
46+
if (options && options.ignorePattern) {
47+
customIgnoreRegExp = new RegExp(options.ignorePattern, "u");
48+
}
3349

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

70+
// Matches the ignore pattern
71+
if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) {
72+
return;
73+
}
74+
5475
// JSX Exception
5576
if (
5677
(isPreambleEmpty || preamble === "{") &&
@@ -80,9 +101,9 @@ module.exports = {
80101

81102
return {
82103
Program() {
83-
const comments = sourceCode.getAllComments();
84-
85-
comments.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);
104+
sourceCode.getAllComments()
105+
.filter(token => token.type !== "Shebang")
106+
.forEach(testCodeAroundComment);
86107
}
87108
};
88109
}

tests/lib/rules/no-inline-comments.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,24 @@ ruleTester.run("no-inline-comments", rule, {
8888
comment
8989
*/}
9090
</div>
91-
)`
91+
)`,
92+
{
93+
code: "import(/* webpackChunkName: \"my-chunk-name\" */ './locale/en');",
94+
options: [
95+
{
96+
ignorePattern: "(?:webpackChunkName):\\s.+"
97+
}
98+
],
99+
parserOptions: { ecmaVersion: 2020 }
100+
},
101+
{
102+
code: "var foo = 2; // Note: This comment is legal.",
103+
options: [
104+
{
105+
ignorePattern: "Note: "
106+
}
107+
]
108+
}
92109
],
93110

94111
invalid: [
@@ -100,6 +117,15 @@ ruleTester.run("no-inline-comments", rule, {
100117
code: "/*A block comment inline before code*/ var a = 2;",
101118
errors: [blockError]
102119
},
120+
{
121+
code: "/* something */ var a = 2;",
122+
options: [
123+
{
124+
ignorePattern: "otherthing"
125+
}
126+
],
127+
errors: [blockError]
128+
},
103129
{
104130
code: "var a = 3; //A comment inline with code",
105131
errors: [lineError]
@@ -108,6 +134,15 @@ ruleTester.run("no-inline-comments", rule, {
108134
code: "var a = 3; // someday use eslint-disable-line here",
109135
errors: [lineError]
110136
},
137+
{
138+
code: "var a = 3; // other line comment",
139+
options: [
140+
{
141+
ignorePattern: "something"
142+
}
143+
],
144+
errors: [lineError]
145+
},
111146
{
112147
code: "var a = 4;\n/**A\n * block\n * comment\n * inline\n * between\n * code*/ var foo = a;",
113148
errors: [blockError]

0 commit comments

Comments
 (0)