summaryrefslogtreecommitdiff
path: root/rubocop/cop/migration/refer_to_index_by_name.rb
blob: 78619472487c2ac79f173090a3a836b969911071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      class ReferToIndexByName < RuboCop::Cop::Base
        include MigrationHelpers

        MSG = 'migration methods that refer to existing indexes must do so by name'

        def_node_matcher :match_index_exists, <<~PATTERN
          (send _ :index_exists? _ _ (hash $...) ?)
        PATTERN

        def_node_matcher :match_remove_index, <<~PATTERN
          (send _ :remove_index _ $_)
        PATTERN

        def_node_matcher :match_remove_concurrent_index, <<~PATTERN
          (send _ :remove_concurrent_index _ _ (hash $...) ?)
        PATTERN

        def_node_matcher :name_option?, <<~PATTERN
          (pair {(sym :name) (str "name")} _)
        PATTERN

        def on_def(node)
          return unless in_migration?(node)

          node.each_descendant(:send) do |send_node|
            next unless index_exists_offense?(send_node) || removing_index_offense?(send_node)

            add_offense(send_node.loc.selector)
          end
        end

        private

        def index_exists_offense?(send_node)
          match_index_exists(send_node) { |option_nodes| needs_name_option?(option_nodes) }
        end

        def removing_index_offense?(send_node)
          remove_index_offense?(send_node) || remove_concurrent_index_offense?(send_node)
        end

        def remove_index_offense?(send_node)
          match_remove_index(send_node) do |column_or_options_node|
            break true unless column_or_options_node.type == :hash

            column_or_options_node.children.none? { |pair| name_option?(pair) }
          end
        end

        def remove_concurrent_index_offense?(send_node)
          match_remove_concurrent_index(send_node) { |option_nodes| needs_name_option?(option_nodes) }
        end

        def needs_name_option?(option_nodes)
          option_nodes.empty? || option_nodes.first.none? { |node| name_option?(node) }
        end
      end
    end
  end
end