forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New cop
Style/Send
checks for the use of send
(disabled by default)
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |