diff --git a/docs/rules/no-inline-comments.md b/docs/rules/no-inline-comments.md index cde77e9e499..8a4baf68726 100644 --- a/docs/rules/no-inline-comments.md +++ b/docs/rules/no-inline-comments.md @@ -87,3 +87,25 @@ var quux = ( ) ``` + +## Options + +### 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 +``` diff --git a/lib/rules/no-inline-comments.js b/lib/rules/no-inline-comments.js index 41b0f1e664c..dec278615e2 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); + 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 ecb475997f7..463c86eea66 100644 --- a/tests/lib/rules/no-inline-comments.js +++ b/tests/lib/rules/no-inline-comments.js @@ -88,7 +88,24 @@ ruleTester.run("no-inline-comments", rule, { comment */} - )` + )`, + { + code: "import(/* webpackChunkName: \"my-chunk-name\" */ './locale/en');", + options: [ + { + ignorePattern: "(?:webpackChunkName):\\s.+" + } + ], + parserOptions: { ecmaVersion: 2020 } + }, + { + code: "var foo = 2; // Note: This comment is legal.", + options: [ + { + ignorePattern: "Note: " + } + ] + } ], invalid: [ @@ -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] @@ -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]