Skip to content

Commit

Permalink
Checks binding.irb call by Lint/Debugger cop
Browse files Browse the repository at this point in the history
From Ruby 2.4, `binding.irb` will be added.
This method is like `binding.pry`.

For example

```sh
$ cat test.rb
require 'irb'

foo = 1
binding.irb

$ ruby test.rb
irb(main):001:0> foo
=> 1
irb(main):002:0>
```

This PR adds detection of `binding.irb` as a debugger.

For example

```sh
$ rubocop
Inspecting 1 file
W

Offenses:

test.rb:4:1: W: Remove debugger entry point binding.irb.
binding.irb
^^^^^^^^^^^

1 file inspected, 1 offenses detected
```

The check works only Ruby 2.4 or higher.
  • Loading branch information
pocke authored and bbatsov committed Nov 28, 2016
1 parent 127b18c commit 62d685b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

* The offense range for `Performance/FlatMap` now includes any parameters that are passed to `flatten`. ([@rrosenblum][])
* [#1747](https://github.com/bbatsov/rubocop/issues/1747): Update `Style/SpecialGlobalVars` messages with a reminder to `require 'English'`. ([@ivanovaleksey][])
* Checks `binding.irb` call by `Lint/Debugger` cop. ([@pocke][])

### Bug fixes

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/lint/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class Debugger < Cop
:save_screenshot} ...)}
END

def_node_matcher :binding_irb_call?, <<-END
(send (send nil :binding) :irb ...)
END

def_node_matcher :pry_rescue?, '(send (const nil :Pry) :rescue ...)'

def on_send(node)
return unless debugger_call?(node)
return unless debugger_call?(node) || binding_irb?(node)
add_offense(node, :expression, format(MSG, node.source))
end

Expand All @@ -38,6 +42,10 @@ def autocorrect(node)
end
end
end

def binding_irb?(node)
target_ruby_version >= 2.4 && binding_irb_call?(node)
end
end
end
end
Expand Down
15 changes: 12 additions & 3 deletions spec/rubocop/cop/lint/debugger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require 'spec_helper'

describe RuboCop::Cop::Lint::Debugger do
subject(:cop) { described_class.new }
describe RuboCop::Cop::Lint::Debugger, :config do
subject(:cop) { described_class.new(config) }

include_examples 'debugger', 'debugger', 'debugger'
include_examples 'debugger', 'byebug', 'byebug'
Expand All @@ -26,7 +26,7 @@
'save_screenshot foo']
include_examples 'non-debugger', 'a non-pry binding', 'binding.pirate'

ALL_COMMANDS = %w(debugger byebug pry remote_pry pry_remote
ALL_COMMANDS = %w(debugger byebug pry remote_pry pry_remote irb
save_and_open_page save_and_open_screenshot
save_screenshot).freeze

Expand All @@ -51,4 +51,13 @@
' puts 1',
'end'].join("\n"))
end

context 'target_ruby_version >= 2.4', :ruby24 do
include_examples 'debugger', 'irb binding', 'binding.irb'

ALL_COMMANDS.each do |src|
include_examples 'non-debugger', "a #{src} in comments", "# #{src}"
include_examples 'non-debugger', "a #{src} method", "code.#{src}"
end
end
end

0 comments on commit 62d685b

Please sign in to comment.