Skip to content

Commit

Permalink
Fix Rails/CompactBlank to avoid reporting offense for filter in R…
Browse files Browse the repository at this point in the history
…uby versions below 2.6
  • Loading branch information
masato-bkn committed Sep 11, 2024
1 parent 5fbb05d commit 378af29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails/compact_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CompactBlank < Base
PATTERN

def on_send(node)
return if target_ruby_version < 2.6 && node.method?(:filter)
return unless bad_method?(node)

range = offense_range(node)
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/rails/compact_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,27 @@ def foo(arg)
collection.filter { |e| e.blank? }
RUBY
end

context 'target_ruby_version >= 2.6', :ruby26 do
it 'registers and corrects an offense when using `filter { |k, v| v.present? }`' do
expect_offense(<<~RUBY)
collection.filter { |k, v| v.present? }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
RUBY

expect_correction(<<~RUBY)
collection.compact_blank
RUBY
end
end

context 'target_ruby_version < 2.6', :ruby25, unsupported_on: :prism do
it 'does not register an offense when using `filter { |e| e.present? }`' do
expect_no_offenses(<<~RUBY)
collection.filter { |e| e.present? }
RUBY
end
end
end

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

0 comments on commit 378af29

Please sign in to comment.