Class: Rubocop::Cop::AlignParameters

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

Constant Summary collapse

ERROR_MESSAGE =
'Align the parameters of a method call if they span ' +
'more than one line.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #offences

Instance Method Summary collapse

Methods inherited from Cop

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

Constructor Details

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

Instance Method Details

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



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

def inspect(file, source, tokens, sexp)
  @file = file
  @tokens = tokens
  @token_indexes = {}
  @tokens.each_with_index { |t, ix| @token_indexes[t.pos] = ix }

  each(:method_add_arg, sexp) do |method_add_arg|
    args = get_args(method_add_arg) or next
    first_arg, rest_of_args = divide_args(args)
    @first_lparen_ix = get_lparen_ix(method_add_arg)
    pos_of_1st_arg = position_of(first_arg) or next # Give up.
    rest_of_args.each do |arg|
      pos = position_of(arg) or next # Give up if no position found.
      if pos.lineno != pos_of_1st_arg.lineno
        if pos.column != pos_of_1st_arg.column
          add_offence(:convention, pos.lineno, ERROR_MESSAGE)
        end
      end
    end
  end
end