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/SoleNestedConditional
cop
#8390
Conversation
I can see how this: if important_criteria
do_something(:important, 'with a long argument') unless unlikely_minor_cond?
end
# could be better than
if important_criteria && !unlikely_minor_cond?
do_something(:important, 'with a long argument')
end |
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.
Also, did you handle the cases if (var = calculate)
manually? That's cheating 🤣. I think that's if a criteria is an assignment than no offense should be raised, no?
I don't see any comments in the specs, are these handled correctly? We should always put comments in our specs, as these are typically difficult to handle, especially when there are line changes.
Not sure. Personally, I'm happy with offense raised in this case 😄 I extracted that cases as a separate assignment before conditional, but would also just append nested conditions to that assignment. I looks more simpler/clear to me as a separate line. |
ade4a56
to
e4312d8
Compare
Added |
The changes look good, but your branch has to be rebased on top of the current |
e4312d8
to
6819bda
Compare
Rebased. |
Thanks! |
Follow rubocop/rubocop#8390. ```console % bundle exec rake Offenses: lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:57:20: C: Style/SoleNestedConditional: Consider merging nested conditions into outer unless conditions. return if valid_options?(association_with_options?(node)) ^^ 185 files inspected, 1 offense detected, 1 offense auto-correctable RuboCop failed! ```
This cop checks for places where nested conditionals can be merged into outer ones, like:
This, imo, makes code more readable and, for sure, reduces nestings.
Inspired by Style/IfInsideElse cop.
That cop also has (disabled, btw, in
rubocop
s.rubocop.yml
file)AllowIfModifier
setting. I haven't added that as I haven't get the point of its usefulness and how thiscan be better than
Probably, the cop name and wordings can be improved.