Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#744](https://github.com/rubocop/rubocop-rails/pull/744): Add new `Rails/WhereMissing` cop. ([@ydah][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |