Skip to content

Commit

Permalink
Merge pull request rubocop#10796 from kachick/fix-wrog-detection-for-…
Browse files Browse the repository at this point in the history
…all-methods

Fix wrong offense with `Style/TopLevelMethodDefinition` when just calling methods on top-level
  • Loading branch information
koic committed Jul 8, 2022
2 parents 795739f + bb304a8 commit c3fb3a3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
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: 2 additions & 0 deletions lib/rubocop/cop/style/top_level_method_definition.rb
Expand Up @@ -47,6 +47,8 @@ module Style
class TopLevelMethodDefinition < Base
MSG = 'Do not define methods at the top-level.'

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
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
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/top_level_method_definition_spec.rb
Expand Up @@ -97,4 +97,10 @@ class Foo
end
RUBY
end

it 'does not register an offense when just called method on top-level' do
expect_no_offenses(<<~RUBY)
require_relative 'foo'
RUBY
end
end

0 comments on commit c3fb3a3

Please sign in to comment.