class RuboCop::Cop::Style::AlignParameters

Here we check if the parameters on a multi-line method call or definition are aligned.

Public Instance Methods

message(node) click to toggle source
# File lib/rubocop/cop/style/align_parameters.rb, line 27
def message(node)
  type = node && node.parent.send_type? ? 'call' : 'definition'
  "Align the parameters of a method #{type} if they span "            'more than one line.'
end
on_method_def(node, _method_name, args, _body) click to toggle source
# File lib/rubocop/cop/style/align_parameters.rb, line 21
def on_method_def(node, _method_name, args, _body)
  args = args.children
  return if args.size < 2
  check_alignment(args, base_column(node, args))
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/align_parameters.rb, line 12
def on_send(node)
  _receiver, method, *args = *node

  return if method == :[]=
  return if args.size < 2

  check_alignment(args, base_column(node, args))
end

Private Instance Methods

base_column(node, args) click to toggle source
# File lib/rubocop/cop/style/align_parameters.rb, line 39
def base_column(node, args)
  if fixed_indentation?
    lineno = target_method_lineno(node)
    line = node.source_range.source_buffer.source_line(lineno)
    indentation_of_line = /\S.*/.match(line).begin(0)
    indentation_of_line + configured_indentation_width
  else
    display_column(args.first.source_range)
  end
end
fixed_indentation?() click to toggle source
# File lib/rubocop/cop/style/align_parameters.rb, line 35
def fixed_indentation?
  cop_config['EnforcedStyle'] == 'with_fixed_indentation'
end
target_method_lineno(node) click to toggle source
# File lib/rubocop/cop/style/align_parameters.rb, line 50
def target_method_lineno(node)
  if node.def_type? || node.defs_type?
    node.loc.keyword.line
  elsif node.loc.selector
    node.loc.selector.line
  else
    # l.(1) has no selector, so we use the opening parenthesis instead
    node.loc.begin.line
  end
end