Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <jeddy3@users.noreply.github.com> * 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 <jeddy3@users.noreply.github.com> Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
- Loading branch information
1 parent
b0a0ee4
commit cba295e
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# comment-pattern | ||
|
||
Specify a pattern for comments. | ||
|
||
<!-- prettier-ignore --> | ||
```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: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
/*not starting with foo*/ | ||
a { color: red; } | ||
``` | ||
|
||
The following patterns are _not_ considered violations: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
/*foo at the beginning*/ | ||
a { color: red; } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-.+/), | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters