class RuboCop::Cop::Layout::ClosingParenthesisIndentation

This cops checks the indentation of hanging closing parentheses in method calls, method definitions, and grouped expressions. A hanging closing parenthesis means `)` preceded by a line break.

@example

# good: when x is on its own line, indent this way
func(
  x,
  y
)

# good: when x follows opening parenthesis, align parentheses
a = b * (x +
         y
        )

# bad
def func(
  x,
  y
  )

Constants

MSG_ALIGN
MSG_INDENT

Public Instance Methods

on_begin(node) click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 39
def on_begin(node)
  check(node, node.children)
end
on_def(node) click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 43
def on_def(node)
  check(node.arguments, node.arguments)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def
on_send(node) click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 35
def on_send(node)
  check(node, node.arguments)
end

Private Instance Methods

check(node, elements) click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 50
def check(node, elements)
  right_paren = node.loc.end

  return unless right_paren && begins_its_line?(right_paren)

  correct_column = expected_column(node, elements)
  @column_delta = correct_column - right_paren.column

  return if @column_delta.zero?

  left_paren = node.loc.begin
  msg = correct_column == left_paren.column ? MSG_ALIGN : MSG_INDENT

  add_offense(right_paren, location: right_paren, message: msg)
end
expected_column(node, elements) click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 66
def expected_column(node, elements)
  left_paren = node.loc.begin

  if node.send_type? && fixed_parameter_indentation? ||
     line_break_after_left_paren?(left_paren, elements)
    left_paren.source_line =~ /\S/
  else
    left_paren.column
  end
end
fixed_parameter_indentation?() click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 77
def fixed_parameter_indentation?
  config.for_cop('Layout/AlignParameters')['EnforcedStyle'] ==
    'with_fixed_indentation'
end
line_break_after_left_paren?(left_paren, elements) click to toggle source
# File lib/rubocop/cop/layout/closing_parenthesis_indentation.rb, line 82
def line_break_after_left_paren?(left_paren, elements)
  elements.first && elements.first.loc.line > left_paren.line
end