lib/rubocop/cop/style/invertible_unless_condition.rb in rubocop-1.60.2 vs lib/rubocop/cop/style/invertible_unless_condition.rb in rubocop-1.61.0
- old
+ new
@@ -30,16 +30,18 @@
# # bad (simple condition)
# foo unless !bar
# foo unless x != y
# foo unless x >= 10
# foo unless x.even?
+ # foo unless odd?
#
# # good
# foo if bar
# foo if x == y
# foo if x < 10
# foo if x.odd?
+ # foo if even?
#
# # bad (complex condition)
# foo unless x != y || x.even?
#
# # good
@@ -97,26 +99,29 @@
when :send then preferred_send_condition(node)
when :or, :and then preferred_logical_condition(node)
end
end
- def preferred_send_condition(node)
- receiver_source = node.receiver.source
+ def preferred_send_condition(node) # rubocop:disable Metrics/CyclomaticComplexity
+ receiver_source = node.receiver&.source
return receiver_source if node.method?(:!)
+ # receiver may be implicit (self)
+ dotted_receiver_source = receiver_source ? "#{receiver_source}." : ''
+
inverse_method_name = inverse_methods[node.method_name]
- return "#{receiver_source}.#{inverse_method_name}" unless node.arguments?
+ return "#{dotted_receiver_source}#{inverse_method_name}" unless node.arguments?
argument_list = node.arguments.map(&:source).join(', ')
if node.operator_method?
return "#{receiver_source} #{inverse_method_name} #{argument_list}"
end
if node.parenthesized?
- return "#{receiver_source}.#{inverse_method_name}(#{argument_list})"
+ return "#{dotted_receiver_source}#{inverse_method_name}(#{argument_list})"
end
- "#{receiver_source}.#{inverse_method_name} #{argument_list}"
+ "#{dotted_receiver_source}#{inverse_method_name} #{argument_list}"
end
def preferred_logical_condition(node)
preferred_lhs = preferred_condition(node.lhs)
preferred_rhs = preferred_condition(node.rhs)