diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-07 06:07:49 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-07 06:07:49 +0300 |
commit | a31408ba64f61275813cc3ffd5aa9bc9ce9f3319 (patch) | |
tree | 711e3c6f7ea239e0aedc28a815d7067280399314 /rubocop | |
parent | 175f124d93ba52aeb850b5c032930168612d1e71 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/graphql/enum_values.rb | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/rubocop/cop/graphql/enum_values.rb b/rubocop/cop/graphql/enum_values.rb new file mode 100644 index 00000000000..71c42596334 --- /dev/null +++ b/rubocop/cop/graphql/enum_values.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +# This cop enforces the enum value conventions from the enum style guide: +# https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#enums +# +# @example +# +# # bad +# class BadEnum < BaseEnum +# graphql_name 'Bad' +# +# value 'foo' +# end +# +# class UngoodEnum < BaseEnum +# graphql_name 'Ungood' +# +# ['bar'].each do |val| +# value val +# end +# end +# +# # good +# class GoodEnum < BaseEnum +# graphql_name 'Good' +# +# value 'FOO' +# end +# +# class GreatEnum < BaseEnum +# graphql_name 'Great' +# +# ['bar'].each do |val| +# value val.upcase +# end +# end + +module RuboCop + module Cop + module Graphql + class EnumValues < RuboCop::Cop::Base + MSG = "Enum values must either be an uppercase string literal or uppercased with the `upcase` method. " \ + "See https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#enums" + + def_node_matcher :enum_value, <<~PATTERN + (send nil? :value $_ $...) + PATTERN + + def_node_search :deprecated?, <<~PATTERN + (hash <(pair (sym :deprecated) _) ...>) + PATTERN + + def_node_matcher :upcase_literal?, <<~PATTERN + (str #upcase?) + PATTERN + + def_node_matcher :upcase_method?, <<~PATTERN + `(send _ :upcase) + PATTERN + + def on_send(node) + value_node, params = enum_value(node) + + return unless value_node + return if params.any? { deprecated?(_1) } + return if upcase_literal?(value_node) || upcase_method?(value_node) + + add_offense(value_node) + end + + private + + def upcase?(str) + str == str.upcase + end + end + end + end +end |