module RubbyCop::Cop::MultilineExpressionIndentation

Common functionality for checking multiline method calls and binary operations.

Public Instance Methods

argument_in_method_call(node, kind) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 108
def argument_in_method_call(node, kind)
  node.each_ancestor(:send, :block).find do |a|
    # If the node is inside a block, it makes no difference if that block
    # is an argument in a method call. It doesn't count.
    break false if a.block_type?

    next if a.setter_method?

    a.arguments.any? do |arg|
      within_node?(node, arg) && (kind == :with_or_without_parentheses ||
                                  kind == :with_parentheses &&
                                  parentheses?(node.parent))
    end
  end
end
assignment_rhs(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 154
def assignment_rhs(node)
  case node.type
  when :casgn   then _scope, _lhs, rhs = *node
  when :op_asgn then _lhs, _op, rhs = *node
  when :send    then rhs = node.last_argument
  else               _lhs, rhs = *node
  end
  rhs
end
check(range, node, lhs, rhs) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 58
def check(range, node, lhs, rhs)
  if range
    incorrect_style_detected(range, node, lhs, rhs)
  else
    correct_style_detected
  end
end
correct_indentation(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 48
def correct_indentation(node)
  if kw_node_with_special_indentation(node)
    # This cop could have its own IndentationWidth configuration
    configured_indentation_width +
      @config.for_cop('Layout/IndentationWidth')['Width']
  else
    configured_indentation_width
  end
end
grouped_expression?(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 171
def grouped_expression?(node)
  node.begin_type? && node.loc.respond_to?(:begin) && node.loc.begin
end
incorrect_style_detected(range, node, lhs, rhs) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 66
def incorrect_style_detected(range, node, lhs, rhs)
  add_offense(range, range, message(node, lhs, rhs)) do
    if supported_styles.size > 2 ||
       offending_range(node, lhs, rhs, alternative_style)
      unrecognized_style_detected
    else
      opposite_style_detected
    end
  end
end
indentation(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 77
def indentation(node)
  node.source_range.source_line =~ /\S/
end
inside_arg_list_parentheses?(node, ancestor) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 175
def inside_arg_list_parentheses?(node, ancestor)
  return false unless ancestor.send_type? && ancestor.parenthesized?

  node.source_range.begin_pos > ancestor.loc.begin.begin_pos &&
    node.source_range.end_pos < ancestor.loc.end.end_pos
end
kw_node_with_special_indentation(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 94
def kw_node_with_special_indentation(node)
  node.each_ancestor.find do |a|
    next unless a.loc.respond_to?(:keyword)

    case a.type
    when :for                  then _, expression, = *a
    when :return               then expression, = *a
    when *Util::MODIFIER_NODES then expression, = *a
    end

    within_node?(node, expression) if expression
  end
end
left_hand_side(lhs) click to toggle source

In a chain of method calls, we regard the top send node as the base for indentation of all lines following the first. For example: a.

b c { block }.            <-- b is indented relative to a
d                         <-- d is indented relative to a
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 23
def left_hand_side(lhs)
  lhs = lhs.parent while lhs.parent && lhs.parent.send_type?
  lhs
end
not_for_this_cop?(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 164
def not_for_this_cop?(node)
  node.ancestors.any? do |ancestor|
    grouped_expression?(ancestor) ||
      inside_arg_list_parentheses?(node, ancestor)
  end
end
on_send(node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 8
def on_send(node)
  return if !node.receiver || node.method?(:[])
  return unless relevant_node?(node)

  lhs = left_hand_side(node.receiver)
  rhs = right_hand_side(node)
  range = offending_range(node, lhs, rhs, style)
  check(range, node, lhs, rhs)
end
operation_description(node, rhs) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 81
def operation_description(node, rhs)
  ancestor = kw_node_with_special_indentation(node)
  if ancestor
    kw = ancestor.loc.keyword.source
    kind = kw == 'for' ? 'collection' : 'condition'
    article = kw =~ /^[iu]/ ? 'an' : 'a'
    "a #{kind} in #{article} `#{kw}` statement"
  else
    'an expression' +
      (part_of_assignment_rhs(node, rhs) ? ' in an assignment' : '')
  end
end
part_of_assignment_rhs(node, candidate) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 124
def part_of_assignment_rhs(node, candidate)
  node.each_ancestor.find do |a|
    case a.type
    when :if, :while, :until, :for, :return, :array, :kwbegin
      break # other kinds of alignment
    when :block
      break if part_of_block_body?(candidate, a)
    when :send
      valid_method_rhs_candidate?(candidate, a)
    when *Util::ASGN_NODES
      valid_rhs_candidate?(candidate, assignment_rhs(a))
    end
  end
end
part_of_block_body?(candidate, node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 149
def part_of_block_body?(candidate, node)
  _method, _args, body = *node
  body && within_node?(candidate, body)
end
regular_method_right_hand_side(send_node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 36
def regular_method_right_hand_side(send_node)
  dot = send_node.loc.dot
  selector = send_node.loc.selector
  if send_node.dot? && selector && dot.line == selector.line
    dot.join(selector)
  elsif selector
    selector
  elsif send_node.implicit_call?
    dot.join(send_node.loc.begin)
  end
end
right_hand_side(send_node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 28
def right_hand_side(send_node)
  if send_node.operator_method? && send_node.arguments?
    send_node.first_argument.source_range # not used for method calls
  else
    regular_method_right_hand_side(send_node)
  end
end
valid_method_rhs_candidate?(candidate, node) click to toggle source

The []= operator and setters (a.b = c) are parsed as :send nodes.

# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 140
def valid_method_rhs_candidate?(candidate, node)
  node.setter_method? &&
    valid_rhs_candidate?(candidate, node.last_argument)
end
valid_rhs_candidate?(candidate, node) click to toggle source
# File lib/rubbycop/cop/mixin/multiline_expression_indentation.rb, line 145
def valid_rhs_candidate?(candidate, node)
  !candidate || within_node?(candidate, node)
end