Welcome to mirror list, hosted at ThFree Co, Russian Federation.

background_migration_base_class.rb « migration « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ec8403a607f66872b68b634f4a889f4bcd6d8e6 (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
# frozen_string_literal: true

module RuboCop
  module Cop
    module Migration
      class BackgroundMigrationBaseClass < RuboCop::Cop::Base
        MSG = 'Batched background migration jobs should inherit from Gitlab::BackgroundMigration::BatchedMigrationJob'

        def_node_search :top_level_module?, <<~PATTERN
          (module (const nil? :Gitlab) (module (const nil? :BackgroundMigration) ...))
        PATTERN

        def_node_matcher :matching_parent_namespace?, <<~PATTERN
          {nil? (const (const {cbase nil?} :Gitlab) :BackgroundMigration)}
        PATTERN

        def_node_search :inherits_batched_migration_job?, <<~PATTERN
          (class _ (const #matching_parent_namespace? :BatchedMigrationJob) ...)
        PATTERN

        def on_module(module_node)
          return unless top_level_module?(module_node)

          top_level_class_node = module_node.each_descendant(:class).first

          return if top_level_class_node.nil? || inherits_batched_migration_job?(top_level_class_node)

          add_offense(top_level_class_node)
        end
      end
    end
  end
end