class RuboCop::Cop::Layout::FirstParameterIndentation

Checks the indentation of the first parameter in a method definition. Parameters after the first one are checked by Layout/ParameterAlignment, not by this cop.

For indenting the first argument of method calls, check out Layout/FirstArgumentIndentation, which supports options related to nesting that are irrelevant for method definitions.

@example

# bad
def some_method(
first_param,
second_param)
  123
end

@example EnforcedStyle: consistent (default)

# The first parameter should always be indented one step more than the
# preceding line.

# good
def some_method(
  first_param,
second_param)
  123
end

@example EnforcedStyle: align_parentheses

# The first parameter should always be indented one step more than the
# opening parenthesis.

# good
def some_method(
                 first_param,
second_param)
  123
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/layout/first_parameter_indentation.rb, line 53
def on_def(node)
  return if node.arguments.empty?
  return if node.arguments.loc.begin.nil?

  check(node)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

autocorrect(corrector, node) click to toggle source
# File lib/rubocop/cop/layout/first_parameter_indentation.rb, line 63
def autocorrect(corrector, node)
  AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
end
base_description(_) click to toggle source

Returns the description of what the correct indentation is based on.

# File lib/rubocop/cop/layout/first_parameter_indentation.rb, line 83
def base_description(_)
  if style == brace_alignment_style
    'the position of the opening parenthesis'
  else
    'the start of the line where the left parenthesis is'
  end
end
brace_alignment_style() click to toggle source
# File lib/rubocop/cop/layout/first_parameter_indentation.rb, line 67
def brace_alignment_style
  :align_parentheses
end
check(def_node) click to toggle source
# File lib/rubocop/cop/layout/first_parameter_indentation.rb, line 71
def check(def_node)
  return if ignored_node?(def_node)

  left_parenthesis = def_node.arguments.loc.begin
  first_elem = def_node.first_argument
  return unless first_elem
  return if same_line?(first_elem, left_parenthesis)

  check_first(first_elem, left_parenthesis, nil, 0)
end
message(base_description) click to toggle source
# File lib/rubocop/cop/layout/first_parameter_indentation.rb, line 91
def message(base_description)
  format(
    MSG,
    configured_indentation_width: configured_indentation_width,
    base_description: base_description
  )
end