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 Style/WhereEquals when the second argument is not yet typed #1321

Merged
merged 1 commit into from
Aug 9, 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_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)", )
Earlopain marked this conversation as resolved.
Show resolved Hide resolved
RUBY
end
end