Skip to content

Commit

Permalink
Merge pull request #1253 from Earlopain/fix-error-for-rails-where-mis…
Browse files Browse the repository at this point in the history
…sing

Fix an error for `Rails/WhereMissing` without receiver
  • Loading branch information
koic committed Mar 20, 2024
2 parents 96f469d + b05a7e0 commit 2f893da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_rails_where_missing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1253](https://github.com/rubocop/rubocop-rails/pull/1253): Fix an error for `Rails/WhereMissing` with leading `where` without receiver. ([@earlopain][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/where_missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,20 @@ def replace_range(child)
end
end

# rubocop:disable Metrics/AbcSize
def remove_where_method(corrector, node, where_node)
range = range_between(where_node.loc.selector.begin_pos, where_node.loc.end.end_pos)
if node.multiline? && !same_line?(node, where_node)
range = range_by_whole_lines(range, include_final_newline: true)
else
elsif where_node.receiver
corrector.remove(where_node.loc.dot)
else
corrector.remove(node.loc.dot)
end

corrector.remove(range)
end
# rubocop:enable Metrics/AbcSize

def same_line?(left_joins_node, where_node)
left_joins_node.loc.selector.line == where_node.loc.selector.line
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/rails/where_missing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
RUBY
end

it 'registers an offense when using `left_joins(:foo).where(foo: {id: nil})` without receiver' do
expect_offense(<<~RUBY)
left_joins(:foo).where(foo: { id: nil }).where(bar: "bar")
^^^^^^^^^^^^^^^^ Use `where.missing(:foo)` instead of `left_joins(:foo).where(foo: { id: nil })`.
RUBY

expect_correction(<<~RUBY)
where.missing(:foo).where(bar: "bar")
RUBY
end

it 'registers an offense when using `left_outer_joins(:foo).where(foos: {id: nil})`' do
expect_offense(<<~RUBY)
Foo.left_outer_joins(:foo).where(foos: { id: nil }).where(bar: "bar")
Expand All @@ -46,6 +57,17 @@
RUBY
end

it 'registers an offense when using `where(foos: {id: nil}).left_joins(:foo)` without receiver' do
expect_offense(<<~RUBY)
where(foos: { id: nil }).left_joins(:foo).where(bar: "bar")
^^^^^^^^^^^^^^^^ Use `where.missing(:foo)` instead of `left_joins(:foo).where(foos: { id: nil })`.
RUBY

expect_correction(<<~RUBY)
where.missing(:foo).where(bar: "bar")
RUBY
end

it 'registers an offense when using `where(foos: {id: nil}, bar: "bar").left_joins(:foo)`' do
expect_offense(<<~RUBY)
Foo.where(foos: { id: nil }, bar: "bar").left_joins(:foo)
Expand All @@ -57,6 +79,17 @@
RUBY
end

it 'registers an offense when using `where(foos: {id: nil}, bar: "bar").left_joins(:foo)` without receiver' do
expect_offense(<<~RUBY)
where(foos: { id: nil }, bar: "bar").left_joins(:foo)
^^^^^^^^^^^^^^^^ Use `where.missing(:foo)` instead of `left_joins(:foo).where(foos: { id: nil })`.
RUBY

expect_correction(<<~RUBY)
where(bar: "bar").where.missing(:foo)
RUBY
end

it "registers an offense when using `left_joins(:foo).where(foos: {id: nil}, bar: 'bar')`" do
expect_offense(<<~RUBY)
Foo.left_joins(:foo).where(foos: { id: nil }, bar: "bar")
Expand Down

0 comments on commit 2f893da

Please sign in to comment.