Welcome to mirror list, hosted at ThFree Co, Russian Federation.

descriptions.rb « graphql « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c9455076991cffc3304a8455947ce5a5e363590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true

# 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
#
# @examples
#
#   # bad
#   class AwfulType
#     field :some_field, GraphQL::Types::String
#   end
#
#   class TerribleType
#     argument :some_argument, GraphQL::Types::String
#   end
#
#   class UngoodType
#     field :some_argument,
#       GraphQL::Types::String,
#       description: "A description that does not end in a period"
#   end
#
#   class BadEnum
#     value "some_value"
#   end
#
#   # good
#   class GreatType
#     argument :some_field,
#       GraphQL::Types::String,
#       description: "Well described - a superb description."
#
#     field :some_field,
#       GraphQL::Types::String,
#       description: "Thorough and compelling description."
#   end
#
#   class GoodEnum
#     value "some_value", "Good description."
#   end

module RuboCop
  module Cop
    module Graphql
      class Descriptions < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        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} ...)
        PATTERN

        def_node_matcher :enum?, <<~PATTERN
          (send nil? :value ...)
        PATTERN

        def_node_matcher :resolver_kwarg, <<~PATTERN
          (... (hash <(pair (sym :resolver) $_) ...>))
        PATTERN

        def_node_matcher :description_kwarg, <<~PATTERN
          (... (hash <(pair (sym :description) $_) ...>))
        PATTERN

        def_node_matcher :enum_style_description, <<~PATTERN
          (send nil? :value _ $str ...)
        PATTERN

        def on_send(node)
          return unless graphql_describable?(node)
          return if resolver_kwarg(node) # Fields may inherit the description from their resolvers.

          description = locate_description(node)

          message = if description.nil?
                      MSG_NO_DESCRIPTION
                    elsif no_period?(description)
                      MSG_NO_PERIOD
                    elsif bad_start?(description)
                      MSG_BAD_START
                    end

          return unless message

          add_offense(node, message: message) do |corrector|
            description = locate_description(node)
            next unless description

            corrector.insert_after(before_end_quote(description), '.')
          end
        end

        private

        # 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

        def no_period?(description)
          string?(description) && !description.value.strip.end_with?('.')
        end

        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?

          heredoc_source = string.location.heredoc_body.source
          adjust = heredoc_source.index(/\s+\Z/) - heredoc_source.length
          string.location.heredoc_body.adjust(end_pos: adjust)
        end
      end
    end
  end
end