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 #11006] Allow multiple elsif for Style/IfWithBooleanLiteralBranches #11007

Conversation

koic
Copy link
Member

@koic koic commented Sep 16, 2022

Fixes #11006.

This PR allows multiple elsif for Style/IfWithBooleanLiteralBranches.

I think it's good to allow when multiple elsifs are used instead of a new config option. OTOH, if there is only single elsif, elsif will not exist due to autocorrection. So I think the current behavior can be kept.


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.

@koic koic force-pushed the change_allow_multiple_elsif_for_style_if_with_boolean_literal_branches branch from ee749be to 7953319 Compare September 16, 2022 12:40
Copy link

@BuonOmo BuonOmo left a comment

Choose a reason for hiding this comment

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

That was fast! LGTM :)

@@ -8,6 +8,20 @@ module Style
# The conditions to be checked are comparison methods, predicate methods, and double negative.
# `nonzero?` method is allowed by default.
# These are customizable with `AllowedMethods` option.
# Multiple `elsif`s with booleans in branches are allowed for readability.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps it'd be better to mention that this cop targets only ifs with a single else branch. This seems clearer to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you! I've tweaked the text to "This cop targets only ifs with a single elsif or else branch".

@koic koic force-pushed the change_allow_multiple_elsif_for_style_if_with_boolean_literal_branches branch from 7953319 to f688561 Compare September 16, 2022 13:43
@@ -8,6 +8,20 @@ module Style
# The conditions to be checked are comparison methods, predicate methods, and double negative.
# `nonzero?` method is allowed by default.
# These are customizable with `AllowedMethods` option.
# This cop targets only `if`s with a single `elsif` or `else` branch.
#
# [source,console]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't it say ruby here instead of console?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops! I've fixed.

@@ -76,6 +90,12 @@ def on_if(node)

private

def multiple_elsif?(node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We might add this to rubocop-ast as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good idea! I will open a PR to rubocop-ast.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've opened rubocop/rubocop-ast#239 to rubocop-ast.

Copy link
Member Author

Choose a reason for hiding this comment

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

The above PR has been closed because there are currently no plans to use it outside of this PR. I would like to extract to rubocop-ast when a concrete reuse use case comes up.

…teralBranches`

Fixes rubocop#11006.

This PR allows multiple `elsif` for `Style/IfWithBooleanLiteralBranches`.

I think it's good to allow when multiple `elsif`s are used instead of a new config option.
OTOH, if there is only single `elsif`, `elsif` will not exist due to autocorrection.
So I think the current behavior can be kept.
@koic koic force-pushed the change_allow_multiple_elsif_for_style_if_with_boolean_literal_branches branch from f688561 to 8122219 Compare September 16, 2022 15:21
koic added a commit to koic/rubocop-ast that referenced this pull request Sep 16, 2022
Follow up rubocop/rubocop#11007 (comment).

This PR adds `multiple_elsif` to `IfNode`.
koic added a commit to koic/rubocop-ast that referenced this pull request Sep 16, 2022
Follow up rubocop/rubocop#11007 (comment).

This PR adds `multiple_elsif` to `IfNode`.
koic added a commit to koic/rubocop-ast that referenced this pull request Sep 16, 2022
Follow up rubocop/rubocop#11007 (comment).

This PR adds `multiple_elsif` to `IfNode`.
@bbatsov bbatsov merged commit 6e910eb into rubocop:master Nov 1, 2022
@koic koic deleted the change_allow_multiple_elsif_for_style_if_with_boolean_literal_branches branch November 1, 2022 07:04
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 1, 2022

I had forgotten about this PR. 🤦

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.

Style/IfWithBooleanLiteralBranches: multiple branches readability
3 participants