2019-09-13 09:26:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-01 07:11:29 -05:00
|
|
|
# This cop checks for missing GraphQL descriptions and enforces the description style guide:
|
|
|
|
# https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#description-style-guide
|
2019-09-13 09:26:31 -04:00
|
|
|
#
|
2021-03-01 07:11:29 -05:00
|
|
|
# @examples
|
2019-09-13 09:26:31 -04:00
|
|
|
#
|
|
|
|
# # bad
|
2021-03-01 07:11:29 -05:00
|
|
|
# class AwfulType
|
2021-07-23 14:10:06 -04:00
|
|
|
# field :some_field, GraphQL::Types::String
|
2019-09-13 09:26:31 -04:00
|
|
|
# end
|
|
|
|
#
|
2021-03-01 07:11:29 -05:00
|
|
|
# class TerribleType
|
2021-07-23 14:10:06 -04:00
|
|
|
# argument :some_argument, GraphQL::Types::String
|
2019-09-13 09:26:31 -04:00
|
|
|
# end
|
|
|
|
#
|
2021-03-01 07:11:29 -05:00
|
|
|
# class UngoodType
|
2020-12-11 10:10:04 -05:00
|
|
|
# field :some_argument,
|
2021-07-23 14:10:06 -04:00
|
|
|
# GraphQL::Types::String,
|
2020-12-11 10:10:04 -05:00
|
|
|
# description: "A description that does not end in a period"
|
|
|
|
# end
|
|
|
|
#
|
2021-03-01 07:11:29 -05:00
|
|
|
# class BadEnum
|
|
|
|
# value "some_value"
|
|
|
|
# end
|
|
|
|
#
|
2019-09-13 09:26:31 -04:00
|
|
|
# # good
|
2021-03-01 07:11:29 -05:00
|
|
|
# class GreatType
|
2019-09-13 09:26:31 -04:00
|
|
|
# argument :some_field,
|
2021-07-23 14:10:06 -04:00
|
|
|
# GraphQL::Types::String,
|
2020-12-11 10:10:04 -05:00
|
|
|
# description: "Well described - a superb description."
|
2019-09-13 09:26:31 -04:00
|
|
|
#
|
|
|
|
# field :some_field,
|
2021-07-23 14:10:06 -04:00
|
|
|
# GraphQL::Types::String,
|
2021-08-09 05:22:41 -04:00
|
|
|
# description: "Thorough and compelling description."
|
2019-09-13 09:26:31 -04:00
|
|
|
# end
|
2021-03-01 07:11:29 -05:00
|
|
|
#
|
|
|
|
# class GoodEnum
|
|
|
|
# value "some_value", "Good description."
|
|
|
|
# end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Graphql
|
|
|
|
class Descriptions < RuboCop::Cop::Cop
|
2021-08-09 05:22:41 -04:00
|
|
|
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}"
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-03-01 07:11:29 -05:00
|
|
|
def_node_matcher :graphql_describable?, <<~PATTERN
|
|
|
|
(send nil? {:field :argument :value} ...)
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def_node_matcher :enum?, <<~PATTERN
|
|
|
|
(send nil? :value ...)
|
2019-09-13 09:26:31 -04:00
|
|
|
PATTERN
|
|
|
|
|
2021-03-30 14:10:47 -04:00
|
|
|
def_node_matcher :resolver_kwarg, <<~PATTERN
|
|
|
|
(... (hash <(pair (sym :resolver) $_) ...>))
|
|
|
|
PATTERN
|
|
|
|
|
2021-03-01 07:11:29 -05:00
|
|
|
def_node_matcher :description_kwarg, <<~PATTERN
|
2020-12-11 10:10:04 -05:00
|
|
|
(... (hash <(pair (sym :description) $_) ...>))
|
2019-09-13 09:26:31 -04:00
|
|
|
PATTERN
|
|
|
|
|
2021-03-01 07:11:29 -05:00
|
|
|
def_node_matcher :enum_style_description, <<~PATTERN
|
|
|
|
(send nil? :value _ $str ...)
|
|
|
|
PATTERN
|
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
def on_send(node)
|
2021-03-01 07:11:29 -05:00
|
|
|
return unless graphql_describable?(node)
|
2021-03-30 14:10:47 -04:00
|
|
|
return if resolver_kwarg(node) # Fields may inherit the description from their resolvers.
|
2020-12-11 10:10:04 -05:00
|
|
|
|
2021-03-01 07:11:29 -05:00
|
|
|
description = locate_description(node)
|
2020-12-11 10:10:04 -05:00
|
|
|
|
|
|
|
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)
|
2021-08-09 05:22:41 -04:00
|
|
|
add_offense(node, location: :expression, message: MSG_BAD_START) if bad_start?(description)
|
2020-12-11 10:10:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Autocorrect missing periods at end of description.
|
|
|
|
def autocorrect(node)
|
|
|
|
lambda do |corrector|
|
2021-03-01 07:11:29 -05:00
|
|
|
description = locate_description(node)
|
2020-12-11 10:10:04 -05:00
|
|
|
next unless description
|
|
|
|
|
|
|
|
corrector.insert_after(before_end_quote(description), '.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-03-01 07:11:29 -05:00
|
|
|
# Fields and arguments define descriptions using a `description` keyword argument.
|
|
|
|
# Enums may define descriptions this way, or as a second `String` param.
|
|
|
|
def locate_description(node)
|
|
|
|
description = description_kwarg(node)
|
|
|
|
|
|
|
|
return description unless description.nil? && enum?(node)
|
|
|
|
|
|
|
|
enum_style_description(node)
|
|
|
|
end
|
|
|
|
|
2020-12-11 10:10:04 -05:00
|
|
|
def no_period?(description)
|
2021-08-09 05:22:41 -04:00
|
|
|
string?(description) && !description.value.strip.end_with?('.')
|
2020-12-11 10:10:04 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-08-09 05:22:41 -04:00
|
|
|
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.
|
2020-12-11 10:10:04 -05:00
|
|
|
def before_end_quote(string)
|
|
|
|
return string.source_range.adjust(end_pos: -1) unless string.heredoc?
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2020-12-11 10:10:04 -05:00
|
|
|
heredoc_source = string.location.heredoc_body.source
|
|
|
|
adjust = heredoc_source.index(/\s+\Z/) - heredoc_source.length
|
|
|
|
string.location.heredoc_body.adjust(end_pos: adjust)
|
2019-09-13 09:26:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|