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

Added a cop Style/Send. Disabled by default. #2081

Merged
merged 1 commit into from
Aug 4, 2015

Conversation

syndbg
Copy link
Contributor

@syndbg syndbg commented Jul 28, 2015

Motivation:
I was reading through The Well Ground Rubyist 2nd edition where I read in chapter 2, that it's preferred to use __send__ or public_send instead of send.

Implementation:

  • Added a Style/Send cop that registers offences when send is used. Instead it recommends public_send or __send__.
  • Added the cop in disabled.yml. I think the cop is a bit too strict to be enabled by default.
  • Added send tests that verify correct behavior when linting code that uses public_send, send and __send__.

@@ -44,6 +44,11 @@ Style/MissingElse:
- case
- both

Style/Send:
Description: 'Prefer __send__ or public_send to send,
Copy link
Collaborator

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.

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 29, 2015

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 __send__ and public_send without receivers, which is obviously wrong.

syndbg added a commit to syndbg/ruby-style-guide that referenced this pull request Jul 29, 2015
@syndbg syndbg force-pushed the style_cop_send branch 2 times, most recently from a0ffab0 to 195e4d9 Compare July 29, 2015 17:45
syndbg added a commit to syndbg/ruby-style-guide that referenced this pull request Jul 29, 2015
as `send` may overlap with existing methods.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#prefer-public-send'
Enabled: false

Copy link
Contributor Author

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.

@syndbg
Copy link
Contributor Author

syndbg commented Jul 29, 2015

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.'
Copy link
Collaborator

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.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 1, 2015

@syndbg ping

@syndbg
Copy link
Contributor Author

syndbg commented Aug 1, 2015

@bbatsov pong. Sorry about the delays 🎱

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 1, 2015

A bit more feedback:

  • you need some tests where the invocations have receivers
  • the commit message doesn't match the project's commit message guidelines

@syndbg syndbg force-pushed the style_cop_send branch 2 times, most recently from d0c8140 to 06b8309 Compare August 1, 2015 11:32
inspect_source(cop, 'Object.send(:inspect)')
end

it 'registers an offense for an invocation without args' do
Copy link
Collaborator

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.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 4, 2015

The commit message should be New copStyle/Sendchecks for the use ofsend(disabled by defaut).

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`'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Object#send

Copy link
Collaborator

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 ..

@syndbg
Copy link
Contributor Author

syndbg commented Aug 4, 2015

@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 ?

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 4, 2015

This is the changelog entry format. :-)

@syndbg
Copy link
Contributor Author

syndbg commented Aug 4, 2015

Alright.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 4, 2015

One final remark - drop that . from the commit message's title. Titles are not sentences. That's also mentioned in our CONTRIBUTING.md.

bbatsov added a commit that referenced this pull request Aug 4, 2015
@bbatsov bbatsov merged commit 242a761 into rubocop:master Aug 4, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 4, 2015

👍 Thanks!

@syndbg
Copy link
Contributor Author

syndbg commented Aug 4, 2015

Thanks for the patience! 🍻

@syndbg syndbg deleted the style_cop_send branch August 4, 2015 16:36
syndbg added a commit to syndbg/ruby-style-guide that referenced this pull request Aug 9, 2015
shyouhei pushed a commit to shyouhei/ruby-style-guide that referenced this pull request Nov 11, 2015
shyouhei added a commit to shyouhei/ruby-style-guide that referenced this pull request Nov 16, 2015
shyouhei added a commit to shyouhei/ruby-style-guide that referenced this pull request Nov 17, 2015
ellimist pushed a commit to ellimist/ruby-style-guide that referenced this pull request Mar 15, 2017
@andyw8
Copy link
Contributor

andyw8 commented Apr 6, 2017

@syndbg I think this should have been two separate cops.

One should be to prefer __send__ over send (in case send is already defined).

The other should be to prefer public_send, when calling another object, so as to respect's the object's encapsulation, since send or __send__ will circumvent private or protected.

(I was the author of that rule in the style guide (rubocop/ruby-style-guide#360)).

Any thoughts?

sauloperez added a commit to coopdevs/openfoodnetwork that referenced this pull request Aug 30, 2018
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
sauloperez added a commit to coopdevs/openfoodnetwork that referenced this pull request Aug 30, 2018
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
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.

3 participants