123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::MultilineExpressionIndentation

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/multiline_expression_indentation.rb

Overview

Common functionality for checking multiline method calls and binary operations.

Constant Summary

Instance Method Summary

Instance Method Details

#argument_in_method_call(node, kind) (private)

rubocop:todo Metrics/CyclomaticComplexity

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 132

def argument_in_method_call(node, kind) # rubocop:todo Metrics/CyclomaticComplexity
  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?
    next unless kind == :with_or_without_parentheses ||
                (kind == :with_parentheses && parentheses?(a))

    a.arguments.any? { |arg| within_node?(node, arg) }
  end
end

#assignment_rhs(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 188

def assignment_rhs(node)
  case node.type
  when :casgn   then _scope, _lhs, rhs = *node
  when :op_asgn then _lhs, _op, rhs = *node
  when :send, :csend then rhs = node.last_argument
  else               _lhs, rhs = *node
  end
  rhs
end

#check(range, node, lhs, rhs) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 65

def check(range, node, lhs, rhs)
  if range
    incorrect_style_detected(range, node, lhs, rhs)
  else
    correct_style_detected
  end
end

#correct_indentation(node) (private)

The correct indentation of node is usually IndentationWidth, with one exception: prefix keywords.

while foo &&  # Here, {while} is called a "prefix keyword"
    bar       # This is called "special indentation"
  baz
end

Note that postfix conditionals do not get "special indentation".

next if foo &&
  bar # normal indentation, not special
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 55

def correct_indentation(node)
  kw_node = kw_node_with_special_indentation(node)
  if kw_node && !postfix_conditional?(kw_node)
    # This cop could have its own IndentationWidth configuration
    configured_indentation_width + @config.for_cop('Layout/IndentationWidth')['Width']
  else
    configured_indentation_width
  end
end

#disqualified_rhs?(candidate, ancestor) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 160

def disqualified_rhs?(candidate, ancestor)
  UNALIGNED_RHS_TYPES.include?(ancestor.type) ||
    (ancestor.block_type? && part_of_block_body?(candidate, ancestor))
end

#grouped_expression?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 204

def grouped_expression?(node)
  node.begin_type? && node.loc.respond_to?(:begin) && node.loc.begin
end

#incorrect_style_detected(range, node, lhs, rhs) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 73

def incorrect_style_detected(range, node, lhs, rhs)
  add_offense(range, message: message(node, lhs, rhs)) do |corrector|
    autocorrect(corrector, range)

    if supported_styles.size > 2 || offending_range(node, lhs, rhs, alternative_style)
      unrecognized_style_detected
    else
      opposite_style_detected
    end
  end
end

#indentation(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 85

def indentation(node)
  node.source_range.source_line =~ /\S/
end

#indented_keyword_expression(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 122

def indented_keyword_expression(node)
  if node.for_type?
    expression = node.collection
  else
    expression, = *node
  end

  expression
end

#inside_arg_list_parentheses?(node, ancestor) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 208

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

#keyword_message_tail(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 99

def keyword_message_tail(node)
  keyword = node.loc.keyword.source
  kind    = keyword == 'for' ? 'collection' : 'condition'
  article = keyword.start_with?('i', 'u') ? 'an' : 'a'

  format(KEYWORD_MESSAGE_TAIL, kind: kind, article: article, keyword: keyword)
end

#kw_node_with_special_indentation(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 107

def kw_node_with_special_indentation(node)
  keyword_node =
    node.each_ancestor(*KEYWORD_ANCESTOR_TYPES).find do |ancestor|
      next if ancestor.if_type? && ancestor.ternary?

      within_node?(node, indented_keyword_expression(ancestor))
    end

  if keyword_node && block_given?
    yield keyword_node
  else
    keyword_node
  end
end

#left_hand_side(lhs) (private)

In a chain of method calls, we regard the top call 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

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 32

def left_hand_side(lhs)
  while lhs.parent&.call_type? && lhs.parent.loc.dot && !lhs.parent.assignment_method?
    lhs = lhs.parent
  end
  lhs
end

#not_for_this_cop?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 198

def not_for_this_cop?(node)
  node.ancestors.any? do |ancestor|
    grouped_expression?(ancestor) || inside_arg_list_parentheses?(node, ancestor)
  end
end

#on_csend(node)

Alias for #on_send.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 23

alias on_csend on_send

#on_send(node) Also known as: #on_csend

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 14

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) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 89

def operation_description(node, rhs)
  kw_node_with_special_indentation(node) do |ancestor|
    return keyword_message_tail(ancestor)
  end

  part_of_assignment_rhs(node, rhs) { |_node| return ASSIGNMENT_MESSAGE_TAIL }

  DEFAULT_MESSAGE_TAIL
end

#part_of_assignment_rhs(node, candidate) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 146

def part_of_assignment_rhs(node, candidate)
  rhs_node = node.each_ancestor.find do |ancestor|
    break if disqualified_rhs?(candidate, ancestor)

    valid_rhs?(candidate, ancestor)
  end

  if rhs_node && block_given?
    yield rhs_node
  else
    rhs_node
  end
end

#part_of_block_body?(candidate, block_node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 184

def part_of_block_body?(candidate, block_node)
  block_node.body && within_node?(candidate, block_node.body)
end

#postfix_conditional?(node) ⇒ Boolean (private)

Returns true if node is a conditional whose body and condition begin on the same line.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 217

def postfix_conditional?(node)
  node.if_type? && node.modifier_form?
end

#valid_method_rhs_candidate?(candidate, node) ⇒ Boolean (private)

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

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 176

def valid_method_rhs_candidate?(candidate, node)
  node.setter_method? && valid_rhs_candidate?(candidate, node.last_argument)
end

#valid_rhs?(candidate, ancestor) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 165

def valid_rhs?(candidate, ancestor)
  if ancestor.send_type?
    valid_method_rhs_candidate?(candidate, ancestor)
  elsif ancestor.assignment?
    valid_rhs_candidate?(candidate, assignment_rhs(ancestor))
  else
    false
  end
end

#valid_rhs_candidate?(candidate, node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 180

def valid_rhs_candidate?(candidate, node)
  !candidate || within_node?(candidate, node)
end

#within_node?(inner, outer) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 221

def within_node?(inner, outer)
  o = outer.is_a?(AST::Node) ? outer.source_range : outer
  i = inner.is_a?(AST::Node) ? inner.source_range : inner
  i.begin_pos >= o.begin_pos && i.end_pos <= o.end_pos
end