-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Added a cop Style/Send. Disabled by default. #2081
Conversation
@@ -44,6 +44,11 @@ Style/MissingElse: | |||
- case | |||
- both | |||
|
|||
Style/Send: | |||
Description: 'Prefer __send__ or public_send to send, |
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.
It's better to qualify those - BasicObject#__send__
and Object#public_send
.
That's a good idea and you should submit a matching rule to the community Ruby Style Guide. The implementation, however, has to be fixed, as right now you'll be registering offences for any method invocation except |
Submitted as requested in rubocop/rubocop#2081
a0ffab0
to
195e4d9
Compare
Submitted as requested in rubocop/rubocop#2081
as `send` may overlap with existing methods.' | ||
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#prefer-public-send' | ||
Enabled: false | ||
|
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.
Updated description
and linked the relevant style guide
. The rule about preferring __send__
over send
will appear beneath prefer-public-send
, so it's intentional that I link to prefer-public-send
.
Rebased latest master. Should be good for another review. Sorry for the troubles. |
@@ -44,6 +44,12 @@ Style/MissingElse: | |||
- case | |||
- both | |||
|
|||
Style/Send: | |||
Description: 'Prefer `BasicObject#__send__` or `Object#public_send` to `send`, | |||
as `send` may overlap with existing methods.' |
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.
No need to split this string here.
@syndbg ping |
@bbatsov pong. Sorry about the delays 🎱 |
A bit more feedback:
|
d0c8140
to
06b8309
Compare
inspect_source(cop, 'Object.send(:inspect)') | ||
end | ||
|
||
it 'registers an offense for an invocation without args' do |
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.
Btw, why are we registering an offence for send
without arguments? That would clearly be a false positive. We should change this.
The commit message should be |
module Style | ||
# This cop checks for the use of the send method. | ||
class Send < Cop | ||
MSG = 'Prefer `BasicObject#__send__` or `Object#public_send` to `send`' |
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.
Object#send
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.
The message should end with .
.
@bbatsov Regarding the commit message, isn't it the same format as the example ones in https://github.com/bbatsov/rubocop/blob/master/CONTRIBUTING.md#changelog-entry-format ? |
This is the changelog entry format. :-) |
Alright. |
One final remark - drop that |
👍 Thanks! |
Thanks for the patience! 🍻 |
Submitted as requested in rubocop/rubocop#2081
Submitted as requested in rubocop/rubocop#2081
Submitted as requested in rubocop/rubocop#2081
@syndbg I think this should have been two separate cops. One should be to prefer The other should be to prefer (I was the author of that rule in the style guide (rubocop/ruby-style-guide#360)). Any thoughts? |
Currently the Style/Send cop doesn't enforce `#public_send` however (that's what we want). It simply discourages the use of #send. See rubocop/rubocop#2081 (comment) for details. A new entry on the Code Conventions doc has been added to overcome this limitation: https://github.com/openfoodfoundation/openfoodnetwork/wiki/Code-Conventions#prefer-public_send-over-send
Devs keep using `#send` although that method does not preserve private/protected visibility. Watching after this turned out to be quite time-consuming while doing code review. Currently, the Style/Send cop doesn't enforce `#public_send` however (that's what we want). It simply discourages the use of #send. See rubocop/rubocop#2081 (comment) for details. So a new entry on the Code Conventions doc has been added to overcome this limitation: https://github.com/openfoodfoundation/openfoodnetwork/wiki/Code-Conventions#prefer-public_send-over-send
Motivation:
I was reading through The Well Ground Rubyist 2nd edition where I read in chapter 2, that it's preferred to use
__send__
orpublic_send
instead ofsend
.Implementation:
send
is used. Instead it recommendspublic_send
or__send__
.disabled.yml
. I think the cop is a bit too strict to be enabled by default.send
tests that verify correct behavior when linting code that usespublic_send
,send
and__send__
.