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: preserve whitespace in multiline-comment-style (fixes #12312) #12316
Conversation
428cabc
to
c478675
Compare
Thank you for fixing. Would you add tests for other options? For example, {
code: `
/*
* {
* "foo": 1,
* "bar": 2
* }
*/
`,
output: `
// {
// "foo": 1,
// "bar": 2
// }
`,
options: ["separate-lines"],
errors: [
{ messageId: "expectedLines", line: 2 }
]
} |
WIP! Updated this PR but added the "do not merge" label because I need to go back through and refactor/clean up the PR. |
4844b82
to
50b9391
Compare
This is ready for re-review! |
// { | ||
// "foo": 1, | ||
// "bar": 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.
We could update the autofixer to not include trailing whitespace, but I wasn't sure it was worth the extra complexity. Thoughts?
I added this because many people configure their editors to remove trailing whitespace automatically, and that could lead to a frustratingly vague error when running the tests.
60b3456
to
a77a6d4
Compare
@mysticatea This has been updated! |
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.
Just one question. Thanks for working on this!
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!
I appreciate the explicit template interpolations for trailing whitespace as it makes it much easier to know, at a glance, what is going on.
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 really good to me, thank you!
But I have a concern that the autofix removes empty lines in some situations.
71d6c76
to
0e7cb29
Compare
I have two last failing tests to fix. Marking as do not merge until I do so. |
1cec21f
to
3c9007f
Compare
3c9007f
to
f3f1232
Compare
Sorry for the delay! This ended up being a much more complex change than I anticipated 😄 Thanks for the great reviews! |
@mysticatea Friendly ping :) |
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.
Thank you for the update and sorry for my late response.
Looks really great to me!
There are nice refactoring and many tests.
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 #12312
What changes did you make? (Give an overview)
This PR preserves leading whitespace when converting a block comment to a "starred" block comment in themultiline-comment-style
rule's autofixer. Since this rule doesn't know about indentation, I decided to take the strategy of checking if the leading whitespace matches the expected whitespace (i.e. the whitespace before the opening delimiter for the block comment) and, if it does, to use any remaining whitespace after the asterisk. I think this will work for the vast majority of cases (though it could get weird if folks mix tabs and spaces). If they don't match, it falls back to the current behavior and adds one space.Apologies for the large diff - I ended up doing a lot of refactoring to try to make the code easier to follow. All the calculation of how much whitespace should be used to offset each line now gets calculated in
getCommentLines()
, which I think is a clearer separation of concerns between this module's internal functions.I believe the autofix should be consistent across formats now. In the case of an easy to use delimiter (
*
forstarred-block
or//
forseparate-lines
), calculating the whitespace is pretty straightforward. Forbare-block
, the check now iterates through the lines of the comment and checks if any lines are indented less than the opening line, and offsets all the lines by the difference. If it's indented more, it removes the appropriate amount of whitespace. Otherwise, if it's correct, it returns the line unchanged.Is there anything you'd like reviewers to focus on?
This only preserves whitespace if the the leading whitespace on the line matches the leading whitespace before the opening delimiter of the block comment (/*
). Does this seem like a good way to go about this? In other cases, I'm not sure how you would know exactly how much to indent something (particularly if there's a mix of tabs and spaces).Not directly related to this change, but I think it's a bit strange that we don't push a space after the asterisk when we convert line comments to "starred" block comments. I have added a test to document this behavior.