Skip to content

Commit 5e66785

Browse files
authored
Merge pull request #1361 from masato-bkn/fix-rails-compact-blank
Fix `Rails/CompactBlank` to avoid reporting offense for `filter` in Ruby versions below 2.6
2 parents 5fbb05d + f4f3bf1 commit 5e66785

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/rubocop/cop/rails/compact_blank.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CompactBlank < Base
8080
PATTERN
8181

8282
def on_send(node)
83+
return if target_ruby_version < 2.6 && node.method?(:filter)
8384
return unless bad_method?(node)
8485

8586
range = offense_range(node)

spec/rubocop/cop/rails/compact_blank_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,27 @@ def foo(arg)
257257
collection.filter { |e| e.blank? }
258258
RUBY
259259
end
260+
261+
context 'target_ruby_version >= 2.6', :ruby26 do
262+
it 'registers and corrects an offense when using `filter { |e| e.present? }`' do
263+
expect_offense(<<~RUBY)
264+
collection.filter { |e| e.present? }
265+
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
266+
RUBY
267+
268+
expect_correction(<<~RUBY)
269+
collection.compact_blank
270+
RUBY
271+
end
272+
end
273+
274+
context 'target_ruby_version < 2.6', :ruby25, unsupported_on: :prism do
275+
it 'does not register an offense when using `filter { |e| e.present? }`' do
276+
expect_no_offenses(<<~RUBY)
277+
collection.filter { |e| e.present? }
278+
RUBY
279+
end
280+
end
260281
end
261282

262283
context 'Rails <= 6.0', :rails60 do

0 commit comments

Comments
 (0)