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

namespaced_class.rb « search « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d0901cb6f63909894116cbff6e0919466324696 (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
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

module RuboCop
  module Cop
    module Search
      # Cop that enforces use of Search namespace for search related code.
      #
      # @example
      #   # bad
      #   class MySearchClass
      #   end
      #
      #   # good
      #   module Search
      #     class MySearchClass
      #     end
      #   end
      class NamespacedClass < RuboCop::Cop::Base
        MSG = 'Search related code must be declared inside Search top level namespace. For more info: https://gitlab.com/gitlab-org/gitlab/-/issues/398207'

        # These namespaces are considered acceptable.
        # Note: Nested namespace like Foo::Bar are also supported.
        PERMITTED_NAMESPACES = %w[
          Search EE::Search API::Search EE::API::Search API::Admin::Search RuboCop::Cop::Search
          API::Entities::Search::Zoekt
        ].map { |x| x.split('::') }.freeze

        SEARCH_REGEXES = [
          /elastic/i,
          /zoekt/i,
          /search/i
        ].freeze

        def on_module(node)
          add_identifiers(node)

          run_search_namespace_cop(node) if node.child_nodes.none? { |n| n.module_type? || n.class_type? }
        end

        def on_class(node)
          add_identifiers(node)
          run_search_namespace_cop(node)
        end

        private

        def run_search_namespace_cop(node)
          add_offense(node.loc.name) if !namespace_allowed? && namespace_search_related?
        end

        def add_identifiers(node)
          identifiers.concat(identifiers_for(node))
        end

        def identifiers
          @identifiers ||= []
        end

        def identifiers_for(node)
          source = node.respond_to?(:identifier) ? node.identifier.source : node.source
          source.sub(/^::/, '').split('::')
        end

        def namespace_allowed?
          PERMITTED_NAMESPACES.any? do |namespaces|
            identifiers.first(namespaces.size) == namespaces
          end
        end

        def namespace_search_related?
          SEARCH_REGEXES.any? { |x| x.match?(identifiers.join('::')) }
        end
      end
    end
  end
end