Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1511](https://github.com/rubocop/rubocop-rails/pull/1511): Don't register offenses for `Rails/WhereExists` when `exists?` is given multiple or splat arguments. ([@lovro-bikic][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rails/where_exists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class WhereExists < Base
(call (call _ :where $...) :exists?)
PATTERN

def_node_matcher :exists_with_args?, <<~PATTERN
(call _ :exists? $...)
def_node_matcher :exists_with_arg?, <<~PATTERN
(call _ :exists? $!splat_type?)
PATTERN

def on_send(node)
Expand Down Expand Up @@ -91,7 +91,7 @@ def find_offenses?(node, &block)
if exists_style?
where_exists_call?(node, &block)
elsif where_style?
exists_with_args?(node, &block)
exists_with_arg?(node) { |arg| yield([arg]) }
end
end

Expand Down
17 changes: 9 additions & 8 deletions spec/rubocop/cop/rails/where_exists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,26 @@
RUBY
end

it 'registers an offense and corrects when using `exists?` with an multiple arguments' do
expect_offense(<<~RUBY)
it 'does not register an offense when using `exists?` with multiple arguments' do
expect_no_offenses(<<~RUBY)
User.exists?('name = ?', 'john')
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `where('name = ?', 'john').exists?` over `exists?('name = ?', 'john')`.
RUBY
end

expect_correction(<<~RUBY)
User.where('name = ?', 'john').exists?
it 'does not register an offense when using `exists?` with splat argument' do
expect_no_offenses(<<~RUBY)
User.exists?(*conditions)
RUBY
end

it 'registers an offense when using implicit receiver and arg' do
expect_offense(<<~RUBY)
exists?('name = ?', 'john')
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `where('name = ?', 'john').exists?` over `exists?('name = ?', 'john')`.
exists?(['name = ?', 'john'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `where(['name = ?', 'john']).exists?` over `exists?(['name = ?', 'john'])`.
RUBY

expect_correction(<<~RUBY)
where('name = ?', 'john').exists?
where(['name = ?', 'john']).exists?
RUBY
end

Expand Down