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
Fix: multiline-comment-style rule add extra space after * (fixes #12785) #12823
Conversation
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 working on this! This looks like a regression I introduced in #12316. As you can see from the tests, the current behavior is intentional. If the original comment didn't have a leading offset space, it currently tries to preserve that formatting when it autofixes.
I think the correct fix here would be to check if any line starts with a /
character and, if there isn't leading whitespace being added, set the offset to a single space character to all lines. This would allow the rule to function as it does now for the majority of cases and handle this edge case gracefully.
Additionally, we require tests for all changes. Feel free to start with the case reported in the issue! |
Actually the problem is with the regular expression here https://github.com/eslint/eslint/pull/12316/files#diff-e8cce2004dd5e0386021b0e80370c8ebR316 and not the diff in the description. If we split the regular expression into two parts, one for the prefix before * and one after, I can get it to work. Do you want me to do that instead of "If the original comment didn't have a leading offset space, it currently tries to preserve that formatting". That way it code will be generic and we may not have to just check that one edge case specifically. Mean time I will try this and let you know |
To be clear, the behavior before the PR mentioned in the description was very different how it is now. Can you share an example of what you're thinking? I'm not sure I'm following. I think it's working correctly for other cases besides this one edge case, and we should keep the current behavior the same (which should be easy to verify since this rule is tested fairly well). |
So what I am saying is, in the diff pasted, this problem occurs when the preceeding line also does not have * and initial offset hence is an empty string. So I can just add a check to see if the initial offset is an empty string and if it is just set initialoffset to a string with a space. |
@karthikkp If I'm understanding correctly, that would cause the tests I linked above to fail. The rule tries to preserve the leading whitespace (or lack thereof) when it's converting the comment, so it's sometimes correct for the initial offset to be an empty string. I think this is the correct behavior (and was intentional when I made that change). Are there any other cases where malformed code can occur other than when there's no leading whitespace and the first character of a comment line is a |
Oh okay got it. Then I think handling the edge case alone will be the way to go. Will do that |
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 a working on this! Do you mind updating the PR title? I believe it's currently failing the commit message check because it is too long.
@@ -1379,6 +1379,25 @@ ${" "} | |||
{ messageId: "missingStar", line: 4 }, | |||
{ messageId: "endNewline", line: 4 } | |||
] | |||
}, |
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 we make this test suite a bit more robust by adding other kinds of formatted comments being formatted to starred-block
?
Some examples:
////This comment is in
//`separate-lines` format.
// // This comment is in
// `separate-lines` format.
/*
* // a line comment
*some.code();
*/
/*
// a line comment
* some.code();
*/
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.
The first two are not converted to starred block comments in the current version. So the fix that I am working should not change that behavior right?
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.
Oh, interesting. Yes, that makes sense to me. Thanks for adding these tests.
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 working on this! I left a question about the result of fixing.
…t white space after 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.
Thanks for the changes! I left a minor suggestion.
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.
LGTM, thank you!
@@ -1379,6 +1379,25 @@ ${" "} | |||
{ messageId: "missingStar", line: 4 }, | |||
{ messageId: "endNewline", line: 4 } | |||
] | |||
}, |
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.
Oh, interesting. Yes, that makes sense to me. Thanks for adding these tests.
@yeonjuan The changes you had requested have been made 👍 |
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.
LGTM, thanks!
Thanks for contributing to ESLint! |
…nt#12785) (eslint#12823) * Fix: multiline-comment-style rule missed an extra space after * (fixes eslint#12785) * logic changed to handled only the edge case * test case added for the edge case * checks full offset and apply space after * only if there already isn't white space after it. * addeed new test case
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
fixes #12785
What changes did you make? (Give an overview)
Added the extra white space which was removed here https://github.com/eslint/eslint/pull/12316/files#diff-e8cce2004dd5e0386021b0e80370c8ebL190
Is there anything you'd like reviewers to focus on?
No