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 Rails/WhereMissing without receiver #1253

Merged
merged 1 commit into from
Mar 20, 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_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