diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-03 18:11:31 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-03 18:11:31 +0300 |
commit | e6fa9529b4922a4c552e8908a2929ff995e8b53d (patch) | |
tree | 9ccfea4d253faa8c202f753631961a794af44ad6 /rubocop | |
parent | 720cf698151643831bf36e3bd4ccd1c8e9246184 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/graphql/enum_names.rb | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/rubocop/cop/graphql/enum_names.rb b/rubocop/cop/graphql/enum_names.rb new file mode 100644 index 00000000000..74847cb8d17 --- /dev/null +++ b/rubocop/cop/graphql/enum_names.rb @@ -0,0 +1,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 |