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

Add new Rails/WhereMissing cop #744

Merged
merged 1 commit into from Aug 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1024,6 +1024,12 @@ Rails/WhereExists:
VersionAdded: '2.7'
VersionChanged: '2.10'

Rails/WhereMissing:
Description: 'Use `where.missing(...)` to find 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
111 changes: 111 additions & 0 deletions lib/rubocop/cop/rails/where_missing.rb
@@ -0,0 +1,111 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Use `where.missing(...)` to find missing relationship records.
#
# This cop is enabled in Rails 6.1 or higher.
#
# @example
# # bad
# Post.left_joins(:author).where(authors: { id: nil })
#
# # good
# Post.where.missing(:author)
#
class WhereMissing < Base
include RangeHelp
extend AutoCorrector
extend TargetRailsVersion

MSG = 'Use `where.missing(:%<left_joins_association>s)` instead of ' \
'`%<left_joins_method>s(:%<left_joins_association>s).where(%<where_association>s: { id: nil })`.'
RESTRICT_ON_SEND = %i[left_joins left_outer_joins].freeze

minimum_target_rails_version 6.1

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

# @!method missing_relationship(node)
def_node_search :missing_relationship, <<~PATTERN
(pair (sym _) (hash (pair (sym :id) (nil))))
PATTERN

def on_send(node)
return unless node.first_argument.sym_type?

where_node_and_argument(root_receiver(node)) do |where_node, where_argument|
next unless same_relationship?(where_argument, node.first_argument)

range = range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
register_offense(node, where_node, where_argument, range)
break
end
end

private

def root_receiver(node)
node&.parent&.send_type? ? root_receiver(node.parent) : node
end

def same_relationship?(where, left_joins)
where.value.to_s.match?(/^#{left_joins.value}s?$/)
end

def register_offense(node, where_node, where_argument, range)
add_offense(range, message: message(node, where_argument)) do |corrector|
corrector.replace(node.loc.selector, 'where.missing')
if multi_condition?(where_node.first_argument)
replace_where_method(corrector, where_node)
else
remove_where_method(corrector, node, where_node)
end
end
end

def replace_where_method(corrector, where_node)
missing_relationship(where_node) do |where_clause|
corrector.remove(replace_range(where_clause))
end
end

def replace_range(child)
if (right_sibling = child.right_sibling)
range_between(child.loc.expression.begin_pos, right_sibling.loc.expression.begin_pos)
else
range_between(child.left_sibling.loc.expression.end_pos, child.loc.expression.end_pos)
end
end

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
corrector.remove(where_node.loc.dot)
end

corrector.remove(range)
end

def same_line?(left_joins_node, where_node)
left_joins_node.loc.selector.line == where_node.loc.selector.line
end

def multi_condition?(where_arg)
where_arg.children.count > 1
end

def message(node, where_argument)
format(MSG, left_joins_association: node.first_argument.value, left_joins_method: node.method_name,
where_association: where_argument.value)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Expand Up @@ -122,4 +122,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'