class RubbyCop::Cop::Layout::MultilineOperationIndentation

This cop checks the indentation of the right hand side operand in binary operations that span more than one line.

@example

# bad
if a +
b
  something
end

Public Instance Methods

on_and(node) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 20
def on_and(node)
  check_and_or(node)
end
on_or(node) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 24
def on_or(node)
  check_and_or(node)
end
validate_config() click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 28
def validate_config
  return unless style == :aligned && cop_config['IndentationWidth']

  raise ValidationError, 'The `Layout/MultilineOperationIndentation`' \
                        ' cop only accepts an `IndentationWidth` ' \
                        'configuration parameter when ' \
                        '`EnforcedStyle` is `indented`.'
end

Private Instance Methods

check_and_or(node) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 43
def check_and_or(node)
  lhs, rhs = *node
  range = offending_range(node, lhs, rhs.source_range, style)
  check(range, node, lhs, rhs.source_range)
end
message(node, lhs, rhs) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 76
def message(node, lhs, rhs)
  what = operation_description(node, rhs)
  if should_align?(node, rhs, style)
    "Align the operands of #{what} spanning multiple lines."
  else
    used_indentation = rhs.column - indentation(lhs)
    "Use #{correct_indentation(node)} (not #{used_indentation}) " \
      "spaces for indenting #{what} spanning multiple lines."
  end
end
offending_range(node, lhs, rhs, given_style) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 49
def offending_range(node, lhs, rhs, given_style)
  return false unless begins_its_line?(rhs)
  return false if lhs.loc.line == rhs.line # Needed for unary op.
  return false if not_for_this_cop?(node)

  correct_column = if should_align?(node, rhs, given_style)
                     node.loc.column
                   else
                     indentation(lhs) + correct_indentation(node)
                   end
  @column_delta = correct_column - rhs.column
  rhs if @column_delta.nonzero?
end
relevant_node?(node) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 39
def relevant_node?(node)
  !node.loc.dot # Don't check method calls with dot operator.
end
should_align?(node, rhs, given_style) click to toggle source
# File lib/rubbycop/cop/layout/multiline_operation_indentation.rb, line 63
def should_align?(node, rhs, given_style)
  assignment_node = part_of_assignment_rhs(node, rhs)
  if assignment_node
    assignment_rhs = CheckAssignment.extract_rhs(assignment_node)
    return true if begins_its_line?(assignment_rhs.source_range)
  end

  given_style == :aligned &&
    (kw_node_with_special_indentation(node) ||
     assignment_node ||
     argument_in_method_call(node, :with_or_without_parentheses))
end