-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathobject_description.rb
64 lines (54 loc) · 1.57 KB
/
object_description.rb
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
# frozen_string_literal: true
module RuboCop
module Cop
module GraphQL
# This cop checks if a type (object, input, interface, scalar, union,
# mutation, subscription, and resolver) has a description.
#
# @example
# # good
#
# class Types::UserType < Types::BaseObject
# description "Represents application user"
# # ...
# end
#
# # bad
#
# class Types::UserType < Types::BaseObject
# # ...
# end
#
class ObjectDescription < Base
include RuboCop::GraphQL::NodePattern
include RuboCop::GraphQL::DescriptionMethod
MSG = "Missing type description"
# @!method interface?(node)
def_node_matcher :interface?, <<~PATTERN
(send nil? :include (const ...))
PATTERN
def on_class(node)
return if child_nodes(node).find { |child_node| has_description?(child_node) }
add_offense(node.identifier)
end
def on_module(node)
return if child_nodes(node).none? { |child_node| interface?(child_node) }
if child_nodes(node).none? { |child_node| has_description?(child_node) }
add_offense(node.identifier)
end
end
private
def has_description?(node)
description_method_call?(node)
end
def child_nodes(node)
if node.body.instance_of? RuboCop::AST::Node
node.body.child_nodes
else
node.child_nodes
end
end
end
end
end
end