class RuboCop::Cop::Layout::EmptyLines

This cop checks for two or more consecutive blank lines.

@example

# bad - It has two empty lines.
some_method
# one empty line
# two empty lines
some_method

# good
some_method
# one empty line
some_method

Constants

LINE_OFFSET
MSG

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubocop/cop/layout/empty_lines.rb, line 42
def autocorrect(range)
  ->(corrector) { corrector.remove(range) }
end
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/layout/empty_lines.rb, line 29
def investigate(processed_source)
  return if processed_source.tokens.empty?

  lines = Set.new
  processed_source.each_token do |token|
    lines << token.line
  end

  each_extra_empty_line(lines.sort) do |range|
    add_offense(range, location: range)
  end
end

Private Instance Methods

each_extra_empty_line(lines) { |source_range(buffer, line, 0)| ... } click to toggle source
# File lib/rubocop/cop/layout/empty_lines.rb, line 48
def each_extra_empty_line(lines)
  prev_line = 1

  lines.each do |cur_line|
    if exceeds_line_offset?(cur_line - prev_line)
      # we need to be wary of comments since they
      # don't show up in the tokens
      ((prev_line + 1)...cur_line).each do |line|
        next unless previous_and_current_lines_empty?(line)

        yield source_range(processed_source.buffer, line, 0)
      end
    end

    prev_line = cur_line
  end
end
exceeds_line_offset?(line_diff) click to toggle source
# File lib/rubocop/cop/layout/empty_lines.rb, line 66
def exceeds_line_offset?(line_diff)
  line_diff > LINE_OFFSET
end
previous_and_current_lines_empty?(line) click to toggle source
# File lib/rubocop/cop/layout/empty_lines.rb, line 70
def previous_and_current_lines_empty?(line)
  processed_source[line - 2].empty? && processed_source[line - 1].empty?
end