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

graphql_name_position.rb « graphql « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b876fb5b08859a34ee1e3e6236df28be3f5f12a5 (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
# frozen_string_literal: true

# This cop ensures that if a class uses `graphql_name`, then
# it's the first line of the class
#
# @example
#
#   # bad
#   class AwfulClass
#     field :some_field, GraphQL::Types::JSON
#     graphql_name 'AwfulClass'
#   end
#
#   # good
#   class GreatClass
#     graphql_name 'AwfulClass'
#     field :some_field, GraphQL::Types::String
#   end

module RuboCop
  module Cop
    module Graphql
      class GraphqlNamePosition < RuboCop::Cop::Base
        MSG = '`graphql_name` should be the first line of the class: '\
              'https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#naming-conventions'

        def_node_search :graphql_name?, <<~PATTERN
          (send nil? :graphql_name ...)
        PATTERN

        def on_class(node)
          return unless graphql_name?(node)
          return if node.body.single_line?

          add_offense(node) unless graphql_name?(node.body.children.first)
        end
      end
    end
  end
end