rubocop-graphql 0.1.1 → 0.1.2
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30e4dfa482d073cf52c315042d736a54ce725491b3dcf130c9fecd3987bed8d2
|
4
|
+
data.tar.gz: 4e2467eab2c0ae0be138f6acf3b21912ad888c7b3cbcad98c2c7b0da3d9579af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6099e724a272914bac6212d07ad92256316db2874b26edcd2daf9e8155668f5ddb6b394221b740e8ed51273b49eb101cd83cba55c59352a6c2cd178e972afb9b
|
7
|
+
data.tar.gz: 80f3d7cfa9e10f209e3f8d2cbf373ac370dc738da62e05b704f37cb8aaccd41216aed6294f063092d26c606645877b3bc0c2aba77bfabf6b5590b79d31e6928f
|
@@ -57,30 +57,33 @@ module RuboCop
|
|
57
57
|
)
|
58
58
|
PATTERN
|
59
59
|
|
60
|
-
def_node_matcher :
|
61
|
-
(
|
60
|
+
def_node_matcher :class_body, <<~PATTERN
|
61
|
+
(class ... (begin $...))
|
62
62
|
PATTERN
|
63
63
|
|
64
64
|
def on_send(node)
|
65
|
-
return
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
65
|
+
return if !field_definition?(node) || style != :define_resolver_after_definition
|
66
|
+
|
67
|
+
field = RuboCop::GraphQL::Field.new(node)
|
68
|
+
check_resolver_is_defined_after_definition(field)
|
69
|
+
end
|
70
|
+
|
71
|
+
def on_class(node)
|
72
|
+
return if style != :group_definitions
|
73
|
+
|
74
|
+
body = class_body(node)
|
75
|
+
check_grouped_field_declarations(body) if body
|
74
76
|
end
|
75
77
|
|
76
78
|
private
|
77
79
|
|
78
80
|
GROUP_DEFS_MSG = "Group all field definitions together."
|
79
81
|
|
80
|
-
def check_grouped_field_declarations(
|
81
|
-
fields =
|
82
|
+
def check_grouped_field_declarations(body)
|
83
|
+
fields = body.select { |node| field_definition?(node) || field_definition_with_body?(node) }
|
82
84
|
|
83
85
|
first_field = fields.first
|
86
|
+
|
84
87
|
fields.each_with_index do |node, idx|
|
85
88
|
next if node.sibling_index == first_field.sibling_index + idx
|
86
89
|
|
@@ -93,16 +96,31 @@ module RuboCop
|
|
93
96
|
def check_resolver_is_defined_after_definition(field)
|
94
97
|
return if field.resolver || field.method || field.hash_key
|
95
98
|
|
96
|
-
|
99
|
+
root = field.ancestors.find { |parent| root_node?(parent) }
|
100
|
+
method_definition = find_method_definition(root, field.resolver_method_name)
|
101
|
+
return unless method_definition
|
97
102
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
if method_definition.sibling_index - field.sibling_index > 1
|
104
|
-
add_offense(field.node, message: RESOLVER_AFTER_FIELD_MSG)
|
103
|
+
field_sibling_index = if field_definition_with_body?(field.parent)
|
104
|
+
field.parent.sibling_index
|
105
|
+
else
|
106
|
+
field.sibling_index
|
105
107
|
end
|
108
|
+
|
109
|
+
return if method_definition.sibling_index - field_sibling_index == 1
|
110
|
+
|
111
|
+
add_offense(field.node, message: RESOLVER_AFTER_FIELD_MSG)
|
112
|
+
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?
|
106
124
|
end
|
107
125
|
end
|
108
126
|
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, :ancestors
|
10
10
|
|
11
11
|
def_node_matcher :field_kwargs, <<~PATTERN
|
12
12
|
(send nil? :field
|
@@ -37,6 +37,10 @@ module RuboCop
|
|
37
37
|
(pair (sym :hash_key) ...)
|
38
38
|
PATTERN
|
39
39
|
|
40
|
+
def_node_matcher :resolver_method_option, <<~PATTERN
|
41
|
+
(pair (sym :resolver_method) (sym $...))
|
42
|
+
PATTERN
|
43
|
+
|
40
44
|
attr_reader :node
|
41
45
|
|
42
46
|
def initialize(node)
|
@@ -66,6 +70,10 @@ module RuboCop
|
|
66
70
|
def hash_key
|
67
71
|
kwargs.find { |kwarg| hash_key_kwarg?(kwarg) }
|
68
72
|
end
|
73
|
+
|
74
|
+
def resolver_method_name
|
75
|
+
@resolver_method_name ||= kwargs.flat_map { |kwarg| resolver_method_option(kwarg) }.compact.first || name
|
76
|
+
end
|
69
77
|
end
|
70
78
|
end
|
71
79
|
end
|