From 8ea8844d807994572dd6d46f448302a7b690ca65 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Wed, 11 Mar 2020 09:55:31 +0100 Subject: [PATCH] Update: Add ignorePattern to no-inline-comments --- lib/rules/no-inline-comments.js | 33 +++++++++++++++++++++++---- tests/lib/rules/no-inline-comments.js | 19 ++++++++++++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/rules/no-inline-comments.js b/lib/rules/no-inline-comments.js index 41b0f1e664c7..2cc7a47c6cc0 100644 --- a/lib/rules/no-inline-comments.js +++ b/lib/rules/no-inline-comments.js @@ -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." @@ -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 @@ -51,6 +71,11 @@ module.exports = { return; } + // Matches the ignore pattern + if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) { + return; + } + // JSX Exception if ( (isPreambleEmpty || preamble === "{") && @@ -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); } }; } diff --git a/tests/lib/rules/no-inline-comments.js b/tests/lib/rules/no-inline-comments.js index ecb475997f7c..05524d2a686e 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 */} - )` + )`, + + // Webpack + { + code: ` + import( + /* webpackInclude: /\\.json$/ */ + /* webpackChunkName: "my-chunk-name" */ + './locale/en' + ); + `, + options: [ + { + ignorePattern: "(?:webpackChunkName|webpackInclude):\\s.+" + } + ], + parserOptions: { ecmaVersion: 2020 } + } ], invalid: [