Skip to content

Commit

Permalink
Support filter in Rails/CompactBlank
Browse files Browse the repository at this point in the history
  • Loading branch information
masato-bkn committed Sep 9, 2024
1 parent cbb7257 commit 12894ef
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/change_support_filter_in_rails_compact_blank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1359](https://github.com/rubocop/rubocop-rails/pull/1359): Support `filter` in `Rails/CompactBlank`. ([@masato-bkn][])
14 changes: 10 additions & 4 deletions lib/rubocop/cop/rails/compact_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module Rails
# collection.reject { |_k, v| v.blank? }
# collection.select(&:present?)
# collection.select { |_k, v| v.present? }
# collection.filter(&:present?)
# collection.filter { |_k, v| v.present? }
#
# # good
# collection.compact_blank
Expand All @@ -44,7 +46,7 @@ class CompactBlank < Base
extend TargetRailsVersion

MSG = 'Use `%<preferred_method>s` instead.'
RESTRICT_ON_SEND = %i[reject delete_if select keep_if].freeze
RESTRICT_ON_SEND = %i[reject delete_if select filter keep_if].freeze

minimum_target_rails_version 6.1

Expand All @@ -64,14 +66,14 @@ class CompactBlank < Base

def_node_matcher :select_with_block?, <<~PATTERN
(block
(send _ {:select :keep_if})
(send _ {:select :filter :keep_if})
$(args ...)
(send
$(lvar _) :present?))
PATTERN

def_node_matcher :select_with_block_pass?, <<~PATTERN
(send _ {:select :keep_if}
(send _ {:select :filter :keep_if}
(block-pass
(sym :present?)))
PATTERN
Expand Down Expand Up @@ -120,7 +122,11 @@ def offense_range(node)
end

def preferred_method(node)
node.method?(:reject) || node.method?(:select) ? 'compact_blank' : 'compact_blank!'
if %i[reject select filter].include?(node.method_name)
'compact_blank'
else
'compact_blank!'
end
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions spec/rubocop/cop/rails/compact_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,39 @@
RUBY
end

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

it 'registers and corrects an offense when using `filter(&:present?)`' do
expect_offense(<<~RUBY)
collection.filter(&:present?)
^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
RUBY

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

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

it 'registers and corrects an offense when using `keep_if { |e| e.present? }`' do
expect_offense(<<~RUBY)
collection.keep_if { |e| e.present? }
Expand Down Expand Up @@ -169,6 +202,18 @@
RUBY
end

it 'does not register an offense when using `filter! { |e| e.present? }`' do
expect_no_offenses(<<~RUBY)
collection.filter! { |e| e.present? }
RUBY
end

it 'does not register an offense when using `filter!(&:present?)`' do
expect_no_offenses(<<~RUBY)
collection.filter!(&:present?)
RUBY
end

it 'does not register an offense when using `compact_blank`' do
expect_no_offenses(<<~RUBY)
collection.compact_blank
Expand Down Expand Up @@ -206,6 +251,12 @@ def foo(arg)
collection.select { |e| e.blank? }
RUBY
end

it 'does not register an offense when using `filter { |e| e.blank? }`' do
expect_no_offenses(<<~RUBY)
collection.filter { |e| e.blank? }
RUBY
end
end

context 'Rails <= 6.0', :rails60 do
Expand All @@ -226,5 +277,11 @@ def foo(arg)
collection.select { |e| e.present? }
RUBY
end

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

0 comments on commit 12894ef

Please sign in to comment.