Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10859] Fix Lint/Debugger to be able to handle method chains correctly #10869

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_fix_lintdebugger_to_be_able_to_handle.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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