-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
New: multiline-comment-style rule (fixes #8320) #9389
Conversation
This code is currently messy and incomplete. There are also some edge cases that the rule's design needs to address (e.g. allowing "banner" comments which are created from several consecutive line comments).
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.
Thanks for the PR! This mostly looks good -- I just noticed a small bug.
`, | ||
options: ["bare-block"], | ||
errors: [{ message: EXPECTED_BLOCK_ERROR, line: 2 }] | ||
} |
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.
Can you add a test where a single-line comment contains */
? I think this will currently cause invalid syntax.
/* eslint multiline-comment-style: [error, bare-block] */
// foo */
// bar
lib/rules/multiline-comment-style.js
Outdated
: MISSING_STAR_ERROR, | ||
fix(fixer) { | ||
|
||
// TODO: Make this more readable, possibly by splitting it into two separate cases. |
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.
Is this comment still needed?
lib/rules/multiline-comment-style.js
Outdated
}, | ||
message: EXPECTED_BLOCK_ERROR, | ||
fix(fixer) { | ||
return fixer.replaceTextRange([commentGroup[0].range[0], commentGroup[commentGroup.length - 1].range[1]], convertToBlock(commentGroup[0], commentLines.filter(line => line))); |
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.
Nitpick: Can you split this onto multiple lines?
return fixer.replaceTextRange(
[commentGroup[0].range[0], commentGroup[commentGroup.length - 1].range[1]],
convertToBlock(commentGroup[0], commentLines.filter(line => line))
);
(As a separate note, maybe we should enable max-len
on the codebase.)
lib/rules/multiline-comment-style.js
Outdated
// prohibits block comments from having a * at the beginning of each line. | ||
if (commentGroup[0].type === "Block") { | ||
const block = commentGroup[0]; | ||
const lines = block.value.split(astUtils.LINEBREAK_MATCHER).slice(1, -1); |
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.
Why is .slice(1, -1)
used here? It seems like this will omit the last line if someone does this:
/* foo
* bar */
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.
this is to skip the first line and last line in some cases like this:
/*
* foo
* bar
*/
but seems not ideal. 😂
lib/rules/multiline-comment-style.js
Outdated
}, | ||
message: EXPECTED_LINES_ERROR, | ||
fix(fixer) { | ||
return fixer.replaceTextRange(block.range, convertToSeparateLines(block, commentLines.filter(line => line))); |
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.
Nitpick: This could be
return fixer.replaceText(block, convertToSeparateLines(block, commentLines.filter(line => line)));
lib/rules/multiline-comment-style.js
Outdated
}, | ||
message: EXPECTED_BLOCK_ERROR, | ||
fix(fixer) { | ||
return fixer.replaceTextRange(block.range, convertToBlock(block, commentLines.filter(line => line))); |
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.
Nitpick: This could be
return fixer.replaceText(block, convertToBlock(block, commentLines.filter(line => line)));
(Maybe we should create a new rule in eslint-plugin-eslint-plugin
for it 😃)
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.
good idea!
}, | ||
{ | ||
code: ` | ||
/* this blockk |
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.
Nitpick: There is a typo here (this should just say block
).
lib/rules/multiline-comment-style.js
Outdated
}); | ||
} | ||
|
||
// TODO: clean this up |
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.
Is this comment still needed?
Squashed commits: [914bea1] fix: fixer would cause invalid syntax. [a4dd76c] Fix: isJSDoc checking each line starts with "*". [e16985a] prefer fixer.replaceText(). .
LGTM |
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.
Looks good to me. Thanks!
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.
This is looking great! Just a few comments/questions.
return { | ||
Program() { | ||
return sourceCode.getAllComments() | ||
.filter(comment => comment.type !== "Shebang") |
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.
Nitpick: Is there a reason all these checks can't happen in one .filter()
to cut down on the number of times we have to iterate over the comments array?
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.
I don't feel strong about this. will update if you like it~ 😄
|
||
// disallows consecutive line comments in favor of using a block comment. | ||
if (commentGroup[0].type === "Line" && commentLines.length > 1 && | ||
!commentLines.some(value => value.includes("*/"))) { |
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.
Why is this check necessary? Example of something that would be ignored that I'm not sure should be:
// Here's some regex /\w*/ in
// a comment.
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.
Answered by #9389 (comment)
"starred-block"(commentGroup) { | ||
const commentLines = getCommentLines(commentGroup); | ||
|
||
if (commentLines.some(value => value.includes("*/"))) { |
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.
Why is this check necessary? Example of something that would be ignored that I'm not sure should be:
// Here's some regex /\w*/ in
// a comment.
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.
*/
cannot exsist in a block comment, e.g.
/* Here's some regex /\w*/ in
a comment. */
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.
Unless I'm mistaken, can't commentLines
also include Line
comments? Or maybe a better question is this: what would happen for the example I listed above? Apologies if I'm misreading or misunderstanding something!
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.
This part of the rule looks at line comments and reports an error to indicate that they should be converted to block comments. However, line comments that contain */
can't be converted to block comments, so it doesn't make sense for the rule to report them.
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.
Ah, right - got it! Thanks!
function isJSDoc(commentGroup) { | ||
const lines = commentGroup[0].value.split(astUtils.LINEBREAK_MATCHER); | ||
|
||
return commentGroup[0].type === "Block" && |
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.
I wonder if we should make extract this into ast-utils so we can reuse it?
Another place we do a similar (though less rigorous) check that we could unify: https://github.com/eslint/eslint/blob/master/lib/util/source-code.js#L295
Thanks for contributing to ESLint! |
What is the purpose of this pull request? (put an "X" next to item)
[x] New rule (template)
What changes did you make? (Give an overview)
fixes #8320
Is there anything you'd like reviewers to focus on?