Class: Rubocop::Cop::EmptyLines

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/empty_lines.rb

Constant Summary collapse

MSG =
'Extra blank line detected.'
LINE_OFFSET =
2

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #name, #on_comment

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#inspect(source, tokens, ast, comments) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/empty_lines.rb', line 9

def inspect(source, tokens, ast, comments)
  return if tokens.empty?

  prev_line = 1

  tokens.each do |token|
    cur_line = token.pos.line
    line_diff = cur_line - prev_line

    if line_diff > LINE_OFFSET
      # we need to be wary of comments since they
      # don't show up in the tokens
      ((prev_line + 1)...cur_line).each do |line|
        # we check if the prev and current lines are empty
        if source[line - 2].empty? && source[line - 1].empty?
          add_offence(:convention, line, MSG)
        end
      end
    end

    prev_line = cur_line
  end
end