Skip to content

Commit

Permalink
New cop Style/Send checks for the use of send (disabled by default)
Browse files Browse the repository at this point in the history
  • Loading branch information
syndbg committed Aug 4, 2015
1 parent 6ef5a23 commit 35ef5ed
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#2081](https://github.com/bbatsov/rubocop/pull/2081): New cop `Style/Send` checks for the use of `send` and instead encourages changing it to `BasicObject#__send__` or `Object#public_send` (disabled by default). ([@syndbg][])
* [#2057](https://github.com/bbatsov/rubocop/pull/2057): New cop `Lint/FormatParameterMismatch` checks for a mismatch between the number of fields expected in format/sprintf/% and what was pased to it. ([@edmz][])
* [#2010](https://github.com/bbatsov/rubocop/pull/2010): Add `space` style for SpaceInsideStringInterpolation. ([@gotrevor][])
* [#2007](https://github.com/bbatsov/rubocop/pull/2007): Allow any modifier before `def`, not only visibility modifiers. ([@fphilipe][])
Expand Down Expand Up @@ -1521,3 +1522,4 @@
[@maxjacobson]: https://github.com/maxjacobson
[@sliuu]: https://github.com/sliuu
[@edmz]: https://github.com/edmz
[@syndbg]: https://github.com/syndbg
4 changes: 4 additions & 0 deletions config/disabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Style/MissingElse:

Style/OptionHash:
Description: "Don't use option hashes when you can use keyword arguments."

Style/Send:
Description: 'Prefer `Object#__send__` or `Object#public_send` to `send`, as `send` may overlap with existing methods.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#prefer-public-send'
Enabled: false

Style/SymbolArray:
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
require 'rubocop/cop/style/rescue_modifier'
require 'rubocop/cop/style/self_assignment'
require 'rubocop/cop/style/semicolon'
require 'rubocop/cop/style/send'
require 'rubocop/cop/style/signal_exception'
require 'rubocop/cop/style/single_line_block_params'
require 'rubocop/cop/style/single_line_methods'
Expand Down
18 changes: 18 additions & 0 deletions lib/rubocop/cop/style/send.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# encoding: utf-8

module RuboCop
module Cop
module Style
# This cop checks for the use of the send method.
class Send < Cop
MSG = 'Prefer `Object#__send__` or `Object#public_send` to `send`.'

def on_send(node)
_receiver, method_name, *args = *node
return unless method_name == :send && args.length > 0
add_offense(node, :selector)
end
end
end
end
end
81 changes: 81 additions & 0 deletions spec/rubocop/cop/style/send_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# encoding: utf-8

require 'spec_helper'

describe RuboCop::Cop::Style::Send do
subject(:cop) { described_class.new }

context 'with send' do
context 'and with a receiver' do
it 'registers an offense for an invocation with args' do
inspect_source(cop, 'Object.send(:inspect)')
expect(cop.offenses.size).to eq(1)
end

it 'does not register an offense for an invocation without args' do
inspect_source(cop, 'Object.send')
expect(cop.offenses).to be_empty
end
end

context 'and without a receiver' do
it 'registers an offense for an invocation with args' do
inspect_source(cop, 'send(:inspect)')
expect(cop.offenses.size).to eq(1)
end

it 'does not register an offense for an invocation without args' do
inspect_source(cop, 'send')
expect(cop.offenses).to be_empty
end
end
end

context 'with __send__' do
after(:each) { expect(cop.offenses).to be_empty }

context 'and with a receiver' do
it 'does not register an offense for an invocation with args' do
inspect_source(cop, 'Object.__send__(:inspect)')
end

it 'does not register an offense for an invocation without args' do
inspect_source(cop, 'Object.__send__')
end
end

context 'and without a receiver' do
it 'does not register an offense for an invocation with args' do
inspect_source(cop, '__send__(:inspect)')
end

it 'does not register an offense for an invocation without args' do
inspect_source(cop, '__send__')
end
end
end

context 'with public_send' do
after(:each) { expect(cop.offenses).to be_empty }

context 'and with a receiver' do
it 'does not register an offense for an invocation with args' do
inspect_source(cop, 'Object.public_send(:inspect)')
end

it 'does not register an offense for an invocation without args' do
inspect_source(cop, 'Object.public_send')
end
end

context 'and without a receiver' do
it 'does not register an offense for an invocation with args' do
inspect_source(cop, 'public_send(:inspect)')
end

it 'does not register an offense for an invocation without args' do
inspect_source(cop, 'public_send')
end
end
end
end

0 comments on commit 35ef5ed

Please sign in to comment.