Class: Rubocop::Cop::EmptyLines

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

Constant Summary collapse

ERROR_MESSAGE =
'Use empty lines between defs.'

Instance Attribute Summary

Attributes inherited from Cop

#offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, enabled?, #has_report?, inherited, #initialize, #inspect_source

Constructor Details

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

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/empty_lines.rb', line 6

def inspect(file, source, tokens, sexp)
  each_parent_of(:def, sexp) do |parent|
    defs = parent.select { |child| child[0] == :def }
    identifier_of_first_def = defs[0][1]
    current_row_ix = identifier_of_first_def[-1][0] - 1
    # The first def doesn't need to have an empty line above it,
    # so we iterate starting at index 1.
    defs[1..-1].each { |child|
      next_row_ix = child[1][-1][0] - 1
      if source[current_row_ix..next_row_ix].grep(/^[ \t]*$/).empty?
        add_offence(:convention, next_row_ix, source[next_row_ix],
                    ERROR_MESSAGE)
      end
      current_row_ix = next_row_ix
    }
  end
end