Skip to content

Commit

Permalink
Fix an error for Layout/EmptyLinesAroundExceptionHandlingKeywords
Browse files Browse the repository at this point in the history
This PR fixes the following error for `Layout/EmptyLinesAroundExceptionHandlingKeywords`
when `begin` and `rescue` are on the same line.

```console
% echo 'begin; foo; rescue => e; end' | bundle exec rubocop --stdin
  example.rb -d
For /Users/koic/src/github.com/rubocop/rubocop: configuration from
  /Users/koic/src/github.com/rubocop/rubocop/.rubocop.yml
configuration from
  /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-performance-1.11.5/config/default.yml
configuration from
  /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-performance-1.11.5/config/default.yml
Default configuration from
  /Users/koic/src/github.com/rubocop/rubocop/config/default.yml
configuration from
  /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rspec-2.5.0/config/default.yml
configuration from
  /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rspec-2.5.0/config/default.yml
configuration from
  /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rake-0.6.0/config/default.yml
configuration from
  /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rake-0.6.0/config/default.yml
Inheriting configuration from
  /Users/koic/src/github.com/rubocop/rubocop/.rubocop_todo.yml
Inspecting 1 file
Scanning /Users/koic/src/github.com/rubocop/rubocop/example.rb
An error occurred while Layout/EmptyLinesAroundExceptionHandlingKeywords
  cop was inspecting
  /Users/koic/src/github.com/rubocop/rubocop/example.rb:1:0.
The range 29...30 is outside the bounds of the source
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:406:in
  `check_range_validity'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/corrector.rb:100:in
  `check_range_validity'
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:398:in
  `combine'
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:194:in
  `replace'
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:218:in
  `remove'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/correctors/empty_line_corrector.rb:13:in
  `correct'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/mixin/empty_lines_around_body.rb:104:in
  `block in check_line'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/base.rb:342:in
  `correct'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/base.rb:127:in `add_offense'
```
  • Loading branch information
koic committed Oct 17, 2021
1 parent e9c339b commit 95262d0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
@@ -0,0 +1 @@
* [#10193](https://github.com/rubocop/rubocop/pull/10193): Fix an error for `Layout/EmptyLinesAroundExceptionHandlingKeywords` when `begin` and `rescue` are on the same line. ([@koic][])
Expand Up @@ -65,21 +65,24 @@ class EmptyLinesAroundExceptionHandlingKeywords < Base
MSG = 'Extra empty line detected %<location>s the `%<keyword>s`.'

def on_def(node)
check_body(node.body)
check_body(node.body, node.loc.line)
end
alias on_defs on_def

def on_kwbegin(node)
body, = *node
check_body(body)
check_body(body, node.loc.line)
end

private

def check_body(node)
locations = keyword_locations(node)
def check_body(body, line_of_def_or_kwbegin)
locations = keyword_locations(body)

locations.each do |loc|
line = loc.line
next if line == line_of_def_or_kwbegin

keyword = loc.source
# below the keyword
check_line(style, line, message('after', keyword), &:empty?)
Expand Down
Expand Up @@ -127,6 +127,14 @@ def foo
end
RUBY

include_examples 'accepts', '`begin` and `rescue` are on the same line', <<~RUBY
begin; foo; rescue => e; end
RUBY

include_examples 'accepts', '`def` and `rescue` are on the same line', <<~RUBY
def do_something; foo; rescue => e; end
RUBY

it 'with complex begin-end - registers many offenses' do
expect_offense(<<~RUBY)
begin
Expand Down

0 comments on commit 95262d0

Please sign in to comment.