-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Update: indent comments w/ nearby code if no blank lines (fixes #9733) #10640
Conversation
Also needed to ensure that comments between element lists have desired offsets set if first element is on same line as beginning of element list
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!
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.
Actually I have a small nitpick, but it's not a blocker.
lib/rules/indent.js
Outdated
@@ -856,6 +856,10 @@ module.exports = { | |||
previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line | |||
) { | |||
offsets.setDesiredOffsets(element.range, firstTokenOfPreviousElement, 0); | |||
|
|||
sourceCode.getCommentsBefore(element).forEach(comment => { | |||
offsets.setDesiredOffsets(comment.range, firstTokenOfPreviousElement, 0); |
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.
Could this be combined with the line above into something like this instead?
offsets.setDesiredOffsets(
[previousElement.range[1], element.range[1]],
firstTokenOfPreviousElement,
0
);
offsets.setDesiredOffsets
accepts a text range as the first argument, and modifies the indentation of all tokens in that range, without needing to iterate over all of those tokens. So it's not necessary to call it multiple times on separate consecutive tokens, and doing so ends up being slightly worse for performance (although it's unlikely to matter much in this case since users probably don't have large numbers of consecutive comments between elements).
@not-an-aardvark I've incorporated your feedback. Let me know if you see any other issues. Thanks! |
Also needed to ensure that comments between element lists have desired offsets set if first element is on same line as beginning of element list
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
See issue #9733.
What changes did you make? (Give an overview)
Ensure that comments (which are the first or sole content of a line) can only line up with preceding or following tokens if those tokens are on the immediately previous line. Otherwise, comments must be in "correct" location.
Fixing this exposed an issue in
addElementListIndent
where, when second and later elements in an element list need to be aligned with the first element and the first element begins on the same line as the containing node, comment tokens between the elements also must have offsets explicitly specified as being relative to the first element's first token. (An example of where this issue would have manifested can be found here: The test cases in this file are all aligned with the list container, but the comment's desired offset was still 4 spaces to the right from there. This wasn't an issue before I wrote this PR, because the comment indent fallback logic was allowing it to anchor to the tokens 2 lines above or below.)Although the issue was accepted as a bug, I know this could increase warnings, so I've committed this as an "Update".
Is there anything you'd like reviewers to focus on?
hasBlankLinesBetween
function with other helper functions (e.g.,countTrailingLinebreaks
) to improve maintainabillity?