/
debugger.rb
77 lines (68 loc) · 1.78 KB
/
debugger.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for calls to debugger or pry.
#
# @example
#
# # bad (ok during development)
#
# # using pry
# def some_method
# binding.pry
# do_something
# end
#
# @example
#
# # bad (ok during development)
#
# # using byebug
# def some_method
# byebug
# do_something
# end
#
# @example
#
# # good
#
# def some_method
# do_something
# end
class Debugger < Cop
MSG = 'Remove debugger entry point `%<source>s`.'
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
:save_screenshot} ...)}
PATTERN
def_node_matcher :binding_irb_call?, <<~PATTERN
(send (send {#kernel? nil?} :binding) :irb ...)
PATTERN
def on_send(node)
return unless debugger_call?(node) || binding_irb?(node)
add_offense(node)
end
private
def message(node)
format(MSG, source: node.source)
end
def binding_irb?(node)
target_ruby_version >= 2.4 && binding_irb_call?(node)
end
end
end
end
end