From b76ae638462ab0f673e5915986070518dd3f9ad3 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 19 Aug 2021 09:08:42 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-2-stable-ee --- rubocop/cop/graphql/descriptions.rb | 34 ++++++++++++++++++---------- rubocop/cop/graphql/id_type.rb | 4 ++-- rubocop/cop/graphql/json_type.rb | 2 +- rubocop/cop/graphql/old_types.rb | 44 ++++++++++++++++++++++++++++++++++++ rubocop/cop/graphql/resolver_type.rb | 4 ++-- 5 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 rubocop/cop/graphql/old_types.rb (limited to 'rubocop/cop/graphql') diff --git a/rubocop/cop/graphql/descriptions.rb b/rubocop/cop/graphql/descriptions.rb index 520e34dcd16..0d69fd55931 100644 --- a/rubocop/cop/graphql/descriptions.rb +++ b/rubocop/cop/graphql/descriptions.rb @@ -7,16 +7,16 @@ # # # bad # class AwfulType -# field :some_field, GraphQL::STRING_TYPE +# field :some_field, GraphQL::Types::String # end # # class TerribleType -# argument :some_argument, GraphQL::STRING_TYPE +# argument :some_argument, GraphQL::Types::String # end # # class UngoodType # field :some_argument, -# GraphQL::STRING_TYPE, +# GraphQL::Types::String, # description: "A description that does not end in a period" # end # @@ -27,12 +27,12 @@ # # good # class GreatType # argument :some_field, -# GraphQL::STRING_TYPE, +# GraphQL::Types::String, # description: "Well described - a superb description." # # field :some_field, -# GraphQL::STRING_TYPE, -# description: "A thorough and compelling description." +# GraphQL::Types::String, +# description: "Thorough and compelling description." # end # # class GoodEnum @@ -43,8 +43,10 @@ module RuboCop module Cop module Graphql class Descriptions < RuboCop::Cop::Cop - MSG_NO_DESCRIPTION = 'Please add a `description` property.' - MSG_NO_PERIOD = '`description` strings must end with a `.`.' + MSG_STYLE_GUIDE_LINK = 'See the description style guide: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#description-style-guide' + MSG_NO_DESCRIPTION = "Please add a `description` property. #{MSG_STYLE_GUIDE_LINK}" + MSG_NO_PERIOD = "`description` strings must end with a `.`. #{MSG_STYLE_GUIDE_LINK}" + MSG_BAD_START = "`description` strings should not start with \"A...\" or \"The...\". #{MSG_STYLE_GUIDE_LINK}" def_node_matcher :graphql_describable?, <<~PATTERN (send nil? {:field :argument :value} ...) @@ -75,6 +77,7 @@ module RuboCop return add_offense(node, location: :expression, message: MSG_NO_DESCRIPTION) unless description add_offense(node, location: :expression, message: MSG_NO_PERIOD) if no_period?(description) + add_offense(node, location: :expression, message: MSG_BAD_START) if bad_start?(description) end # Autocorrect missing periods at end of description. @@ -100,12 +103,19 @@ module RuboCop end def no_period?(description) - # Test that the description node is a `:str` (as opposed to - # a `#copy_field_description` call) before checking. - description.type == :str && !description.value.strip.end_with?('.') + string?(description) && !description.value.strip.end_with?('.') end - # Returns a Parser::Source::Range that ends just before the final String delimiter. + def bad_start?(description) + string?(description) && description.value.strip.downcase.start_with?('a ', 'the ') + end + + # Returns true if `description` node is a `:str` (as opposed to a `#copy_field_description` call) + def string?(description) + description.type == :str + end + + # Returns a `Parser::Source::Range` that ends just before the final `String` delimiter. def before_end_quote(string) return string.source_range.adjust(end_pos: -1) unless string.heredoc? diff --git a/rubocop/cop/graphql/id_type.rb b/rubocop/cop/graphql/id_type.rb index 0d2fb6ad852..ba973242efa 100644 --- a/rubocop/cop/graphql/id_type.rb +++ b/rubocop/cop/graphql/id_type.rb @@ -4,12 +4,12 @@ module RuboCop module Cop module Graphql class IDType < RuboCop::Cop::Cop - MSG = 'Do not use GraphQL::ID_TYPE, use a specific GlobalIDType instead' + MSG = 'Do not use GraphQL::Types::ID, use a specific GlobalIDType instead' WHITELISTED_ARGUMENTS = %i[iid full_path project_path group_path target_project_path namespace_path].freeze def_node_search :graphql_id_type?, <<~PATTERN - (send nil? :argument (_ #does_not_match?) (const (const nil? :GraphQL) :ID_TYPE) ...) + (send nil? :argument (_ #does_not_match?) (const (const (const nil? :GraphQL) :Types) :ID) ...) PATTERN def on_send(node) diff --git a/rubocop/cop/graphql/json_type.rb b/rubocop/cop/graphql/json_type.rb index a8c38358535..8518a771cbd 100644 --- a/rubocop/cop/graphql/json_type.rb +++ b/rubocop/cop/graphql/json_type.rb @@ -12,7 +12,7 @@ # # # good # class GreatClass -# field :some_field, GraphQL::STRING_TYPE +# field :some_field, GraphQL::Types::String # end module RuboCop diff --git a/rubocop/cop/graphql/old_types.rb b/rubocop/cop/graphql/old_types.rb new file mode 100644 index 00000000000..2df594c7016 --- /dev/null +++ b/rubocop/cop/graphql/old_types.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +# This cop checks for use of older GraphQL types in GraphQL fields +# and arguments. +# GraphQL::ID_TYPE, GraphQL::INT_TYPE, GraphQL::STRING_TYPE, GraphQL::BOOLEAN_TYPE +# +# @example +# +# # bad +# class AwfulClass +# field :some_field, GraphQL::STRING_TYPE +# end +# +# # good +# class GreatClass +# field :some_field, GraphQL::Types::String +# end + +module RuboCop + module Cop + module Graphql + class OldTypes < RuboCop::Cop::Cop + MSG_ID = 'Avoid using GraphQL::ID_TYPE. Use GraphQL::Types::ID instead' + MSG_INT = 'Avoid using GraphQL::INT_TYPE. Use GraphQL::Types::Int instead' + MSG_STRING = 'Avoid using GraphQL::STRING_TYPE. Use GraphQL::Types::String instead' + MSG_BOOLEAN = 'Avoid using GraphQL::BOOLEAN_TYPE. Use GraphQL::Types::Boolean instead' + + def_node_matcher :has_old_type?, <<~PATTERN + (send nil? {:field :argument} + (sym _) + (const (const nil? :GraphQL) ${:ID_TYPE :INT_TYPE :STRING_TYPE :BOOLEAN_TYPE}) + (...)?) + PATTERN + + def on_send(node) + old_constant = has_old_type?(node) + return unless old_constant + + add_offense(node, location: :expression, message: "#{self.class}::MSG_#{old_constant[0..-6]}".constantize) + end + end + end + end +end diff --git a/rubocop/cop/graphql/resolver_type.rb b/rubocop/cop/graphql/resolver_type.rb index 1209c5dbc6b..e9fa768fd3e 100644 --- a/rubocop/cop/graphql/resolver_type.rb +++ b/rubocop/cop/graphql/resolver_type.rb @@ -7,7 +7,7 @@ # # bad # module Resolvers # class NoTypeResolver < BaseResolver -# field :some_field, GraphQL::STRING_TYPE +# field :some_field, GraphQL::Types::String # end # end # @@ -16,7 +16,7 @@ # class WithTypeResolver < BaseResolver # type MyType, null: true # -# field :some_field, GraphQL::STRING_TYPE +# field :some_field, GraphQL::Types::String # end # end -- cgit v1.2.3