Skip to content

Commit

Permalink
Fix an error for Style/WhereEquals when the second argument is not …
Browse files Browse the repository at this point in the history
…yet typed
  • Loading branch information
Earlopain committed Aug 4, 2024
1 parent f15473f commit 0c7301b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_where_equals_error.md
Original file line number Diff line number Diff line change
@@ -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][])
8 changes: 4 additions & 4 deletions lib/rubocop/cop/rails/where_equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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?

Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rails/where_equals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0c7301b

Please sign in to comment.