Skip to content

Commit

Permalink
Do not offense UselessRestrictOnSend when defined on_send or after_se…
Browse files Browse the repository at this point in the history
…nd with alias
  • Loading branch information
kachick committed Jul 8, 2022
1 parent 757c4ce commit bb304a8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ class UselessRestrictOnSend < Base
MSG = 'Useless `RESTRICT_ON_SEND` is defined.'

# @!method defined_send_callback?(node)
def_node_search :defined_send_callback?, '(def {:on_send :after_send} ...)'
def_node_search :defined_send_callback?, <<~PATTERN
{
(def {:on_send :after_send} ...)
(alias (sym {:on_send :after_send}) _source ...)
(send nil? :alias_method {(sym {:on_send :after_send}) (str {"on_send" "after_send"})} _source ...)
}
PATTERN

def on_casgn(node)
return if !restrict_on_send?(node) || defined_send_callback?(node.parent)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/top_level_method_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Style
class TopLevelMethodDefinition < Base
MSG = 'Do not define methods at the top-level.'

RESTRICT_ON_SEND = %i[define_method].freeze # rubocop:disable InternalAffairs/UselessRestrictOnSend
RESTRICT_ON_SEND = %i[define_method].freeze

def on_def(node)
return unless top_level_method_definition?(node)
Expand Down
48 changes: 48 additions & 0 deletions spec/rubocop/cop/internal_affairs/useless_restrict_on_send_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ def on_send(node)
RUBY
end

it 'does not register an offense when using `RESTRICT_ON_SEND` and defines `on_send` with alias' do
expect_no_offenses(<<~RUBY)
class FooCop
RESTRICT_ON_SEND = %i[bad_method].freeze
def on_def(node)
# ...
end
alias on_send on_def
end
RUBY
end

it 'does not register an offense when using `RESTRICT_ON_SEND` and defines `on_send` with alias_method' do
expect_no_offenses(<<~RUBY)
class FooCop
RESTRICT_ON_SEND = %i[bad_method].freeze
def on_def(node)
# ...
end
alias_method :on_send, :on_def
end
RUBY
end

it 'does not register an offense when using `RESTRICT_ON_SEND` and defines `after_send`' do
expect_no_offenses(<<~RUBY)
class FooCop
Expand All @@ -37,4 +61,28 @@ def after_send(node)
end
RUBY
end

it 'does not register an offense when using `RESTRICT_ON_SEND` and defines `after_send` with alias' do
expect_no_offenses(<<~RUBY)
class FooCop
RESTRICT_ON_SEND = %i[bad_method].freeze
def after_send(node)
# ...
end
alias after_send any
end
RUBY
end

it 'does not register an offense when using `RESTRICT_ON_SEND` and defines `after_send` with alias_method' do
expect_no_offenses(<<~RUBY)
class FooCop
RESTRICT_ON_SEND = %i[bad_method].freeze
def after_send(node)
# ...
end
self.alias_method "after_send", "any"
end
RUBY
end
end

0 comments on commit bb304a8

Please sign in to comment.