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 11, 2020
1 parent c44306e commit 8ea8844
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
33 changes: 29 additions & 4 deletions lib/rules/no-inline-comments.js
Expand Up @@ -21,7 +21,21 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-inline-comments"
},

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

messages: {
unexpectedInlineComment: "Unexpected comment inline with code."
Expand All @@ -30,6 +44,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 +71,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 +105,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
19 changes: 18 additions & 1 deletion tests/lib/rules/no-inline-comments.js
Expand Up @@ -88,7 +88,24 @@ ruleTester.run("no-inline-comments", rule, {
comment
*/}
</div>
)`
)`,

// Webpack
{
code: `
import(
/* webpackInclude: /\\.json$/ */
/* webpackChunkName: "my-chunk-name" */
'./locale/en'
);
`,
options: [
{
ignorePattern: "(?:webpackChunkName|webpackInclude):\\s.+"
}
],
parserOptions: { ecmaVersion: 2020 }
}
],

invalid: [
Expand Down

0 comments on commit 8ea8844

Please sign in to comment.