Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Layout/EmptyLinesAroundExceptionHandlingKeywords #10193

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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