module RuboCop::Cop::Style::EmptyLinesAroundBody

Common functionality for checking if presence/absence of empty lines around some kind of body matches the configuration.

Constants

MSG_EXTRA
MSG_MISSING

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubocop/cop/mixin/empty_lines_around_body.rb, line 14
def autocorrect(range)
  lambda do |corrector|
    case style
    when :no_empty_lines then corrector.remove(range)
    when :empty_lines    then corrector.insert_before(range, "\n")
    end
  end
end

Private Instance Methods

check(node, body) click to toggle source
# File lib/rubocop/cop/mixin/empty_lines_around_body.rb, line 25
def check(node, body)
  # When style is `empty_lines`, if the body is empty, we don't enforce
  # the presence OR absence of an empty line
  # But if style is `no_empty_lines`, there must not be an empty line
  return unless body || style == :no_empty_lines

  start_line = node.loc.keyword.line
  end_line = node.loc.end.line
  return if start_line == end_line

  check_source(start_line, end_line)
end
check_both(start_line, end_line, msg, &block) click to toggle source
# File lib/rubocop/cop/mixin/empty_lines_around_body.rb, line 49
def check_both(start_line, end_line, msg, &block)
  kind = self.class::KIND
  check_line(start_line, format(msg, kind, 'beginning'), &block)
  check_line(end_line - 2, format(msg, kind, 'end'), &block)
end
check_line(line, msg) { |lines| ... } click to toggle source
# File lib/rubocop/cop/mixin/empty_lines_around_body.rb, line 55
def check_line(line, msg)
  return unless yield processed_source.lines[line]

  offset = style == :empty_lines && msg.include?('end.') ? 2 : 1
  range = source_range(processed_source.buffer, line + offset, 0)
  add_offense(range, range, msg)
end
check_source(start_line, end_line) click to toggle source
# File lib/rubocop/cop/mixin/empty_lines_around_body.rb, line 38
def check_source(start_line, end_line)
  case style
  when :no_empty_lines
    check_both(start_line, end_line, MSG_EXTRA, &:empty?)
  when :empty_lines
    check_both(start_line, end_line, MSG_MISSING) do |line|
      !line.empty?
    end
  end
end