Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filter in Rails/CompactBlank #1359

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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][])
11 changes: 7 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,8 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RESTRICT_ON_SEND = %i[reject delete_if select filter keep_if].freeze
RESTRICT_ON_SEND = %i[reject delete_if select filter keep_if].freeze
DESTRUCTIVE_METHODS = %i[delete_if keep_if].freeze

DESTRUCTIVE_METHODS = %i[delete_if keep_if].freeze

minimum_target_rails_version 6.1

Expand All @@ -64,14 +67,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 +123,7 @@ def offense_range(node)
end

def preferred_method(node)
node.method?(:reject) || node.method?(:select) ? 'compact_blank' : 'compact_blank!'
DESTRUCTIVE_METHODS.include?(node.method_name) ? 'compact_blank!' : 'compact_blank'
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