lib/rubocop/cop/rails/compact_blank.rb in rubocop-rails-2.13.1 vs lib/rubocop/cop/rails/compact_blank.rb in rubocop-rails-2.13.2
- old
+ new
@@ -8,29 +8,25 @@
# @safety
# It is unsafe by default because false positives may occur in the
# blank check of block arguments to the receiver object.
#
# For example, `[[1, 2], [3, nil]].reject { |first, second| second.blank? }` and
- # `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `empty?`.
+ # `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `blank?`.
# This will work fine when the receiver is a hash object.
#
# @example
#
# # bad
# collection.reject(&:blank?)
- # collection.reject(&:empty?)
# collection.reject { |_k, v| v.blank? }
- # collection.reject { |_k, v| v.empty? }
#
# # good
# collection.compact_blank
#
# # bad
# collection.reject!(&:blank?)
- # collection.reject!(&:empty?)
# collection.reject! { |_k, v| v.blank? }
- # collection.reject! { |_k, v| v.empty? }
#
# # good
# collection.compact_blank!
#
class CompactBlank < Base
@@ -46,17 +42,17 @@
def_node_matcher :reject_with_block?, <<~PATTERN
(block
(send _ {:reject :reject!})
$(args ...)
(send
- $(lvar _) {:blank? :empty?}))
+ $(lvar _) :blank?))
PATTERN
def_node_matcher :reject_with_block_pass?, <<~PATTERN
(send _ {:reject :reject!}
(block_pass
- (sym {:blank? :empty?})))
+ (sym :blank?)))
PATTERN
def on_send(node)
return unless bad_method?(node)
@@ -71,13 +67,18 @@
def bad_method?(node)
return true if reject_with_block_pass?(node)
if (arguments, receiver_in_block = reject_with_block?(node.parent))
- return arguments.length == 1 || use_hash_value_block_argument?(arguments, receiver_in_block)
+ return use_single_value_block_argument?(arguments, receiver_in_block) ||
+ use_hash_value_block_argument?(arguments, receiver_in_block)
end
false
+ end
+
+ def use_single_value_block_argument?(arguments, receiver_in_block)
+ arguments.length == 1 && arguments[0].source == receiver_in_block.source
end
def use_hash_value_block_argument?(arguments, receiver_in_block)
arguments.length == 2 && arguments[1].source == receiver_in_block.source
end