Skip to content

Commit

Permalink
Add new Rails/WhereMissing cop
Browse files Browse the repository at this point in the history
Refs: https://rails.rubystyle.guide/#finding-missing-relationship-records

This cop is check for the use `left_joins` and `where` method
for finding missing relationship records.

```ruby
# bad
Foo.left_joins(:foo).where(foos: { id: nil })

# good
Foo.where.missing(:foo)
```
  • Loading branch information
ydah committed Jul 19, 2022
1 parent 3ad75cc commit b77b44d
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_new_rails_where_missing_cop.md
@@ -0,0 +1 @@
* [#744](https://github.com/rubocop/rubocop-rails/pull/744): Add new `Rails/WhereMissing` cop. ([@ydah][])
6 changes: 6 additions & 0 deletions config/default.yml
Expand Up @@ -1018,6 +1018,12 @@ Rails/WhereExists:
VersionAdded: '2.7'
VersionChanged: '2.10'

Rails/WhereMissing:
Description: 'Check for the use `left_joins` and `where` method for finding missing relationship records.'
StyleGuide: 'https://rails.rubystyle.guide/#finding-missing-relationship-records'
Enabled: pending
VersionAdded: '<<next>>'

Rails/WhereNot:
Description: 'Use `where.not(...)` instead of manually constructing negated SQL in `where`.'
StyleGuide: 'https://rails.rubystyle.guide/#hash-conditions'
Expand Down
49 changes: 49 additions & 0 deletions lib/rubocop/cop/rails/where_missing.rb
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Check for the use `left_joins` and `where` method for finding missing relationship records.
#
# This cop is enabled in Rails 6.1 or higher.
#
# @example
# # bad
# Foo.left_joins(:foo).where(foos: { id: nil })
#
# # good
# Foo.where.missing(:foo)
#
class WhereMissing < Base
include RangeHelp
extend TargetRailsVersion

MSG = 'Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.'
RESTRICT_ON_SEND = %i[left_joins].freeze

minimum_target_rails_version 6.1

# @!method where_arguments(node)
def_node_search :where_arguments, <<~PATTERN
(send ... :where (hash (pair $(sym _) (hash (pair (sym :id) (nil)))) ... ))
PATTERN

def on_send(node)
where_arguments(node.ancestors.last) do |where_argument|
next unless same_relationship?(where_argument, node.first_argument)

range = range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
add_offense(range)
return
end
end

private

def same_relationship?(where, left_joins)
"#{left_joins.value}s" == where.value.to_s
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Expand Up @@ -121,4 +121,5 @@
require_relative 'rails/validation'
require_relative 'rails/where_equals'
require_relative 'rails/where_exists'
require_relative 'rails/where_missing'
require_relative 'rails/where_not'
127 changes: 127 additions & 0 deletions spec/rubocop/cop/rails/where_missing_spec.rb
@@ -0,0 +1,127 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::WhereMissing, :config do
context 'Rails 6.1', :rails61 do
it 'registers an offense when using `left_joins(foo).where(foos: {id: nil})`' do
expect_offense(<<~RUBY)
Foo.left_joins(:foo).where(foos: { id: nil }).where(bar: "bar")
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
RUBY
end

it 'registers an offense when using `where(foos: {id: nil}).left_joins(foo)`' do
expect_offense(<<~RUBY)
Foo.where(foos: { id: nil }).left_joins(:foo).where(bar: "bar")
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
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")
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
RUBY
end

it 'registers an offense when using `where(foos: {id: nil}).joins(:bar).left_joins(foo)`' do
expect_offense(<<~RUBY)
Foo.left_joins(:foo).joins(:bar).where(foos: { id: nil })
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
RUBY
end

it 'registers an offense when using `left_joins(foo).where(foos: {id: nil})` with multi-line leading ' \
'dot method calls' do
expect_offense(<<~RUBY)
Foo
.left_joins(:foo)
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
.where(foos: { id: nil })
.where(bar: "bar")
RUBY
end

it 'registers an offense when using `left_joins(foo).where(foos: {id: nil})` with multi-line trailing ' \
'dot method calls' do
expect_offense(<<~RUBY)
Foo.
left_joins(:foo).
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
where(foos: { id: nil }).
where(bar: "bar")
RUBY
end

it 'registers an offense when using `left_joins(foo).where(foos: {id: nil})` and there is a line break after' \
'`left_joins.where`' do
expect_offense(<<~RUBY)
Foo.left_joins(:foo).where(foos: { id: nil })
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
.where(bar: "bar")
RUBY
end

it 'registers an offense when using `left_joins(foo).where(foos: {id: nil})` and there is a line break after' \
'`left_joins.where` and receiver' do
expect_offense(<<~RUBY)
Foo
.left_joins(:foo).where(foos: { id: nil })
^^^^^^^^^^^^^^^^ Use `where.missing(:x)` instead of `left_joins(:x).where(xs: { id: nil })`.
.where(bar: "bar")
RUBY
end

it 'does not register an offense when not finding missing relationship records`' do
expect_no_offenses(<<~RUBY)
Foo.left_joins(:foo).where(bazs: { id: nil })
Foo.left_joins(:foo).where(foos: { name: nil })
Foo.left_joins(:foo).where(foos: { id: 1 })
RUBY
end
end

context 'Rails 6.0', :rails60 do
it 'does not register an offense when using `left_joins(foo).where(foos: {id: nil})`' do
expect_no_offenses(<<~RUBY)
Foo.left_joins(:foo).where(foos: { id: nil }).where(bar: "bar")
RUBY
end

it 'does not register an offense when using `left_joins(foo).where(foos: {id: nil})` with multi-line leading ' \
'dot method calls' do
expect_no_offenses(<<~RUBY)
Foo
.left_joins(:foo)
.where(foos: { id: nil })
.where(bar: "bar")
RUBY
end

it 'does not register an offense when using `left_joins(foo).where(foos: {id: nil})` with multi-line trailing ' \
'dot method calls' do
expect_no_offenses(<<~RUBY)
Foo.
left_joins(:foo).
where(foos: { id: nil }).
where(bar: "bar")
RUBY
end

it 'does not register an offense when using `left_joins(foo).where(foos: {id: nil})` and there is a line break ' \
'`after left_joins.where`' do
expect_no_offenses(<<~RUBY)
Foo.left_joins(:foo).where(foos: { id: nil })
.where(bar: "bar")
RUBY
end

it 'does not register an offense when using `left_joins(foo).where(foos: {id: nil})` and there is a line break ' \
'`after left_joins.where` and receiver' do
expect_no_offenses(<<~RUBY)
Foo
.left_joins(:foo).where(foos: { id: nil })
.where(bar: "bar")
RUBY
end
end
end

0 comments on commit b77b44d

Please sign in to comment.