class RuboCop::Cop::Layout::EmptyLinesAroundExceptionHandlingKeywords

Checks if empty lines exist around the bodies of ‘begin` sections. This cop doesn’t check empty lines at ‘begin` body beginning/end and around method definition body. `Style/EmptyLinesAroundBeginBody` or `Style/EmptyLinesAroundMethodBody` can be used for this purpose.

@example

# good

begin
  do_something
rescue
  do_something2
else
  do_something3
ensure
  do_something4
end

# good

def foo
  do_something
rescue
  do_something2
end

# bad

begin
  do_something

rescue

  do_something2

else

  do_something3

ensure

  do_something4
end

# bad

def foo
  do_something

rescue

  do_something2
end

Constants

MSG

Public Instance Methods

on_block(node)
Alias for: on_def
on_def(node) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 67
def on_def(node)
  check_body(node.body, node.loc.line)
end
Also aliased as: on_defs, on_block, on_numblock
on_defs(node)
Alias for: on_def
on_kwbegin(node) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 74
def on_kwbegin(node)
  body, = *node
  check_body(body, node.loc.line)
end
on_numblock(node)
Alias for: on_def

Private Instance Methods

check_body(body, line_of_def_or_kwbegin) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 81
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 || last_body_and_end_on_same_line?(body)

    keyword = loc.source
    # below the keyword
    check_line(style, line, message('after', keyword), &:empty?)
    # above the keyword
    check_line(style, line - 2, message('before', keyword), &:empty?)
  end
end
keyword_locations(node) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 113
def keyword_locations(node)
  return [] unless node

  case node.type
  when :rescue
    keyword_locations_in_rescue(node)
  when :ensure
    keyword_locations_in_ensure(node)
  else
    []
  end
end
keyword_locations_in_ensure(node) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 130
def keyword_locations_in_ensure(node)
  ensure_body, = *node
  [
    node.loc.keyword,
    *keyword_locations(ensure_body)
  ]
end
keyword_locations_in_rescue(node) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 126
def keyword_locations_in_rescue(node)
  [node.loc.else, *node.resbody_branches.map { |body| body.loc.keyword }].compact
end
last_body_and_end_on_same_line?(body) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 96
def last_body_and_end_on_same_line?(body)
  end_keyword_line = body.parent.loc.end.line
  return body.loc.last_line == end_keyword_line unless body.rescue_type?

  last_body_line = body.else? ? body.loc.else.line : body.resbody_branches.last.loc.line

  last_body_line == end_keyword_line
end
message(location, keyword) click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 105
def message(location, keyword)
  format(MSG, location: location, keyword: keyword)
end
style() click to toggle source
# File lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb, line 109
def style
  :no_empty_lines
end