Skip to content

Commit

Permalink
Merge pull request #10193 from koic/fix_an_error_for_empty_lines_arou…
Browse files Browse the repository at this point in the history
…nd_exception_handling_keywords

Fix an error for `Layout/EmptyLinesAroundExceptionHandlingKeywords`
  • Loading branch information
koic committed Oct 17, 2021
2 parents 41f2632 + 95262d0 commit 1894717
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 1894717

Please sign in to comment.