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

Fix an error for Rails/WhereExists with EnforcedStyle: where and implicit recievers #1241

Merged
merged 1 commit into from
Feb 26, 2024
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/fix_error_for_rails_where_exists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1241](https://github.com/rubocop/rubocop-rails/pull/1241): Fix an error for `Rails/WhereExists` with `EnforcedStyle: where` and implicit receivers. ([@earlopain][])
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 @@ -67,7 +67,7 @@ def on_send(node)
return unless convertable_args?(args)

range = correction_range(node)
good_method = build_good_method(args, dot_source: node.loc.dot.source)
good_method = build_good_method(args, dot: node.loc.dot)
message = format(MSG, good_method: good_method, bad_method: range.source)

add_offense(range, message: message) do |corrector|
Expand Down Expand Up @@ -109,11 +109,11 @@ def correction_range(node)
end
end

def build_good_method(args, dot_source: '.')
def build_good_method(args, dot:)
if exists_style?
build_good_method_exists(args)
elsif where_style?
build_good_method_where(args, dot_source)
build_good_method_where(args, dot&.source || '.')
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/rails/where_exists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
RUBY
end

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

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

it 'registers an offense when using `where(...).exists?` with an association' do
expect_offense(<<~RUBY)
user.posts.where(published: true).exists?
Expand Down Expand Up @@ -142,6 +153,17 @@
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')`.
RUBY

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

it 'registers an offense and corrects when using `exists?` with an association' do
expect_offense(<<~RUBY)
user.posts.exists?(published: true)
Expand Down