class RuboCop::Cop::Layout::AlignParameters

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

To set the alignment of the first argument, use the cop FirstParameterIndentation.

@example EnforcedStyle: with_first_parameter (default)

# good

def foo(bar,
        baz)
  123
end

def foo(
  bar,
  baz
)
  123
end

# bad

def foo(bar,
     baz)
  123
end

# bad

def foo(
  bar,
     baz)
  123
end

@example EnforcedStyle: with_fixed_indentation

# good

def foo(bar,
  baz)
  123
end

def foo(
  bar,
  baz
)
  123
end

# bad

def foo(bar,
        baz)
  123
end

# bad

def foo(
  bar,
     baz)
  123
end

Constants

ALIGN_PARAMS_MSG
FIXED_INDENT_MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/layout/align_parameters.rb, line 87
def autocorrect(node)
  AlignmentCorrector.correct(processed_source, node, column_delta)
end
on_def(node) click to toggle source
# File lib/rubocop/cop/layout/align_parameters.rb, line 80
def on_def(node)
  return if node.arguments.size < 2

  check_alignment(node.arguments, base_column(node, node.arguments))
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

base_column(node, args) click to toggle source
# File lib/rubocop/cop/layout/align_parameters.rb, line 101
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/layout/align_parameters.rb, line 97
def fixed_indentation?
  cop_config['EnforcedStyle'] == 'with_fixed_indentation'
end
message(_node) click to toggle source
# File lib/rubocop/cop/layout/align_parameters.rb, line 93
def message(_node)
  fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG
end
target_method_lineno(node) click to toggle source
# File lib/rubocop/cop/layout/align_parameters.rb, line 112
def target_method_lineno(node)
  node.loc.keyword.line
end