From 4abfda5c1d24ba557e341673deba192a2c257dd6 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Fri, 2 Oct 2020 18:54:20 +0300 Subject: [PATCH 1/9] added implementation + tests --- lib/rules/comment-pattern/__tests__/index.js | 84 ++++++++++++++++++++ lib/rules/comment-pattern/index.js | 47 +++++++++++ lib/rules/index.js | 1 + 3 files changed, 132 insertions(+) create mode 100644 lib/rules/comment-pattern/__tests__/index.js create mode 100644 lib/rules/comment-pattern/index.js diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js new file mode 100644 index 0000000000..d7fc296a55 --- /dev/null +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -0,0 +1,84 @@ +'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 + */`, + }, + { + code: '.without-comment {}', + }, + ], + + reject: [ + { + code: '/* not foo- */', + message: messages.expected, + }, + { + code: '/**/', + message: messages.expected, + }, + ], +}); + +testRule({ + ruleName, + config: [true], + + accept: [ + { + code: '/* some comment */', + }, + { + code: '/**/', + }, + { + code: '.without-comment {}', + }, + ] +}); + +testRule({ + ruleName, + config: [/foo-.+/], + syntax: 'scss', + + accept: [ + { + code: 'a {} // foo-ok', + description: 'ignored inline scss comments', + }, + { + code: '// foo-ok', + description: 'ignored inline scss comments', + }, + { + code: '.without-comment { }', + description: 'ignores scss without comments', + }, + ], + + reject: [ + { + code: 'a {} // not-foo', + description: 'checks inline scss comments', + message: messages.expected, + }, + ] +}); diff --git a/lib/rules/comment-pattern/index.js b/lib/rules/comment-pattern/index.js new file mode 100644 index 0000000000..e7cb9afb5c --- /dev/null +++ b/lib/rules/comment-pattern/index.js @@ -0,0 +1,47 @@ +// @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: 'Expected comment to match specified pattern', +}); + +function rule(pattern) { + return (root, result) => { + const validOptions = validateOptions(result, ruleName, { + actual: pattern, + possible: [_.isRegExp], + }); + + if (!validOptions) { + return; + } + + root.walkComments((comment) => { + const text = comment.text; + + + if (pattern.test(text)) { + return; + } + + report({ + message: messages.expected, + 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'))(), From 13bfb87e3d4ed62eb655df26680df73145bd0868 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Sun, 4 Oct 2020 22:23:32 +0300 Subject: [PATCH 2/9] Added/updated readme --- docs/user-guide/rules/list.md | 1 + lib/rules/comment-pattern/README.md | 39 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 lib/rules/comment-pattern/README.md diff --git a/docs/user-guide/rules/list.md b/docs/user-guide/rules/list.md index 06b93637b8..7b464d4806 100644 --- a/docs/user-guide/rules/list.md +++ b/docs/user-guide/rules/list.md @@ -230,6 +230,7 @@ Grouped first by the following categories and then by the [_thing_](http://apps. - [`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. +- [`comment-pattern`](../../../lib/rules/comment-pattern/README.md): Specify a pattern for comments. ### General / Sheet diff --git a/lib/rules/comment-pattern/README.md b/lib/rules/comment-pattern/README.md new file mode 100644 index 0000000000..8b18f6a644 --- /dev/null +++ b/lib/rules/comment-pattern/README.md @@ -0,0 +1,39 @@ +# custom-property-pattern + +Specify a pattern for comments. + + +```css + /*comment*/ + /* ↑ */ a { --foo-: 1px; } +/** ↑ + * The pattern of this */ +``` + +## Options + +`regex` + +The pattern to match the comments with. + +Given the regex: + +```js +/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; } +``` From 7428b0f6c623f83b3d69bdd3d27e58335c3123d4 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Mon, 5 Oct 2020 08:20:38 +0300 Subject: [PATCH 3/9] applied prettier --- lib/rules/comment-pattern/README.md | 2 +- lib/rules/comment-pattern/__tests__/index.js | 4 ++-- lib/rules/comment-pattern/index.js | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/rules/comment-pattern/README.md b/lib/rules/comment-pattern/README.md index 8b18f6a644..51a547b0c5 100644 --- a/lib/rules/comment-pattern/README.md +++ b/lib/rules/comment-pattern/README.md @@ -18,7 +18,7 @@ The pattern to match the comments with. Given the regex: -```js +``` /foo .+/ ``` diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js index d7fc296a55..552dd3eb42 100644 --- a/lib/rules/comment-pattern/__tests__/index.js +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -51,7 +51,7 @@ testRule({ { code: '.without-comment {}', }, - ] + ], }); testRule({ @@ -80,5 +80,5 @@ testRule({ description: 'checks inline scss comments', message: messages.expected, }, - ] + ], }); diff --git a/lib/rules/comment-pattern/index.js b/lib/rules/comment-pattern/index.js index e7cb9afb5c..bd2aa259b0 100644 --- a/lib/rules/comment-pattern/index.js +++ b/lib/rules/comment-pattern/index.js @@ -27,7 +27,6 @@ function rule(pattern) { root.walkComments((comment) => { const text = comment.text; - if (pattern.test(text)) { return; } From c426c6c714d772bc7558c2f9652affda2c7586d0 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Mon, 5 Oct 2020 08:50:00 +0300 Subject: [PATCH 4/9] PR fixes (supporting string as option + tests, formatting, improved readme) --- docs/user-guide/rules/list.md | 2 +- lib/rules/comment-pattern/README.md | 13 +++--- lib/rules/comment-pattern/__tests__/index.js | 49 ++++++++++++++++---- lib/rules/comment-pattern/index.js | 10 ++-- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/docs/user-guide/rules/list.md b/docs/user-guide/rules/list.md index 7b464d4806..fb3b6f7d1a 100644 --- a/docs/user-guide/rules/list.md +++ b/docs/user-guide/rules/list.md @@ -228,9 +228,9 @@ 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. -- [`comment-pattern`](../../../lib/rules/comment-pattern/README.md): Specify a pattern for comments. ### General / Sheet diff --git a/lib/rules/comment-pattern/README.md b/lib/rules/comment-pattern/README.md index 51a547b0c5..a0199cd8c2 100644 --- a/lib/rules/comment-pattern/README.md +++ b/lib/rules/comment-pattern/README.md @@ -4,22 +4,21 @@ Specify a pattern for comments. ```css - /*comment*/ - /* ↑ */ a { --foo-: 1px; } -/** ↑ +/* comment */ +/** ↑ * The pattern of this */ ``` ## Options -`regex` +`regex|string` -The pattern to match the comments with. +A string will be translated into a RegExp like so `new RegExp(yourString)` — so be sure to escape properly. -Given the regex: +Given the string: ``` -/foo .+/ +"foo .+" ``` The following patterns are considered violations: diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js index 552dd3eb42..e59fe9e3ee 100644 --- a/lib/rules/comment-pattern/__tests__/index.js +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -18,7 +18,7 @@ testRule({ \r\n \n\r \t - */`, + */`, }, { code: '.without-comment {}', @@ -28,11 +28,46 @@ testRule({ reject: [ { code: '/* not foo- */', - message: messages.expected, + message: messages.expected(/foo-.+/), }, { code: '/**/', - message: messages.expected, + message: messages.expected(/foo-.+/), + }, + ], +}); + +testRule({ + ruleName, + config: ['foo-.+'], + + accept: [ + { + code: '/* foo-valid yay */', + }, + { + code: `/* foo-- + multi-line + \n + \r + \r\n + \n\r + \t + */`, + }, + { + code: '.without-comment {}', + }, + ], + + reject: [ + { + code: '/* not foo- */', + message: messages.expected(/foo-.+/), + }, + { + code: '/**/', + message: messages.expected(/foo-.+/), }, ], }); @@ -62,15 +97,9 @@ testRule({ accept: [ { code: 'a {} // foo-ok', - description: 'ignored inline scss comments', }, { code: '// foo-ok', - description: 'ignored inline scss comments', - }, - { - code: '.without-comment { }', - description: 'ignores scss without comments', }, ], @@ -78,7 +107,7 @@ testRule({ { code: 'a {} // not-foo', description: 'checks inline scss comments', - message: messages.expected, + message: messages.expected(/foo-.+/), }, ], }); diff --git a/lib/rules/comment-pattern/index.js b/lib/rules/comment-pattern/index.js index bd2aa259b0..531faa4775 100644 --- a/lib/rules/comment-pattern/index.js +++ b/lib/rules/comment-pattern/index.js @@ -10,29 +10,31 @@ const validateOptions = require('../../utils/validateOptions'); const ruleName = 'comment-pattern'; const messages = ruleMessages(ruleName, { - expected: 'Expected comment to match specified pattern', + expected: (pattern) => `Expected comment to match specified pattern "${pattern}"`, }); function rule(pattern) { return (root, result) => { const validOptions = validateOptions(result, ruleName, { actual: pattern, - possible: [_.isRegExp], + possible: [_.isRegExp, _.isString], }); if (!validOptions) { return; } + const normalizedPattern = _.isString(pattern) ? new RegExp(pattern) : pattern; + root.walkComments((comment) => { const text = comment.text; - if (pattern.test(text)) { + if (normalizedPattern.test(text)) { return; } report({ - message: messages.expected, + message: messages.expected(normalizedPattern), node: comment, result, ruleName, From 2d15baca8433f9e7837d311241467c68c9784ce4 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Tue, 6 Oct 2020 08:32:36 +0300 Subject: [PATCH 5/9] specifying the original pattern in the error message + fixing tests --- lib/rules/comment-pattern/__tests__/index.js | 4 ++-- lib/rules/comment-pattern/index.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js index e59fe9e3ee..477ec50ec2 100644 --- a/lib/rules/comment-pattern/__tests__/index.js +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -63,11 +63,11 @@ testRule({ reject: [ { code: '/* not foo- */', - message: messages.expected(/foo-.+/), + message: messages.expected('foo-.+'), }, { code: '/**/', - message: messages.expected(/foo-.+/), + message: messages.expected('foo-.+'), }, ], }); diff --git a/lib/rules/comment-pattern/index.js b/lib/rules/comment-pattern/index.js index 531faa4775..b1894d1045 100644 --- a/lib/rules/comment-pattern/index.js +++ b/lib/rules/comment-pattern/index.js @@ -34,7 +34,7 @@ function rule(pattern) { } report({ - message: messages.expected(normalizedPattern), + message: messages.expected(pattern), node: comment, result, ruleName, From 7802de3a01b9a26cb6342e352799b827c6dcacbf Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Tue, 6 Oct 2020 21:33:51 +0300 Subject: [PATCH 6/9] Update lib/rules/comment-pattern/index.js Co-authored-by: Richard Hallows --- lib/rules/comment-pattern/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/comment-pattern/index.js b/lib/rules/comment-pattern/index.js index b1894d1045..c78aa4aa45 100644 --- a/lib/rules/comment-pattern/index.js +++ b/lib/rules/comment-pattern/index.js @@ -10,7 +10,7 @@ const validateOptions = require('../../utils/validateOptions'); const ruleName = 'comment-pattern'; const messages = ruleMessages(ruleName, { - expected: (pattern) => `Expected comment to match specified pattern "${pattern}"`, + expected: (pattern) => `Expected comment to match pattern "${pattern}"`, }); function rule(pattern) { From 9e847bce2f382531e78f892ac014072c0deb2af6 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Tue, 6 Oct 2020 21:41:52 +0300 Subject: [PATCH 7/9] removed irrelevant testRule --- lib/rules/comment-pattern/__tests__/index.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js index 477ec50ec2..3564dc5f69 100644 --- a/lib/rules/comment-pattern/__tests__/index.js +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -72,23 +72,6 @@ testRule({ ], }); -testRule({ - ruleName, - config: [true], - - accept: [ - { - code: '/* some comment */', - }, - { - code: '/**/', - }, - { - code: '.without-comment {}', - }, - ], -}); - testRule({ ruleName, config: [/foo-.+/], From 53366ad6ce10917a1e5e6fbc41e02e9341f271e3 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Wed, 7 Oct 2020 08:09:18 +0300 Subject: [PATCH 8/9] Update lib/rules/comment-pattern/README.md Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/rules/comment-pattern/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/comment-pattern/README.md b/lib/rules/comment-pattern/README.md index a0199cd8c2..b0a276e225 100644 --- a/lib/rules/comment-pattern/README.md +++ b/lib/rules/comment-pattern/README.md @@ -1,4 +1,4 @@ -# custom-property-pattern +# comment-pattern Specify a pattern for comments. From b02ec9efe0ae505a16d41dbfd28735127fcda934 Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Sun, 11 Oct 2020 20:51:56 +0300 Subject: [PATCH 9/9] removed redundant tests which are covered in basic checks --- lib/rules/comment-pattern/__tests__/index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/rules/comment-pattern/__tests__/index.js b/lib/rules/comment-pattern/__tests__/index.js index 3564dc5f69..c3ff4fcd43 100644 --- a/lib/rules/comment-pattern/__tests__/index.js +++ b/lib/rules/comment-pattern/__tests__/index.js @@ -20,9 +20,6 @@ testRule({ \t */`, }, - { - code: '.without-comment {}', - }, ], reject: [ @@ -55,9 +52,6 @@ testRule({ \t */`, }, - { - code: '.without-comment {}', - }, ], reject: [