Skip to content

Commit b8c4126

Browse files
authored
Merge pull request #1321 from Earlopain/where-equal-error
Fix an error for `Style/WhereEquals` when the second argument is not yet typed
2 parents f15473f + 0c7301b commit b8c4126

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

changelog/fix_where_equals_error.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/where_equals.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def on_send(node)
4444

4545
range = offense_range(node)
4646

47-
column_and_value = extract_column_and_value(template_node, value_node)
48-
return unless column_and_value
47+
column, value = extract_column_and_value(template_node, value_node)
48+
return unless value
4949

50-
good_method = build_good_method(*column_and_value)
50+
good_method = build_good_method(column, value)
5151
message = format(MSG, good_method: good_method)
5252

5353
add_offense(range, message: message) do |corrector|
@@ -73,7 +73,7 @@ def extract_column_and_value(template_node, value_node)
7373
value =
7474
case template_node.value
7575
when EQ_ANONYMOUS_RE, IN_ANONYMOUS_RE
76-
value_node.source
76+
value_node&.source
7777
when EQ_NAMED_RE, IN_NAMED_RE
7878
return unless value_node&.hash_type?
7979

spec/rubocop/cop/rails/where_equals_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,16 @@
188188
User.where("id IN (#{sql})", name: 'Lastname').first
189189
RUBY
190190
end
191+
192+
it 'does not register an offense when using `=` and the second argument has no content' do
193+
expect_no_offenses(<<~RUBY)
194+
User.where('name = ?', )
195+
RUBY
196+
end
197+
198+
it 'does not register an offense when using `IN` and the second argument has no content' do
199+
expect_no_offenses(<<~RUBY)
200+
User.where("name IN (:names)", )
201+
RUBY
202+
end
191203
end

0 commit comments

Comments
 (0)