Skip to content

Commit fa67d8d

Browse files
committed
Don't register offenses for Rails/WhereExists when exists? is given multiple or splat arguments
1 parent 812251d commit fa67d8d

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/where_exists.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class WhereExists < Base
5858
(call (call _ :where $...) :exists?)
5959
PATTERN
6060

61-
def_node_matcher :exists_with_args?, <<~PATTERN
62-
(call _ :exists? $...)
61+
def_node_matcher :exists_with_arg?, <<~PATTERN
62+
(call _ :exists? $!splat_type?)
6363
PATTERN
6464

6565
def on_send(node)
@@ -91,7 +91,7 @@ def find_offenses?(node, &block)
9191
if exists_style?
9292
where_exists_call?(node, &block)
9393
elsif where_style?
94-
exists_with_args?(node, &block)
94+
exists_with_arg?(node) { |arg| yield([arg]) }
9595
end
9696
end
9797

spec/rubocop/cop/rails/where_exists_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,26 @@
142142
RUBY
143143
end
144144

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

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

156157
it 'registers an offense when using implicit receiver and arg' do
157158
expect_offense(<<~RUBY)
158-
exists?('name = ?', 'john')
159-
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `where('name = ?', 'john').exists?` over `exists?('name = ?', 'john')`.
159+
exists?(['name = ?', 'john'])
160+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `where(['name = ?', 'john']).exists?` over `exists?(['name = ?', 'john'])`.
160161
RUBY
161162

162163
expect_correction(<<~RUBY)
163-
where('name = ?', 'john').exists?
164+
where(['name = ?', 'john']).exists?
164165
RUBY
165166
end
166167

0 commit comments

Comments
 (0)