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

enum_names.rb « graphql « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74847cb8d17093d14e74ef5fce338c626d3e3552 (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
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true

# This cop enforces the enum naming conventions from the enum style guide:
# https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#enums
#
# @example
#
#   # bad
#   class FooBar < BaseEnum
#     value 'FOO'
#   end
#
#   class SubparEnum < BaseEnum
#   end
#
#   class UngoodEnum < BaseEnum
#     graphql_name 'UngoodEnum'
#   end
#
#   # good
#
#   class GreatEnum < BaseEnum
#     graphql_name 'Great'
#
#     value 'BAR'
#   end
#
#   class NiceEnum < BaseEnum
#     declarative_enum NiceDeclarativeEnum
#   end

module RuboCop
  module Cop
    module Graphql
      class EnumNames < RuboCop::Cop::Base
        SEE_SG_MSG = "See https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#enums"
        CLASS_NAME_SUFFIX_MSG = "Enum class names must end with `Enum`. #{SEE_SG_MSG}"
        GRAPHQL_NAME_MISSING_MSG = "A `graphql_name` must be defined for a GraphQL enum. #{SEE_SG_MSG}"
        GRAPHQL_NAME_WITH_ENUM_MSG = "The `graphql_name` must not contain the string \"Enum\". #{SEE_SG_MSG}"

        def_node_matcher :enum_subclass, <<~PATTERN
          (class $(const nil? _) (const {nil? cbase} /.*Enum$/) ...)
        PATTERN

        def_node_search :find_graphql_name, <<~PATTERN
          (... `(send nil? :graphql_name $(...)) ...)
        PATTERN

        def_node_search :declarative_enum?, <<~PATTERN
          (... (send nil? :declarative_enum ...) ...)
        PATTERN

        def on_class(node)
          const_node = enum_subclass(node)
          return unless const_node

          check_class_name(const_node)
          check_graphql_name(node)
        end

        private

        def check_class_name(const_node)
          return unless const_node&.const_name
          return if const_node.const_name.end_with?('Enum')

          add_offense(const_node, message: CLASS_NAME_SUFFIX_MSG)
        end

        def check_graphql_name(node)
          graphql_name_node = find_graphql_name(node)&.first

          if graphql_name_node
            return unless graphql_name_node&.str_content
            return unless graphql_name_node.str_content.downcase.include?('enum')

            add_offense(graphql_name_node, message: GRAPHQL_NAME_WITH_ENUM_MSG)
          else
            return if declarative_enum?(node)

            add_offense(node, message: GRAPHQL_NAME_MISSING_MSG)
          end
        end
      end
    end
  end
end