diff --git a/docs/rules/no-inline-comments.md b/docs/rules/no-inline-comments.md index cde77e9e4991..69fad9278df1 100644 --- a/docs/rules/no-inline-comments.md +++ b/docs/rules/no-inline-comments.md @@ -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. diff --git a/lib/rules/no-inline-comments.js b/lib/rules/no-inline-comments.js index 41b0f1e664c7..c25a63ca91fa 100644 --- a/lib/rules/no-inline-comments.js +++ b/lib/rules/no-inline-comments.js @@ -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." @@ -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 @@ -51,6 +67,11 @@ module.exports = { return; } + // Matches the ignore pattern + if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) { + return; + } + // JSX Exception if ( (isPreambleEmpty || preamble === "{") && @@ -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); } }; } diff --git a/tests/lib/rules/no-inline-comments.js b/tests/lib/rules/no-inline-comments.js index ecb475997f7c..36a7ce8337d5 100644 --- a/tests/lib/rules/no-inline-comments.js +++ b/tests/lib/rules/no-inline-comments.js @@ -88,7 +88,16 @@ ruleTester.run("no-inline-comments", rule, { comment */} - )` + )`, + { + code: "import(/* webpackChunkName: \"my-chunk-name\" */ './locale/en');", + options: [ + { + ignorePattern: "(?:webpackChunkName):\\s.+" + } + ], + parserOptions: { ecmaVersion: 2020 } + } ], invalid: [ @@ -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] @@ -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]