Module: RuboCop::Cop::MultilineExpressionIndentation

Included in:
Style::MultilineMethodCallIndentation, Style::MultilineOperationIndentation
Defined in:
lib/rubocop/cop/mixin/multiline_expression_indentation.rb

Overview

Common functionality for checking multiline method calls and binary operations.

Instance Method Summary collapse

Instance Method Details

#argument_in_method_call(node) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 111

def argument_in_method_call(node)
  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?

    _, method_name, *args = *a
    next if assignment_call?(method_name)
    args.any? { |arg| within_node?(node, arg) }
  end
end

#assignment_call?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 142

def assignment_call?(method_name)
  method_name == :[]= || method_name.to_s =~ /^\w.*=$/
end

#assignment_rhs(node) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 151

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 _receiver, _method_name, *_args, rhs = *node
  else               _lhs, rhs = *node
  end
  rhs
end

#check(range, node, lhs, rhs) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 62

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

#correct_indentation(node) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 52

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('Style/IndentationWidth')['Width']
  else
    configured_indentation_width
  end
end

#grouped_expression?(node) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 168

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

#incorrect_style_detected(range, node, lhs, rhs) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 70

def incorrect_style_detected(range, node, lhs, rhs)
  add_offense(range, range, message(node, lhs, rhs)) do
    if offending_range(node, lhs, rhs, alternative_style)
      unrecognized_style_detected
    else
      opposite_style_detected
    end
  end
end

#indentation(node) ⇒ Object



80
81
82
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 80

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

#inside_arg_list_parentheses?(node, ancestor) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 172

def inside_arg_list_parentheses?(node, ancestor)
  a = ancestor.loc
  return false unless ancestor.type == :send && a.begin &&
                      a.begin.is?('(')
  n = node.source_range
  n.begin_pos > a.begin.begin_pos && n.end_pos < a.end.end_pos
end

#kw_node_with_special_indentation(node) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 97

def kw_node_with_special_indentation(node)
  node.each_ancestor.find do |a|
    next unless a.loc.respond_to?(:keyword)

    case a.type
    when :if, :while, :until then expression, = *a
    when :for                then _, expression, = *a
    when :return             then expression, = *a
    end

    within_node?(node, expression) if expression
  end
end

#left_hand_side(lhs) ⇒ Object

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


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

def left_hand_side(lhs)
  lhs = lhs.parent while lhs.parent && lhs.parent.send_type?
  lhs
end

#not_for_this_cop?(node) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 161

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

#on_send(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 11

def on_send(node)
  return unless relevant_node?(node)

  receiver, method_name, *_args = *node
  return unless receiver
  return if method_name == :[] # Don't check parameters inside [].

  lhs = left_hand_side(receiver)
  rhs = right_hand_side(node)
  range = offending_range(node, lhs, rhs, style)
  check(range, node, lhs, rhs)
end

#operation_description(node, rhs) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 84

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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 123

def part_of_assignment_rhs(node, candidate)
  node.each_ancestor.find do |a|
    case a.type
    when :if, :while, :until, :for, :return, :array
      break # other kinds of alignment
    when :block
      break if part_of_block_body?(candidate, a)
    when :send
      _receiver, method_name, *args = *a

      # The []= operator and setters (a.b = c) are parsed as :send nodes.
      assignment_call?(method_name) &&
        (!candidate || within_node?(candidate, args.last))
    when *Util::ASGN_NODES
      !candidate || within_node?(candidate, assignment_rhs(a))
    end
  end
end

#part_of_block_body?(candidate, node) ⇒ Boolean

Returns:

  • (Boolean)


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

def part_of_block_body?(candidate, node)
  _method, _args, body = *node
  body && within_node?(candidate, body)
end

#right_hand_side(send_node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 34

def right_hand_side(send_node)
  _, method_name, *args = *send_node
  if operator?(method_name) && args.any?
    args.first.source_range # not used for method calls
  else
    dot = send_node.loc.dot
    selector = send_node.loc.selector
    if dot && selector && dot.line == selector.line
      dot.join(selector)
    elsif selector
      selector
    elsif dot.line == send_node.loc.begin.line
      # lambda.(args)
      dot.join(send_node.loc.begin)
    end
  end
end