New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "comment-pattern" rule #4962
Changes from 2 commits
4abfda5
13bfb87
7428b0f
c426c6c
2d15bac
7802de3
9e847bc
53366ad
b02ec9e
a28b506
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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-.+/), | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should match config, and in config we have string.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hudochenkov got it, fixed now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I didn't thought about that :) Now I'm not sure which one is better :) @stylelint/contributors what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems reasonable to me to always show the same form # .stylelintrc.yml
rules:
comment-pattern: ["foo"] // .stylelintrc.js
module.exports = {
rules: {
"comment-pattern": [/foo/] // equal to ["foo"]
}
} $ bin/stylelint.js a.css --config .stylelintrc.yml
a.css
1:1 ✖ Expected comment to match specified pattern "foo" comment-pattern
$ bin/stylelint.js a.css --config .stylelintrc.js
a.css
1:1 ✖ Expected comment to match specified pattern "/foo/" comment-pattern There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ybiquitous forgive me but I might be confused.. Are you suggesting to strip the slashes from the pattern when generating an error message? If so, how should regexes with modifiers look? e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I thought we could keep the same appearance of an error message in any config formats.
Sorry, I missed regex modifiers. Surely your current implementaion seems better if we need to include the modifiers. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok :) |
||||||||||||||||||||||
}, | ||||||||||||||||||||||
], | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
|
@@ -51,7 +86,7 @@ testRule({ | |||||||||||||||||||||
{ | ||||||||||||||||||||||
code: '.without-comment {}', | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
] | ||||||||||||||||||||||
], | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
|
||||||||||||||||||||||
testRule({ | ||||||||||||||||||||||
|
@@ -62,23 +97,17 @@ 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', | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
], | ||||||||||||||||||||||
|
||||||||||||||||||||||
reject: [ | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
code: 'a {} // not-foo', | ||||||||||||||||||||||
description: 'checks inline scss comments', | ||||||||||||||||||||||
message: messages.expected, | ||||||||||||||||||||||
message: messages.expected(/foo-.+/), | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
] | ||||||||||||||||||||||
], | ||||||||||||||||||||||
}); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,30 +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}"`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's drop "specified" as we show the pattern in the message. I think including the pattern is a good idea, but let's create a follow-up issue to do this consistently across the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great. I can open an issue soon |
||||||
}); | ||||||
|
||||||
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, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove these
.without-comment {}
tests as they are duplicated in the basic checks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I didn't know the basic checks :)