Class: Rubocop::Cop::Style::EmptyLines

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

Overview

This cops checks for two or more consecutive blank lines.

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

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

Instance Method Details

#investigate(processed_source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/style/empty_lines.rb', line 11

def investigate(processed_source)
  return if processed_source.tokens.empty?

  prev_line = 1

  processed_source.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 processed_source[line - 2].empty? &&
            processed_source[line - 1].empty?
          range = source_range(processed_source.buffer,
                               processed_source[0...(line - 1)],
                               0,
                               1)
          add_offence(:convention, range, MSG)
        end
      end
    end

    prev_line = cur_line
  end
end