rubocop-graphql 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/config/default.yml +7 -2
- data/lib/rubocop-graphql.rb +3 -0
- data/lib/rubocop/cop/graphql/field_definitions.rb +7 -21
- data/lib/rubocop/cop/graphql/field_method.rb +62 -0
- data/lib/rubocop/cop/graphql_cops.rb +1 -0
- data/lib/rubocop/graphql/field.rb +20 -30
- data/lib/rubocop/graphql/field_kwargs.rb +45 -0
- data/lib/rubocop/graphql/schema_member.rb +35 -0
- data/lib/rubocop/graphql/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03b458539789cb0aeaf7e8852d2c7eba08554f7ef1d3621bce63d7cb271899be
|
4
|
+
data.tar.gz: 16da0bb9da9cfdafd6da1d883890d8a96d6387519c7fba8cec883461d5d43130
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12208cd27b2711fb499331e26e0eead2064115d1f7f47dadcbb0ae218ad8d28fd852a5f4a0f1e3bfcecac6302205612b88f57ae65af89735ab9c454b4e3fbc4f
|
7
|
+
data.tar.gz: d3c8402557eb2b58f1c7a231d5b7a988d9bcfde59a8aa8b23c106f11cfe4ce6a193a1843165b296999bda3fbaa6d6f43560aff0082dd48ca6b18b0a0f5f3641c
|
data/config/default.yml
CHANGED
@@ -6,7 +6,7 @@ AllCops:
|
|
6
6
|
GraphQL/ResolverMethodLength:
|
7
7
|
Enabled: true
|
8
8
|
VersionAdded: '0.80'
|
9
|
-
Description: '
|
9
|
+
Description: 'Checks resolver methods are not too long'
|
10
10
|
Max: 10
|
11
11
|
CountComments: false
|
12
12
|
ExcludedMethods: []
|
@@ -23,4 +23,9 @@ GraphQL/FieldDefinitions:
|
|
23
23
|
GraphQL/FieldDescription:
|
24
24
|
Enabled: true
|
25
25
|
VersionAdded: '0.80'
|
26
|
-
Description: '
|
26
|
+
Description: 'Ensures all fields have a description'
|
27
|
+
|
28
|
+
GraphQL/FieldMethod:
|
29
|
+
Enabled: true
|
30
|
+
VersionAdded: '0.80'
|
31
|
+
Description: 'Checks :method option is used for appropriate fields'
|
data/lib/rubocop-graphql.rb
CHANGED
@@ -6,7 +6,10 @@ require_relative "rubocop/graphql"
|
|
6
6
|
require_relative "rubocop/graphql/version"
|
7
7
|
require_relative "rubocop/graphql/inject"
|
8
8
|
require_relative "rubocop/graphql/node_pattern"
|
9
|
+
|
9
10
|
require_relative "rubocop/graphql/field"
|
11
|
+
require_relative "rubocop/graphql/field_kwargs"
|
12
|
+
require_relative "rubocop/graphql/schema_member"
|
10
13
|
|
11
14
|
RuboCop::GraphQL::Inject.defaults!
|
12
15
|
|
@@ -57,10 +57,6 @@ module RuboCop
|
|
57
57
|
)
|
58
58
|
PATTERN
|
59
59
|
|
60
|
-
def_node_matcher :class_body, <<~PATTERN
|
61
|
-
(class ... (begin $...))
|
62
|
-
PATTERN
|
63
|
-
|
64
60
|
def on_send(node)
|
65
61
|
return if !field_definition?(node) || style != :define_resolver_after_definition
|
66
62
|
|
@@ -71,8 +67,11 @@ module RuboCop
|
|
71
67
|
def on_class(node)
|
72
68
|
return if style != :group_definitions
|
73
69
|
|
74
|
-
|
75
|
-
|
70
|
+
schema_member = RuboCop::GraphQL::SchemaMember.new(node)
|
71
|
+
|
72
|
+
if (body = schema_member.body)
|
73
|
+
check_grouped_field_declarations(body)
|
74
|
+
end
|
76
75
|
end
|
77
76
|
|
78
77
|
private
|
@@ -94,10 +93,9 @@ module RuboCop
|
|
94
93
|
RESOLVER_AFTER_FIELD_MSG = "Define resolver method after field definition."
|
95
94
|
|
96
95
|
def check_resolver_is_defined_after_definition(field)
|
97
|
-
return if field.resolver || field.method || field.hash_key
|
96
|
+
return if field.kwargs.resolver || field.kwargs.method || field.kwargs.hash_key
|
98
97
|
|
99
|
-
|
100
|
-
method_definition = find_method_definition(root, field.resolver_method_name)
|
98
|
+
method_definition = field.schema_member.find_method_definition(field.resolver_method_name)
|
101
99
|
return unless method_definition
|
102
100
|
|
103
101
|
field_sibling_index = if field_definition_with_body?(field.parent)
|
@@ -110,18 +108,6 @@ module RuboCop
|
|
110
108
|
|
111
109
|
add_offense(field.node, message: RESOLVER_AFTER_FIELD_MSG)
|
112
110
|
end
|
113
|
-
|
114
|
-
def find_method_definition(root, method_name)
|
115
|
-
class_body(root).find { |node| node.def_type? && node.method_name == method_name }
|
116
|
-
end
|
117
|
-
|
118
|
-
def root_node?(node)
|
119
|
-
node.parent.nil? || root_with_siblings?(node.parent)
|
120
|
-
end
|
121
|
-
|
122
|
-
def root_with_siblings?(node)
|
123
|
-
node.begin_type? && node.parent.nil?
|
124
|
-
end
|
125
111
|
end
|
126
112
|
end
|
127
113
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# This cop prevents defining unnecessary resolver methods in cases
|
7
|
+
# when :method option can be used
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # good
|
11
|
+
#
|
12
|
+
# class Types::UserType < Types::BaseObject
|
13
|
+
# field :phone, String, null: true, method: :home_phone
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # bad
|
17
|
+
#
|
18
|
+
# class Types::UserType < Types::BaseObject
|
19
|
+
# field :phone, String, null: true
|
20
|
+
#
|
21
|
+
# def phone
|
22
|
+
# object.home_phone
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
class FieldMethod < Cop
|
27
|
+
include RuboCop::GraphQL::NodePattern
|
28
|
+
|
29
|
+
def_node_matcher :method_to_use, <<~PATTERN
|
30
|
+
(def
|
31
|
+
_
|
32
|
+
(args)
|
33
|
+
(send
|
34
|
+
(send nil? :object) $_
|
35
|
+
)
|
36
|
+
)
|
37
|
+
PATTERN
|
38
|
+
|
39
|
+
MSG = "Use method: :%<method_name>s"
|
40
|
+
|
41
|
+
def on_send(node)
|
42
|
+
return unless field_definition?(node)
|
43
|
+
|
44
|
+
field = RuboCop::GraphQL::Field.new(node)
|
45
|
+
|
46
|
+
method_name = field.resolver_method_name
|
47
|
+
method_definition = field.schema_member.find_method_definition(method_name)
|
48
|
+
|
49
|
+
if (method_name = method_to_use(method_definition))
|
50
|
+
add_offense(node, message: message(method_name))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def message(method_name)
|
57
|
+
format(MSG, method_name: method_name)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -6,7 +6,7 @@ module RuboCop
|
|
6
6
|
extend Forwardable
|
7
7
|
extend RuboCop::NodePattern::Macros
|
8
8
|
|
9
|
-
def_delegators :@node, :sibling_index, :parent
|
9
|
+
def_delegators :@node, :sibling_index, :parent
|
10
10
|
|
11
11
|
def_node_matcher :field_kwargs, <<~PATTERN
|
12
12
|
(send nil? :field
|
@@ -18,27 +18,11 @@ module RuboCop
|
|
18
18
|
PATTERN
|
19
19
|
|
20
20
|
def_node_matcher :field_name, <<~PATTERN
|
21
|
-
(send nil? :field (:sym
|
21
|
+
(send nil? :field (:sym $_) ...)
|
22
22
|
PATTERN
|
23
23
|
|
24
24
|
def_node_matcher :field_description, <<~PATTERN
|
25
|
-
(send nil? :field _ _ (:str
|
26
|
-
PATTERN
|
27
|
-
|
28
|
-
def_node_matcher :resolver_kwarg?, <<~PATTERN
|
29
|
-
(pair (sym :resolver) ...)
|
30
|
-
PATTERN
|
31
|
-
|
32
|
-
def_node_matcher :method_kwarg?, <<~PATTERN
|
33
|
-
(pair (sym :method) ...)
|
34
|
-
PATTERN
|
35
|
-
|
36
|
-
def_node_matcher :hash_key_kwarg?, <<~PATTERN
|
37
|
-
(pair (sym :hash_key) ...)
|
38
|
-
PATTERN
|
39
|
-
|
40
|
-
def_node_matcher :resolver_method_option, <<~PATTERN
|
41
|
-
(pair (sym :resolver_method) (sym $...))
|
25
|
+
(send nil? :field _ _ (:str $_) ...)
|
42
26
|
PATTERN
|
43
27
|
|
44
28
|
attr_reader :node
|
@@ -48,31 +32,37 @@ module RuboCop
|
|
48
32
|
end
|
49
33
|
|
50
34
|
def name
|
51
|
-
|
35
|
+
field_name(@node)
|
52
36
|
end
|
53
37
|
|
54
38
|
def description
|
55
|
-
|
39
|
+
field_description(@node)
|
40
|
+
end
|
41
|
+
|
42
|
+
def resolver_method_name
|
43
|
+
kwargs.resolver_method_name || name
|
56
44
|
end
|
57
45
|
|
58
46
|
def kwargs
|
59
|
-
@kwargs ||= field_kwargs(@node)
|
47
|
+
@kwargs ||= FieldKwargs.new(field_kwargs(@node))
|
60
48
|
end
|
61
49
|
|
62
|
-
def
|
63
|
-
|
50
|
+
def schema_member
|
51
|
+
@schema_member ||= SchemaMember.new(root_node)
|
64
52
|
end
|
65
53
|
|
66
|
-
|
67
|
-
|
54
|
+
private
|
55
|
+
|
56
|
+
def root_node
|
57
|
+
@node.ancestors.find { |parent| root_node?(parent) }
|
68
58
|
end
|
69
59
|
|
70
|
-
def
|
71
|
-
|
60
|
+
def root_node?(node)
|
61
|
+
node.parent.nil? || root_with_siblings?(node.parent)
|
72
62
|
end
|
73
63
|
|
74
|
-
def
|
75
|
-
|
64
|
+
def root_with_siblings?(node)
|
65
|
+
node.begin_type? && node.parent.nil?
|
76
66
|
end
|
77
67
|
end
|
78
68
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module GraphQL
|
5
|
+
class FieldKwargs
|
6
|
+
extend RuboCop::NodePattern::Macros
|
7
|
+
|
8
|
+
def_node_matcher :resolver_kwarg?, <<~PATTERN
|
9
|
+
(pair (sym :resolver) ...)
|
10
|
+
PATTERN
|
11
|
+
|
12
|
+
def_node_matcher :method_kwarg?, <<~PATTERN
|
13
|
+
(pair (sym :method) ...)
|
14
|
+
PATTERN
|
15
|
+
|
16
|
+
def_node_matcher :hash_key_kwarg?, <<~PATTERN
|
17
|
+
(pair (sym :hash_key) ...)
|
18
|
+
PATTERN
|
19
|
+
|
20
|
+
def_node_matcher :resolver_method_option, <<~PATTERN
|
21
|
+
(pair (sym :resolver_method) (sym $...))
|
22
|
+
PATTERN
|
23
|
+
|
24
|
+
def initialize(nodes)
|
25
|
+
@nodes = nodes
|
26
|
+
end
|
27
|
+
|
28
|
+
def resolver
|
29
|
+
@nodes.find { |kwarg| resolver_kwarg?(kwarg) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def method
|
33
|
+
@nodes.find { |kwarg| method_kwarg?(kwarg) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def hash_key
|
37
|
+
@nodes.find { |kwarg| hash_key_kwarg?(kwarg) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def resolver_method_name
|
41
|
+
@resolver_method_name ||= @nodes.flat_map { |kwarg| resolver_method_option(kwarg) }.compact.first
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module GraphQL
|
5
|
+
class SchemaMember
|
6
|
+
extend RuboCop::NodePattern::Macros
|
7
|
+
|
8
|
+
def_node_matcher :class_contents, <<~PATTERN
|
9
|
+
(class _ _ $_)
|
10
|
+
PATTERN
|
11
|
+
|
12
|
+
attr_reader :node
|
13
|
+
|
14
|
+
def initialize(node)
|
15
|
+
@node = node
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_method_definition(method_name)
|
19
|
+
body.find { |node| node.def_type? && node.method_name == method_name }
|
20
|
+
end
|
21
|
+
|
22
|
+
def body
|
23
|
+
contents = class_contents(@node)
|
24
|
+
|
25
|
+
if contents.nil?
|
26
|
+
[]
|
27
|
+
elsif contents.begin_type?
|
28
|
+
contents.child_nodes
|
29
|
+
else
|
30
|
+
[contents]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-graphql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Tsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,12 +80,15 @@ files:
|
|
80
80
|
- lib/rubocop/cop/graphql/cop.rb
|
81
81
|
- lib/rubocop/cop/graphql/field_definitions.rb
|
82
82
|
- lib/rubocop/cop/graphql/field_description.rb
|
83
|
+
- lib/rubocop/cop/graphql/field_method.rb
|
83
84
|
- lib/rubocop/cop/graphql/resolver_method_length.rb
|
84
85
|
- lib/rubocop/cop/graphql_cops.rb
|
85
86
|
- lib/rubocop/graphql.rb
|
86
87
|
- lib/rubocop/graphql/field.rb
|
88
|
+
- lib/rubocop/graphql/field_kwargs.rb
|
87
89
|
- lib/rubocop/graphql/inject.rb
|
88
90
|
- lib/rubocop/graphql/node_pattern.rb
|
91
|
+
- lib/rubocop/graphql/schema_member.rb
|
89
92
|
- lib/rubocop/graphql/version.rb
|
90
93
|
homepage: https://github.com/DmitryTsepelev/rubocop-graphql
|
91
94
|
licenses:
|