|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module Rails |
| 6 | + # Use `where.missing(...)` to find missing relationship records. |
| 7 | + # |
| 8 | + # This cop is enabled in Rails 6.1 or higher. |
| 9 | + # |
| 10 | + # @example |
| 11 | + # # bad |
| 12 | + # Post.left_joins(:author).where(authors: { id: nil }) |
| 13 | + # |
| 14 | + # # good |
| 15 | + # Post.where.missing(:author) |
| 16 | + # |
| 17 | + class WhereMissing < Base |
| 18 | + include RangeHelp |
| 19 | + extend AutoCorrector |
| 20 | + extend TargetRailsVersion |
| 21 | + |
| 22 | + MSG = 'Use `where.missing(:%<left_joins_association>s)` instead of ' \ |
| 23 | + '`%<left_joins_method>s(:%<left_joins_association>s).where(%<where_association>s: { id: nil })`.' |
| 24 | + RESTRICT_ON_SEND = %i[left_joins left_outer_joins].freeze |
| 25 | + |
| 26 | + minimum_target_rails_version 6.1 |
| 27 | + |
| 28 | + # @!method where_node_and_argument(node) |
| 29 | + def_node_search :where_node_and_argument, <<~PATTERN |
| 30 | + $(send ... :where (hash <(pair $(sym _) (hash (pair (sym :id) (nil))))...> )) |
| 31 | + PATTERN |
| 32 | + |
| 33 | + # @!method missing_relationship(node) |
| 34 | + def_node_search :missing_relationship, <<~PATTERN |
| 35 | + (pair (sym _) (hash (pair (sym :id) (nil)))) |
| 36 | + PATTERN |
| 37 | + |
| 38 | + def on_send(node) |
| 39 | + return unless node.first_argument.sym_type? |
| 40 | + |
| 41 | + where_node_and_argument(root_receiver(node)) do |where_node, where_argument| |
| 42 | + next unless same_relationship?(where_argument, node.first_argument) |
| 43 | + |
| 44 | + range = range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos) |
| 45 | + register_offense(node, where_node, where_argument, range) |
| 46 | + break |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + private |
| 51 | + |
| 52 | + def root_receiver(node) |
| 53 | + node&.parent&.send_type? ? root_receiver(node.parent) : node |
| 54 | + end |
| 55 | + |
| 56 | + def same_relationship?(where, left_joins) |
| 57 | + where.value.to_s.match?(/^#{left_joins.value}s?$/) |
| 58 | + end |
| 59 | + |
| 60 | + def register_offense(node, where_node, where_argument, range) |
| 61 | + add_offense(range, message: message(node, where_argument)) do |corrector| |
| 62 | + corrector.replace(node.loc.selector, 'where.missing') |
| 63 | + if multi_condition?(where_node.first_argument) |
| 64 | + replace_where_method(corrector, where_node) |
| 65 | + else |
| 66 | + remove_where_method(corrector, node, where_node) |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def replace_where_method(corrector, where_node) |
| 72 | + missing_relationship(where_node) do |where_clause| |
| 73 | + corrector.remove(replace_range(where_clause)) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def replace_range(child) |
| 78 | + if (right_sibling = child.right_sibling) |
| 79 | + range_between(child.loc.expression.begin_pos, right_sibling.loc.expression.begin_pos) |
| 80 | + else |
| 81 | + range_between(child.left_sibling.loc.expression.end_pos, child.loc.expression.end_pos) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def remove_where_method(corrector, node, where_node) |
| 86 | + range = range_between(where_node.loc.selector.begin_pos, where_node.loc.end.end_pos) |
| 87 | + if node.multiline? && !same_line?(node, where_node) |
| 88 | + range = range_by_whole_lines(range, include_final_newline: true) |
| 89 | + else |
| 90 | + corrector.remove(where_node.loc.dot) |
| 91 | + end |
| 92 | + |
| 93 | + corrector.remove(range) |
| 94 | + end |
| 95 | + |
| 96 | + def same_line?(left_joins_node, where_node) |
| 97 | + left_joins_node.loc.selector.line == where_node.loc.selector.line |
| 98 | + end |
| 99 | + |
| 100 | + def multi_condition?(where_arg) |
| 101 | + where_arg.children.count > 1 |
| 102 | + end |
| 103 | + |
| 104 | + def message(node, where_argument) |
| 105 | + format(MSG, left_joins_association: node.first_argument.value, left_joins_method: node.method_name, |
| 106 | + where_association: where_argument.value) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments