From cba295e09156a74bcd3b6b6f6ef394a7870a73c8 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Thu, 15 Oct 2020 23:20:50 +0300 Subject: [PATCH] Add "comment-pattern" rule (#4962) * added implementation + tests * Added/updated readme * applied prettier * PR fixes (supporting string as option + tests, formatting, improved readme) * specifying the original pattern in the error message + fixing tests * Update lib/rules/comment-pattern/index.js Co-authored-by: Richard Hallows * removed irrelevant testRule * Update lib/rules/comment-pattern/README.md Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> * removed redundant tests which are covered in basic checks Co-authored-by: Richard Hallows Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- docs/user-guide/rules/list.md | 1 + lib/rules/comment-pattern/README.md | 38 +++++++++ lib/rules/comment-pattern/__tests__/index.js | 90 ++++++++++++++++++++ lib/rules/comment-pattern/index.js | 48 +++++++++++ lib/rules/index.js | 1 + 5 files changed, 178 insertions(+) create mode 100644 lib/rules/comment-pattern/README.md create mode 100644 lib/rules/comment-pattern/__tests__/index.js create mode 100644 lib/rules/comment-pattern/index.js diff --git a/docs/user-guide/rules/list.md b/docs/user-guide/rules/list.md index 06b93637b8..fb3b6f7d1a 100644 --- a/docs/user-guide/rules/list.md +++ b/docs/user-guide/rules/list.md @@ -228,6 +228,7 @@ Grouped first by the following categories and then by the [_thing_](http://apps. ### Comment +- [`comment-pattern`](../../../lib/rules/comment-pattern/README.md): Specify a pattern for comments. - [`comment-word-blacklist`](../../../lib/rules/comment-word-blacklist/README.md): Specify a list of disallowed words within comments. **(deprecated)** - [`comment-word-disallowed-list`](../../../lib/rules/comment-word-disallowed-list/README.md): Specify a list of disallowed words within comments. diff --git a/lib/rules/comment-pattern/README.md b/lib/rules/comment-pattern/README.md new file mode 100644 index 0000000000..b0a276e225 --- /dev/null +++ b/lib/rules/comment-pattern/README.md @@ -0,0 +1,38 @@ +# comment-pattern + +Specify a pattern for comments. + + +```css +/* comment */ +/** ↑ + * The pattern of this */ +``` + +## Options + +`regex|string` + +A string will be translated into a RegExp like so `new RegExp(yourString)` — so be sure to escape properly. + +Given the string: + +``` +"foo .+" +``` + +The following patterns are considered violations: + + +```css +/*not starting with foo*/ +a { color: red; } +``` + +The following patterns are _not_ considered violations: + + +```css +/*foo at the beginning*/ +a { color: red; } +``` diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js new file mode 100644 index 0000000000..c3ff4fcd43 --- /dev/null +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -0,0 +1,90 @@ +'use strict'; + +const { messages, ruleName } = require('..'); + +testRule({ + ruleName, + config: [/foo-.+/], + + accept: [ + { + code: '/* foo-valid yay */', + }, + { + code: `/* foo-- + multi-line + \n + \r + \r\n + \n\r + \t + */`, + }, + ], + + reject: [ + { + code: '/* not foo- */', + message: messages.expected(/foo-.+/), + }, + { + code: '/**/', + message: messages.expected(/foo-.+/), + }, + ], +}); + +testRule({ + ruleName, + config: ['foo-.+'], + + accept: [ + { + code: '/* foo-valid yay */', + }, + { + code: `/* foo-- + multi-line + \n + \r + \r\n + \n\r + \t + */`, + }, + ], + + reject: [ + { + code: '/* not foo- */', + message: messages.expected('foo-.+'), + }, + { + code: '/**/', + message: messages.expected('foo-.+'), + }, + ], +}); + +testRule({ + ruleName, + config: [/foo-.+/], + syntax: 'scss', + + accept: [ + { + code: 'a {} // foo-ok', + }, + { + code: '// foo-ok', + }, + ], + + reject: [ + { + code: 'a {} // not-foo', + description: 'checks inline scss comments', + message: messages.expected(/foo-.+/), + }, + ], +}); diff --git a/lib/rules/comment-pattern/index.js b/lib/rules/comment-pattern/index.js new file mode 100644 index 0000000000..c78aa4aa45 --- /dev/null +++ b/lib/rules/comment-pattern/index.js @@ -0,0 +1,48 @@ +// @ts-nocheck + +'use strict'; + +const _ = require('lodash'); +const report = require('../../utils/report'); +const ruleMessages = require('../../utils/ruleMessages'); +const validateOptions = require('../../utils/validateOptions'); + +const ruleName = 'comment-pattern'; + +const messages = ruleMessages(ruleName, { + expected: (pattern) => `Expected comment to match pattern "${pattern}"`, +}); + +function rule(pattern) { + return (root, result) => { + const validOptions = validateOptions(result, ruleName, { + actual: pattern, + possible: [_.isRegExp, _.isString], + }); + + if (!validOptions) { + return; + } + + const normalizedPattern = _.isString(pattern) ? new RegExp(pattern) : pattern; + + root.walkComments((comment) => { + const text = comment.text; + + if (normalizedPattern.test(text)) { + return; + } + + report({ + message: messages.expected(pattern), + node: comment, + result, + ruleName, + }); + }); + }; +} + +rule.ruleName = ruleName; +rule.messages = messages; +module.exports = rule; diff --git a/lib/rules/index.js b/lib/rules/index.js index a2ee0d5260..8d51bfd21c 100644 --- a/lib/rules/index.js +++ b/lib/rules/index.js @@ -61,6 +61,7 @@ const rules = { 'color-no-invalid-hex': importLazy(() => require('./color-no-invalid-hex'))(), 'comment-empty-line-before': importLazy(() => require('./comment-empty-line-before'))(), 'comment-no-empty': importLazy(() => require('./comment-no-empty'))(), + 'comment-pattern': importLazy(() => require('./comment-pattern'))(), 'comment-whitespace-inside': importLazy(() => require('./comment-whitespace-inside'))(), 'comment-word-blacklist': importLazy(() => require('./comment-word-blacklist'))(), 'comment-word-disallowed-list': importLazy(() => require('./comment-word-disallowed-list'))(),