summaryrefslogtreecommitdiff
path: root/rubocop/cop/gitlab/namespaced_class.rb
blob: 1f1fd2809227b4885e8d22e39a55bcef26d12bfe (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
# frozen_string_literal: true

module RuboCop
  module Cop
    module Gitlab
      # Cop that enforces use of namespaced classes in order to better identify
      # high level domains within the codebase.

      # @example
      #   # bad
      #   class MyClass
      #   end
      #
      #   # good
      #   module MyDomain
      #     class MyClass
      #     end
      #   end

      class NamespacedClass < RuboCop::Cop::Cop
        MSG = 'Classes must be declared inside a module indicating a product domain namespace. For more info: https://gitlab.com/gitlab-org/gitlab/-/issues/212156'

        def_node_matcher :compact_namespaced_class?, <<~PATTERN
          (class (const (const ...) ...) ...)
        PATTERN

        def on_module(node)
          @namespaced = true
        end

        def on_class(node)
          return if @namespaced

          add_offense(node) unless compact_namespaced_class?(node)
        end
      end
    end
  end
end