From 80beaf2189e6f4afa032954f495e9464ab12f88d Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:18:31 +0100 Subject: [PATCH] Fix an error for `Rails/WhereExists` with `EnforcedStyle: where` and implicit recievers --- changelog/fix_error_for_rails_where_exists.md | 1 + lib/rubocop/cop/rails/where_exists.rb | 6 ++--- spec/rubocop/cop/rails/where_exists_spec.rb | 22 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_error_for_rails_where_exists.md diff --git a/changelog/fix_error_for_rails_where_exists.md b/changelog/fix_error_for_rails_where_exists.md new file mode 100644 index 0000000000..612c7aa19f --- /dev/null +++ b/changelog/fix_error_for_rails_where_exists.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/where_exists.rb b/lib/rubocop/cop/rails/where_exists.rb index ff3b064b0c..505cf7e1b5 100644 --- a/lib/rubocop/cop/rails/where_exists.rb +++ b/lib/rubocop/cop/rails/where_exists.rb @@ -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| @@ -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 diff --git a/spec/rubocop/cop/rails/where_exists_spec.rb b/spec/rubocop/cop/rails/where_exists_spec.rb index b16a3e023d..1fade2409c 100644 --- a/spec/rubocop/cop/rails/where_exists_spec.rb +++ b/spec/rubocop/cop/rails/where_exists_spec.rb @@ -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? @@ -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)