Skip to content

Commit

Permalink
[Fix rubocop#10859] Fix Lint/Debugger to be able to handle method c…
Browse files Browse the repository at this point in the history
…hains correctly.
  • Loading branch information
dvandersluis committed Aug 4, 2022
1 parent 0658f99 commit 2a51b79
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_lintdebugger_to_be_able_to_handle.md
@@ -0,0 +1 @@
* [#10859](https://github.com/rubocop/rubocop/issues/10859): Fix `Lint/Debugger` to be able to handle method chains correctly. ([@dvandersluis][])
4 changes: 4 additions & 0 deletions config/default.yml
Expand Up @@ -1604,6 +1604,7 @@ Lint/Debugger:
# a user's configuration, but are otherwise not significant.
Kernel:
- binding.irb
- Kernel.binding.irb
Byebug:
- byebug
- remote_byebug
Expand All @@ -1621,6 +1622,9 @@ Lint/Debugger:
- binding.pry
- binding.remote_pry
- binding.pry_remote
- Kernel.binding.pry
- Kernel.binding.remote_pry
- Kernel.binding.pry_remote
- Pry.rescue
Rails:
- debugger
Expand Down
30 changes: 15 additions & 15 deletions lib/rubocop/cop/lint/debugger.rb
Expand Up @@ -67,19 +67,6 @@ module Lint
class Debugger < Base
MSG = 'Remove debugger entry point `%<source>s`.'

# @!method kernel?(node)
def_node_matcher :kernel?, <<~PATTERN
(const {nil? cbase} :Kernel)
PATTERN

# @!method valid_receiver?(node, arg1)
def_node_matcher :valid_receiver?, <<~PATTERN
{
(const {nil? cbase} %1)
(send {nil? #kernel?} %1)
}
PATTERN

def on_send(node)
return unless debugger_method?(node)

Expand All @@ -101,7 +88,7 @@ def debugger_methods

*receiver, method_name = v.split('.')
{
receiver: receiver.empty? ? nil : receiver.join.to_sym,
receiver: receiver.empty? ? nil : receiver.map(&:to_sym),
method_name: method_name.to_sym
}
end.compact
Expand All @@ -115,10 +102,23 @@ def debugger_method?(send_node)
if method[:receiver].nil?
send_node.receiver.nil?
else
valid_receiver?(send_node.receiver, method[:receiver])
method[:receiver] == receiver_chain(send_node)
end
end
end

def receiver_chain(send_node)
receivers = []
receiver = send_node.receiver

while receiver
name = receiver.send_type? ? receiver.method_name : receiver.const_name&.to_sym
receivers.unshift(name)
receiver = receiver.receiver
end

receivers
end
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/lint/debugger_spec.rb
Expand Up @@ -27,6 +27,39 @@
RUBY
end
end

context 'with a method chain' do
let(:cop_config) { { 'DebuggerMethods' => %w[debugger.foo.bar] } }

it 'registers an offense for a `debugger.foo.bar` call' do
expect_offense(<<~RUBY)
debugger.foo.bar
^^^^^^^^^^^^^^^^ Remove debugger entry point `debugger.foo.bar`.
RUBY
end
end

context 'with a const chain' do
let(:cop_config) { { 'DebuggerMethods' => %w[Foo::Bar::Baz.debug] } }

it 'registers an offense for a `Foo::Bar::Baz.debug` call' do
expect_offense(<<~RUBY)
Foo::Bar::Baz.debug
^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `Foo::Bar::Baz.debug`.
RUBY
end
end

context 'with a const chain and a method chain' do
let(:cop_config) { { 'DebuggerMethods' => %w[Foo::Bar::Baz.debug.this.code] } }

it 'registers an offense for a `Foo::Bar::Baz.debug.this.code` call' do
expect_offense(<<~RUBY)
Foo::Bar::Baz.debug.this.code
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `Foo::Bar::Baz.debug.this.code`.
RUBY
end
end
end

context 'built-in methods' do
Expand Down

0 comments on commit 2a51b79

Please sign in to comment.