From 0c7301ba0ee0bfa1659c024d4beff80ac760d525 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sun, 4 Aug 2024 11:44:01 +0200 Subject: [PATCH] Fix an error for `Style/WhereEquals` when the second argument is not yet typed --- changelog/fix_where_equals_error.md | 1 + lib/rubocop/cop/rails/where_equals.rb | 8 ++++---- spec/rubocop/cop/rails/where_equals_spec.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 changelog/fix_where_equals_error.md diff --git a/changelog/fix_where_equals_error.md b/changelog/fix_where_equals_error.md new file mode 100644 index 0000000000..2e1ccc7309 --- /dev/null +++ b/changelog/fix_where_equals_error.md @@ -0,0 +1 @@ +* [#1321](https://github.com/rubocop/rubocop-rails/pull/1321): Fix an error for `Rails/WhereEquals` when the second argument is not yet typed (`where("foo = ?", )`). ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/where_equals.rb b/lib/rubocop/cop/rails/where_equals.rb index d7a568e8e4..ce5b6d2a58 100644 --- a/lib/rubocop/cop/rails/where_equals.rb +++ b/lib/rubocop/cop/rails/where_equals.rb @@ -44,10 +44,10 @@ def on_send(node) range = offense_range(node) - column_and_value = extract_column_and_value(template_node, value_node) - return unless column_and_value + column, value = extract_column_and_value(template_node, value_node) + return unless value - good_method = build_good_method(*column_and_value) + good_method = build_good_method(column, value) message = format(MSG, good_method: good_method) add_offense(range, message: message) do |corrector| @@ -73,7 +73,7 @@ def extract_column_and_value(template_node, value_node) value = case template_node.value when EQ_ANONYMOUS_RE, IN_ANONYMOUS_RE - value_node.source + value_node&.source when EQ_NAMED_RE, IN_NAMED_RE return unless value_node&.hash_type? diff --git a/spec/rubocop/cop/rails/where_equals_spec.rb b/spec/rubocop/cop/rails/where_equals_spec.rb index 69c718d85c..11083179f9 100644 --- a/spec/rubocop/cop/rails/where_equals_spec.rb +++ b/spec/rubocop/cop/rails/where_equals_spec.rb @@ -188,4 +188,16 @@ User.where("id IN (#{sql})", name: 'Lastname').first RUBY end + + it 'does not register an offense when using `=` and the second argument has no content' do + expect_no_offenses(<<~RUBY) + User.where('name = ?', ) + RUBY + end + + it 'does not register an offense when using `IN` and the second argument has no content' do + expect_no_offenses(<<~RUBY) + User.where("name IN (:names)", ) + RUBY + end end