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

Add new Style/CaseLikeIf cop #8266

Merged
merged 1 commit into from Jul 9, 2020
Merged

Conversation

fatkodima
Copy link
Contributor

This cop tries to convert if-elsifs to case-whens.

Algorithm, in brief:

case target
when conditions1
  # ...statements...
when conditions2
  # ...statements...
else
  # ...statements...
end
  1. Find possible target from if condition
  2. if found, then for each branch collect conditions and check if they are all depend on target and can be converted to case
  3. if all branches are convertible to cases, convert them

Closes #7736

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 8, 2020

Great work! I wonder if we can do better with the name, though. I'm thinking of CaseLikeIf (case-like if) or SingleValueDispatchIf, but maybe someone from @rubocop-hq/rubocop-core will have better ideas.

@koic
Copy link
Member

koic commented Jul 8, 2020

CaseLikeIf (case-like if)

It sounds very comfortable to me :-)

config/default.yml Outdated Show resolved Hide resolved
@fatkodima fatkodima changed the title Add new Style/IfElsifToCase cop Add new Style/CaseLikeIf cop Jul 8, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 9, 2020

The changes look good, but your branch has to be rebased on top of the current master branch due to merge conflicts.

@fatkodima
Copy link
Contributor Author

Rebased.

@bbatsov bbatsov merged commit 2f184b5 into rubocop:master Jul 9, 2020
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.

Suggest case/when if an if/elsif/else can be easily translated
3 participants