From b7dfe2ae4054aa40e15182fd3c6cb7dd39f131db Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 13 Sep 2019 13:26:31 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- rubocop/cop/graphql/authorize_types.rb | 4 +-- rubocop/cop/graphql/descriptions.rb | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 rubocop/cop/graphql/descriptions.rb (limited to 'rubocop/cop/graphql') diff --git a/rubocop/cop/graphql/authorize_types.rb b/rubocop/cop/graphql/authorize_types.rb index cd8bdbaee59..c69ce10f1c5 100644 --- a/rubocop/cop/graphql/authorize_types.rb +++ b/rubocop/cop/graphql/authorize_types.rb @@ -30,7 +30,7 @@ module RuboCop def in_type?(node) path = node.location.expression.source_buffer.name - path.include?(TYPES_DIR) + path.include? TYPES_DIR end def whitelisted?(class_node) @@ -44,7 +44,7 @@ module RuboCop end def superclass_constant(class_node) - # First one is the class name itself, second is it's superclass + # First one is the class name itself, second is its superclass _class_constant, *others = class_node.descendants others.find { |node| node.const_type? && node&.const_name != 'Types' } diff --git a/rubocop/cop/graphql/descriptions.rb b/rubocop/cop/graphql/descriptions.rb new file mode 100644 index 00000000000..b4e00dfe336 --- /dev/null +++ b/rubocop/cop/graphql/descriptions.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +# This cop checks for missing GraphQL field descriptions. +# +# @example +# +# # bad +# class AwfulClass +# field :some_field, GraphQL::STRING_TYPE +# end +# +# class TerribleClass +# argument :some_argument, GraphQL::STRING_TYPE +# end +# +# # good +# class GreatClass +# argument :some_field, +# GraphQL::STRING_TYPE, +# description: "Well described - a superb description" +# +# field :some_field, +# GraphQL::STRING_TYPE, +# description: "A thorough and compelling description" +# end + +module RuboCop + module Cop + module Graphql + class Descriptions < RuboCop::Cop::Cop + MSG = 'Please add a `description` property.' + + # ability_field and permission_field set a default description. + def_node_matcher :fields, <<~PATTERN + (send nil? :field $...) + PATTERN + + def_node_matcher :arguments, <<~PATTERN + (send nil? :argument $...) + PATTERN + + def_node_matcher :has_description?, <<~PATTERN + (hash <(pair (sym :description) _) ...>) + PATTERN + + def on_send(node) + matches = fields(node) || arguments(node) + + return if matches.nil? + + add_offense(node, location: :expression) unless has_description?(matches.last) + end + end + end + end +end -- cgit v1.2.3