Class: Rubocop::Cop::EmptyLineBetweenDefs

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

Constant Summary collapse

ERROR_MESSAGE =
'Use empty lines between defs.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

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

Constructor Details

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

Instance Method Details

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



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

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].lineno - 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 do |child|
      next_row_ix = child[1][-1].lineno - 1
      if source[current_row_ix..next_row_ix].grep(/^[ \t]*$/).empty?
        add_offence(:convention, next_row_ix + 1, ERROR_MESSAGE)
      end
      current_row_ix = next_row_ix
    end
  end
end