Skip to content

Commit

Permalink
Merge pull request #1361 from masato-bkn/fix-rails-compact-blank
Browse files Browse the repository at this point in the history
Fix `Rails/CompactBlank` to avoid reporting offense for `filter` in Ruby versions below 2.6
  • Loading branch information
koic authored Sep 11, 2024
2 parents 5fbb05d + f4f3bf1 commit 5e66785
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 { |e| e.present? }`' do
expect_offense(<<~RUBY)
collection.filter { |e| e.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 5e66785

Please sign in to comment.