@@ -13,6 +13,12 @@ module Rails
13
13
# `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `blank?`.
14
14
# This will work fine when the receiver is a hash object.
15
15
#
16
+ # And `compact_blank!` has different implementations for `Array`, `Hash`, and
17
+ # `ActionController::Parameters`.
18
+ # `Array#compact_blank!`, `Hash#compact_blank!` are equivalent to `delete_if(&:blank?)`.
19
+ # `ActionController::Parameters#compact_blank!` is equivalent to `reject!(&:blank?)`.
20
+ # If the cop makes a mistake, auto-corrected code may get unexpected behavior.
21
+ #
16
22
# @example
17
23
#
18
24
# # bad
@@ -23,8 +29,10 @@ module Rails
23
29
# collection.compact_blank
24
30
#
25
31
# # bad
26
- # collection.reject!(&:blank?)
27
- # collection.reject! { |_k, v| v.blank? }
32
+ # collection.delete_if(&:blank?) # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
33
+ # collection.delete_if { |_k, v| v.blank? } # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
34
+ # collection.reject!(&:blank?) # Same behavior as `ActionController::Parameters#compact_blank!`
35
+ # collection.reject! { |_k, v| v.blank? } # Same behavior as `ActionController::Parameters#compact_blank!`
28
36
#
29
37
# # good
30
38
# collection.compact_blank!
@@ -35,20 +43,20 @@ class CompactBlank < Base
35
43
extend TargetRailsVersion
36
44
37
45
MSG = 'Use `%<preferred_method>s` instead.'
38
- RESTRICT_ON_SEND = %i[ reject reject! ] . freeze
46
+ RESTRICT_ON_SEND = %i[ reject delete_if reject! ] . freeze
39
47
40
48
minimum_target_rails_version 6.1
41
49
42
50
def_node_matcher :reject_with_block? , <<~PATTERN
43
51
(block
44
- (send _ {:reject :reject!})
52
+ (send _ {:reject :delete_if : reject!})
45
53
$(args ...)
46
54
(send
47
55
$(lvar _) :blank?))
48
56
PATTERN
49
57
50
58
def_node_matcher :reject_with_block_pass? , <<~PATTERN
51
- (send _ {:reject :reject!}
59
+ (send _ {:reject :delete_if : reject!}
52
60
(block_pass
53
61
(sym :blank?)))
54
62
PATTERN
0 commit comments