Method: RuboCop::Cop::Style::SafeNavigation#on_and

Defined in:
lib/rubocop/cop/style/safe_navigation.rb

#on_and(node) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

[View source]

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rubocop/cop/style/safe_navigation.rb', line 157

def on_and(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  collect_and_clauses(node).each do |(lhs, lhs_operator_range), (rhs, _rhs_operator_range)|
    lhs_not_nil_check = not_nil_check?(lhs)
    lhs_receiver = lhs_not_nil_check || lhs
    rhs_receiver = find_matching_receiver_invocation(strip_begin(rhs), lhs_receiver)

    next if !cop_config['ConvertCodeThatCanStartToReturnNil'] && lhs_not_nil_check
    next unless offending_node?(node, lhs_receiver, rhs, rhs_receiver)

    # Since we are evaluating every clause in potentially a complex chain of `and` nodes,
    # we need to ensure that there isn't an object check happening
    lhs_method_chain = find_method_chain(lhs_receiver)
    next unless lhs_method_chain == lhs_receiver || lhs_not_nil_check

    report_offense(
      node,
      rhs, rhs_receiver,
      range_with_surrounding_space(range: lhs.source_range, side: :right),
      range_with_surrounding_space(range: lhs_operator_range, side: :right),
      offense_range: range_between(lhs.source_range.begin_pos, rhs.source_range.end_pos)
    )
  end
end