class RuboCop::Cop::Layout::EmptyLinesAroundExceptionHandlingKeywords
end
do_something2
rescue
do_something
def foo
# bad
end
do_something4
ensure
do_something3
else
do_something2
rescue
do_something
begin
# bad
end
do_something2
rescue
do_something
def foo
# good
end
do_something4
ensure
do_something3
else
do_something2
rescue
do_something
begin
# good
@example
can be used for this purpose.
‘Style/EmptyLinesAroundBeginBody` or `Style/EmptyLinesAroundMethodBody`
beginning/end and around method definition body.
sections. This cop doesn’t check empty lines at ‘begin` body
Checks if empty lines exist around the bodies of `begin`
def check_body(body, line_of_def_or_kwbegin)
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_rescue_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
def keyword_locations(node)
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
def keyword_locations_in_ensure(node)
def keyword_locations_in_ensure(node) ensure_body, = *node [ node.loc.keyword, *keyword_locations(ensure_body) ] end
def keyword_locations_in_rescue(node)
def keyword_locations_in_rescue(node) [node.loc.else, *node.resbody_branches.map { |body| body.loc.keyword }].compact end
def last_rescue_and_end_on_same_line(body)
def last_rescue_and_end_on_same_line(body) body.rescue_type? && body.resbody_branches.last.loc.line == body.parent.loc.end.line end
def message(location, keyword)
def message(location, keyword) format(MSG, location: location, keyword: keyword) end
def on_def(node)
def on_def(node) check_body(node.body, node.loc.line) end
def on_kwbegin(node)
def on_kwbegin(node) body, = *node check_body(body, node.loc.line) end
def style
def style :no_empty_lines end