rubocop-graphql 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75ede967be105c3771fc0c6c648632bf3c8fc49c1e81bb1f89d8b475c0403555
4
- data.tar.gz: ec1ced3e202cac94d70c24ecf890c63c0c7cca819edd623476bf7e831083bd1b
3
+ metadata.gz: 30e4dfa482d073cf52c315042d736a54ce725491b3dcf130c9fecd3987bed8d2
4
+ data.tar.gz: 4e2467eab2c0ae0be138f6acf3b21912ad888c7b3cbcad98c2c7b0da3d9579af
5
5
  SHA512:
6
- metadata.gz: ccca643eb811b6c25a4b8f2918b7c094a943b8c3a477d9c4b17c8e0bb4e75fa0d11b167c652790619e356716f6bc6c501446334c6f4d65f4ea75ffa4da2dfa3a
7
- data.tar.gz: 62d6ec1ae491c25c1c35b3c8a2d90fbec5a938034bbbc8d4abb7a38c3a7a50f2f359433f845e308f2e5257d9614ff7d3c43a403d56a34c61e575738eee2433ff
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 :resolver_method_option, <<~PATTERN
61
- (pair (sym :resolver_method) (sym $...))
60
+ def_node_matcher :class_body, <<~PATTERN
61
+ (class ... (begin $...))
62
62
  PATTERN
63
63
 
64
64
  def on_send(node)
65
- return unless field_definition?(node)
66
-
67
- case style
68
- when :group_definitions
69
- check_grouped_field_declarations(node.parent)
70
- when :define_resolver_after_definition
71
- field = RuboCop::GraphQL::Field.new(node)
72
- check_resolver_is_defined_after_definition(field)
73
- end
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(node)
81
- fields = node.each_child_node.select { |node| field_definition?(node) }
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
- resolver_method = field.kwargs.flat_map { |kwarg| resolver_method_option(kwarg) }.compact.first
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
- method_name = resolver_method || field.name
99
- method_definition = field.parent.each_child_node.find { |node|
100
- node.def_type? && node.method_name == method_name
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
@@ -8,6 +8,13 @@ module RuboCop
8
8
  def_node_matcher :field_definition?, <<~PATTERN
9
9
  (send nil? :field ...)
10
10
  PATTERN
11
+
12
+ def_node_matcher :field_definition_with_body?, <<~PATTERN
13
+ (block
14
+ (send nil? :field ...)
15
+ ...
16
+ )
17
+ PATTERN
11
18
  end
12
19
  end
13
20
  end
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Tsepelev