Skip to content
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

Merged
merged 3 commits into from
Jul 27, 2018

Conversation

platinumazure
Copy link
Member

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?

  • Are there any more test cases I should add? (Especially for the addElementListIndent change.)
  • Could I replace the hasBlankLinesBetween function with other helper functions (e.g., countTrailingLinebreaks) to improve maintainabillity?

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
@platinumazure platinumazure added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion indent Relates to the `indent` rule labels Jul 21, 2018
Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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.

@@ -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);
Copy link
Member

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).

@platinumazure platinumazure added the do not merge This pull request should not be merged yet label Jul 25, 2018
@platinumazure platinumazure removed the do not merge This pull request should not be merged yet label Jul 26, 2018
@platinumazure
Copy link
Member Author

@not-an-aardvark I've incorporated your feedback. Let me know if you see any other issues. Thanks!

@platinumazure platinumazure merged commit 9e76be7 into master Jul 27, 2018
@platinumazure platinumazure deleted the issue9733 branch July 27, 2018 03:46
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jan 24, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jan 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly indent Relates to the `indent` rule rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants