From f4f3bf13a3433a1223e675d1954ac8ad74d96eca Mon Sep 17 00:00:00 2001 From: masato-bkn <37011138+masato-bkn@users.noreply.github.com> Date: Wed, 11 Sep 2024 22:02:52 +0900 Subject: [PATCH] Fix `Rails/CompactBlank` to avoid reporting offense for `filter` in Ruby versions below 2.6 --- lib/rubocop/cop/rails/compact_blank.rb | 1 + spec/rubocop/cop/rails/compact_blank_spec.rb | 21 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/rubocop/cop/rails/compact_blank.rb b/lib/rubocop/cop/rails/compact_blank.rb index 5b3535cf76..636faf5084 100644 --- a/lib/rubocop/cop/rails/compact_blank.rb +++ b/lib/rubocop/cop/rails/compact_blank.rb @@ -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) diff --git a/spec/rubocop/cop/rails/compact_blank_spec.rb b/spec/rubocop/cop/rails/compact_blank_spec.rb index e89d7f3eca..75937de4ee 100644 --- a/spec/rubocop/cop/rails/compact_blank_spec.rb +++ b/spec/rubocop/cop/rails/compact_blank_spec.rb @@ -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