Skip to content

Commit

Permalink
Make Lint/Debugger configurable in terms of debugger methods (#8929)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfbmina committed Nov 6, 2020
1 parent daf4234 commit cf96730
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
16 changes: 16 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,22 @@ Lint/Debugger:
Enabled: true
VersionAdded: '0.14'
VersionChanged: '0.49'
DebuggerReceivers:
- binding
- Kernel
- Pry
DebuggerMethods:
- debugger
- byebug
- remote_byebug
- pry
- remote_pry
- pry_remote
- console
- rescue
- save_and_open_page
- save_and_open_screenshot
- irb

Lint/DeprecatedClassMethods:
Description: 'Check for deprecated class method calls.'
Expand Down
15 changes: 15 additions & 0 deletions docs/modules/ROOT/pages/cops_lint.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ Login
|===

This cop checks for calls to debugger or pry.
The cop can be configured to define which methods and receivers must be fixed.

=== Examples

Expand Down Expand Up @@ -556,6 +557,20 @@ def some_method
end
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| DebuggerReceivers
| `binding`, `Kernel`, `Pry`
| Array

| DebuggerMethods
| `debugger`, `byebug`, `remote_byebug`, `pry`, `remote_pry`, `pry_remote`, `console`, `rescue`, `save_and_open_page`, `save_and_open_screenshot`, `irb`
| Array
|===

== Lint/DeprecatedClassMethods

|===
Expand Down
44 changes: 17 additions & 27 deletions lib/rubocop/cop/lint/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module RuboCop
module Cop
module Lint
# This cop checks for calls to debugger or pry.
# The cop can be configured to define which methods and receivers must be fixed.
#
# @example
#
Expand Down Expand Up @@ -35,33 +36,11 @@ module Lint
class Debugger < Base
MSG = 'Remove debugger entry point `%<source>s`.'

RESTRICT_ON_SEND = %i[
debugger byebug remote_byebug pry remote_pry pry_remote console rescue
save_and_open_page save_and_open_screenshot irb
].freeze

def_node_matcher :kernel?, <<~PATTERN
{
(const nil? :Kernel)
(const (cbase) :Kernel)
}
PATTERN

def_node_matcher :debugger_call?, <<~PATTERN
{(send {nil? #kernel?} {:debugger :byebug :remote_byebug} ...)
(send (send {#kernel? nil?} :binding)
{:pry :remote_pry :pry_remote :console} ...)
(send (const {nil? (cbase)} :Pry) :rescue ...)
(send nil? {:save_and_open_page
:save_and_open_screenshot} ...)}
PATTERN

def_node_matcher :binding_irb_call?, <<~PATTERN
(send (send {#kernel? nil?} :binding) :irb ...)
PATTERN
RESTRICT_ON_SEND = [].freeze

def on_send(node)
return unless debugger_call?(node) || binding_irb?(node)
return unless debugger_method?(node.method_name)
return if !node.receiver.nil? && !debugger_receiver?(node)

add_offense(node)
end
Expand All @@ -72,8 +51,19 @@ def message(node)
format(MSG, source: node.source)
end

def binding_irb?(node)
binding_irb_call?(node)
def debugger_method?(name)
cop_config.fetch('DebuggerMethods', []).include?(name.to_s)
end

def debugger_receiver?(node)
receiver = case node.receiver
when RuboCop::AST::SendNode
node.receiver.method_name
when RuboCop::AST::ConstNode
node.receiver.const_name
end

cop_config.fetch('DebuggerReceivers', []).include?(receiver.to_s)
end
end
end
Expand Down
24 changes: 23 additions & 1 deletion spec/rubocop/cop/lint/debugger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::Debugger, :config do
context 'with allowed methods' do
let(:cop_config) do
{ 'DebuggerMethods' => %w[custom_debugger] }
end

it 'does not reports an offense for a byebug call' do
expect_no_offenses(<<~RUBY)
byebug
RUBY
end

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

it 'reports an offense for a debugger call' do
expect_offense(<<~RUBY)
debugger
Expand Down Expand Up @@ -143,7 +162,10 @@
end

it 'does not report an offense for save_and_open_page with Kernel' do
expect_no_offenses('Kernel.save_and_open_page')
expect_offense(<<~RUBY)
Kernel.save_and_open_page
^^^^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `Kernel.save_and_open_page`.
RUBY
end

%w[debugger byebug console pry remote_pry pry_remote irb save_and_open_page
Expand Down

0 comments on commit cf96730

Please sign in to comment.