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

[Fix #10147] Fix Lint/ElseLayout for else nodes with a single line body #10192

Closed
wants to merge 1 commit into from
Closed

[Fix #10147] Fix Lint/ElseLayout for else nodes with a single line body #10192

wants to merge 1 commit into from

Conversation

jkeck
Copy link

@jkeck jkeck commented Oct 16, 2021

The ElseLayout cop, in spirit, is trying to prevent accidentally using
else when a user meant to use elsif based on the fact that they used
else in a similar way as elsif (by passing an argument to it).

However; the syntax of having a compact single line else statement with
no other body is valid and likely shouldn't be flagged as problematic.

This commit stops ElseLayout cop from reporting an offense when all the
else branch's children are on the same line as the else node itself,
while still reporting errors if there are children on other lines in the
else branch (which is likely a mistake)


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

child.loc.line if child.is_a?(RuboCop::AST::Node)
end.compact

child_lines.push(node.else_branch.loc.line).all?(node.loc.else.line)
Copy link
Author

Choose a reason for hiding this comment

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

This works, but if there is any guidance on better ways to approach what I'm trying to accomplish here I would be happy to update this PR.

This is trying to take an approach somewhat similar to what was described in bullet 3 of #10147 (comment) (I believe), but really just tries to determine if all the children of the else branch are on the same line (thus being the return of the else branch).

…body

The ElseLayout cop, in spirit, is trying to prevent accidentally using
else when a user meant to use elsif based on the fact that they used
else in a similar way as elsif (by passing an argument to it).

However; the syntax of having a compact single line else statement with
no other body is valid and likely shouldn't be flagged as problematic.

This commit stops ElseLayout cop from reporting an offense when all the
else branch's children are on the same line as the else node itself,
while still reporting errors if there are children on other lines in the
else branch (which is likely a mistake)
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 20, 2021

I've never seen anyone write else body on the same line as the else, so I don't feel comfortable with the proposed change. For the cop works as expected right now and the change would be a step back.

@jkeck
Copy link
Author

jkeck commented Oct 20, 2021

Fair enough.

FWIW, this pattern exists for the person who filed the issue #10147 presumably, in the codebase that I work on, as well as the Filewatcher codebase (ref: filewatcher/filewatcher#194).

I appreciate you taking the time to review this PR.

@jkeck jkeck closed this Oct 20, 2021
@jkeck jkeck deleted the allow-else-wo-body-10147 branch October 20, 2021 16:14
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 20, 2021

Yeah, I have no doubt the pattern exists in some codebases, but we have to optimize for the most common pattern. I can consider a configuration option for this if there's enough demand. Thanks for the PR regardless!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants