Skip to content

Commit

Permalink
[Fix rubocop#626] Fix a false positive for Rails/CompactBlank
Browse files Browse the repository at this point in the history
Fixes rubocop#626.

Fix a false positive for `Rails/CompactBlank` when using the receiver
of `blank?` is not a block variable.
  • Loading branch information
koic committed Jan 12, 2022
1 parent ba2a7e3 commit 5a1503f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_for_rails_compact_blank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#626](https://github.com/rubocop/rubocop-rails/issues/626: Fix a false positive for `Rails/CompactBlank` when using the receiver of `blank?` is not a block variable. ([@koic][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rails/compact_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ 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
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/compact_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
collection.reject { |k, v| k.empty? }
RUBY
end

it 'does not register an offense when using the receiver of `blank?` is not a block variable' do
expect_no_offenses(<<~RUBY)
def foo(arg)
collection.reject { |_| arg.blank? }
end
RUBY
end
end

context 'Rails <= 6.0', :rails60 do
Expand Down

0 comments on commit 5a1503f

Please sign in to comment.