Skip to content

Commit 636a726

Browse files
authored
Merge pull request #629 from koic/fix_a_false_positive_for_rails_compact_blank
[Fix #628] Fix a false positive for `Rails/CompactBlank`
2 parents b8e8405 + 799f9c7 commit 636a726

File tree

2 files changed

+11
-64
lines changed

2 files changed

+11
-64
lines changed

lib/rubocop/cop/rails/compact_blank.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ module Rails
1010
# blank check of block arguments to the receiver object.
1111
#
1212
# For example, `[[1, 2], [3, nil]].reject { |first, second| second.blank? }` and
13-
# `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `empty?`.
13+
# `[[1, 2], [3, nil]].compact_blank` are not compatible. The same is true for `blank?`.
1414
# This will work fine when the receiver is a hash object.
1515
#
1616
# @example
1717
#
1818
# # bad
1919
# collection.reject(&:blank?)
20-
# collection.reject(&:empty?)
2120
# collection.reject { |_k, v| v.blank? }
22-
# collection.reject { |_k, v| v.empty? }
2321
#
2422
# # good
2523
# collection.compact_blank
2624
#
2725
# # bad
2826
# collection.reject!(&:blank?)
29-
# collection.reject!(&:empty?)
3027
# collection.reject! { |_k, v| v.blank? }
31-
# collection.reject! { |_k, v| v.empty? }
3228
#
3329
# # good
3430
# collection.compact_blank!
@@ -48,13 +44,13 @@ class CompactBlank < Base
4844
(send _ {:reject :reject!})
4945
$(args ...)
5046
(send
51-
$(lvar _) {:blank? :empty?}))
47+
$(lvar _) :blank?))
5248
PATTERN
5349

5450
def_node_matcher :reject_with_block_pass?, <<~PATTERN
5551
(send _ {:reject :reject!}
5652
(block_pass
57-
(sym {:blank? :empty?})))
53+
(sym :blank?)))
5854
PATTERN
5955

6056
def on_send(node)

spec/rubocop/cop/rails/compact_blank_spec.rb

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@
1313
RUBY
1414
end
1515

16-
it 'registers and corrects an offense when using `reject { |e| e.empty? }`' do
17-
expect_offense(<<~RUBY)
18-
collection.reject { |e| e.empty? }
19-
^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
20-
RUBY
21-
22-
expect_correction(<<~RUBY)
23-
collection.compact_blank
24-
RUBY
25-
end
26-
2716
it 'registers and corrects an offense when using `reject(&:blank?)`' do
2817
expect_offense(<<~RUBY)
2918
collection.reject(&:blank?)
@@ -35,17 +24,6 @@
3524
RUBY
3625
end
3726

38-
it 'registers and corrects an offense when using `reject(&:empty?)`' do
39-
expect_offense(<<~RUBY)
40-
collection.reject(&:empty?)
41-
^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
42-
RUBY
43-
44-
expect_correction(<<~RUBY)
45-
collection.compact_blank
46-
RUBY
47-
end
48-
4927
it 'registers and corrects an offense when using `reject! { |e| e.blank? }`' do
5028
expect_offense(<<~RUBY)
5129
collection.reject! { |e| e.blank? }
@@ -57,17 +35,6 @@
5735
RUBY
5836
end
5937

60-
it 'registers and corrects an offense when using `reject! { |e| e.empty? }`' do
61-
expect_offense(<<~RUBY)
62-
collection.reject! { |e| e.empty? }
63-
^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank!` instead.
64-
RUBY
65-
66-
expect_correction(<<~RUBY)
67-
collection.compact_blank!
68-
RUBY
69-
end
70-
7138
it 'registers and corrects an offense when using `reject!(&:blank?)`' do
7239
expect_offense(<<~RUBY)
7340
collection.reject!(&:blank?)
@@ -79,28 +46,6 @@
7946
RUBY
8047
end
8148

82-
it 'registers and corrects an offense when using `reject!(&:empty?)`' do
83-
expect_offense(<<~RUBY)
84-
collection.reject!(&:empty?)
85-
^^^^^^^^^^^^^^^^^ Use `compact_blank!` instead.
86-
RUBY
87-
88-
expect_correction(<<~RUBY)
89-
collection.compact_blank!
90-
RUBY
91-
end
92-
93-
it 'registers and corrects an offense when using `reject { |k, v| v.empty? }`' do
94-
expect_offense(<<~RUBY)
95-
collection.reject { |k, v| v.empty? }
96-
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
97-
RUBY
98-
99-
expect_correction(<<~RUBY)
100-
collection.compact_blank
101-
RUBY
102-
end
103-
10449
it 'does not register an offense when using `compact_blank`' do
10550
expect_no_offenses(<<~RUBY)
10651
collection.compact_blank
@@ -113,9 +58,9 @@
11358
RUBY
11459
end
11560

116-
it 'does not register an offense when using `reject { |k, v| k.empty? }`' do
61+
it 'does not register an offense when using `reject { |k, v| k.blank? }`' do
11762
expect_no_offenses(<<~RUBY)
118-
collection.reject { |k, v| k.empty? }
63+
collection.reject { |k, v| k.blank? }
11964
RUBY
12065
end
12166

@@ -126,6 +71,12 @@ def foo(arg)
12671
end
12772
RUBY
12873
end
74+
75+
it 'does not register an offense when using `reject { |e| e.empty? }`' do
76+
expect_no_offenses(<<~RUBY)
77+
collection.reject { |e| e.empty? }
78+
RUBY
79+
end
12980
end
13081

13182
context 'Rails <= 6.0', :rails60 do

0 commit comments

Comments
 (0)