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
Handle implicit receivers in Style/InvertibleUnlessCondition
#12711
Handle implicit receivers in Style/InvertibleUnlessCondition
#12711
Conversation
Previously, we would attempt to call `node.receiver.source`, but `node.receiver` could be `nil`. This teaches the cop to handle implicit receivers.
4bbd24e
to
5c9d6a8
Compare
@@ -99,23 +99,23 @@ def preferred_condition(node) | |||
end | |||
end | |||
|
|||
def preferred_send_condition(node) | |||
receiver_source = node.receiver.source | |||
def preferred_send_condition(node) # rubocop:disable Metrics/CyclomaticComplexity |
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.
I didn't see a good way to avoid disabling this cop. It would require breaking apart this method, but I don't know that that would actually improve understandability.
Open to suggestions though.
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.
Another option might be to explicitly fallback to self
as the receiver_source
and then leave all the rest the same, and rely on the redundant self correction to clean it up, but I don't know if that would actually fix the complexity offense, as it still introduces the conditional, even if it's inline.
return receiver_source if node.method?(:!) | ||
|
||
receive = receiver_source ? "#{receiver_source}." : '' # receiver may be implicit (self) |
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.
I chose to do this instead of repeating the conditional
if receiver_source
# ...
else
# ...
end
in three places, since it's always the same logic.
I also considered receipt
as the variable name. Not sure.
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.
Any reason why you didn't go with the name receiver
?
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.
I wasn't sure if that would be confusing because we would then have receiver_source
and receiver
, but receiver
would be receiver_source + '.'
or nil
, not the "receiver" node, as one might expect.
I suppose another option could be dotted_receiver
or dotted_receiver_source
.
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.
I see, no great options here.
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.
Yeah. I did switch1 it to dotted_receiver_source
though, since that's clearer than receive
.
Footnotes
-
This change was accidentally not committed, and appears in Add missing PR feedback changes from #12711 #12722 instead. ↩
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.
I didn't add a case for an operator method, as the receiver would explicitly be self
.
Might also be good to add an example in the cop's description with an implicit receiver. |
I've switched1 to Footnotes
|
This change was meant to be in rubocop#12711.
This change was meant to be in rubocop#12711.
This change was meant to be in #12711.
This change was meant to be in #12711.
Previously, we would attempt to call
node.receiver.source
, butnode.receiver
could benil
.This teaches the cop to handle implicit receivers.
Before submitting the PR make sure the following are checked:
Commit message starts with[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.