rubocop-graphql 1.0.0 → 1.1.0

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: aec3ad39e39612f0b537f18b691a38d2e2a5efc9ec5b284b0fabf0d12521136e
4
- data.tar.gz: ba04fef687999fdb042a926c27346d89beff1fa59b827c4936b6f819cf5abe74
3
+ metadata.gz: 93b15c7311147740481d59f94cab5e62ca4317ada9ec0e21254ec785b5dfd683
4
+ data.tar.gz: 9157944e4d8a28635389fe2b1472c48361001a5674fb1c81ebd0dec4447b44d0
5
5
  SHA512:
6
- metadata.gz: f8094f6adb04e83515a0fd6307ddc3a255b17d7fdd8bf432ba84a77f6d4ba1921dbea3ba2739761b3103d8271d8bcab6b807c65f6479617eb8e116717e5861e0
7
- data.tar.gz: f9664a1f5fb19d71dcef3976525f955f8c515c0655d4f86203bfa72508baeb9a000576ec3aafc44ee9b61061f0cfc79b978e1cada637335a9d98d0dd0249aea5
6
+ metadata.gz: 9cd372736e4f994645dfaf72289ce5933a9e739f5ef179096326574b5da79d6c872ee01097232666d2549d8bd5f9c762dc3af0de6b66ce905a2a3d37814a3c9c
7
+ data.tar.gz: 181fd6b02b1bb4e20fc64f60e39ceb94073ce178be18b6d1bc6177e4d62cf933e2f6bd5ef4e23b4b8aa163a22ccd641ca4727647054f24bcce2882e390bfddd5
@@ -220,7 +220,7 @@ module RuboCop
220
220
 
221
221
  def remove_old_resolver(corrector, resolver_definition)
222
222
  range_to_remove = range_with_surrounding_space(
223
- range: resolver_definition.loc.expression, side: :left
223
+ range: resolver_definition.source_range, side: :left
224
224
  )
225
225
  corrector.remove(range_to_remove)
226
226
 
@@ -229,7 +229,7 @@ module RuboCop
229
229
  return unless resolver_signature
230
230
 
231
231
  range_to_remove = range_with_surrounding_space(
232
- range: resolver_signature.loc.expression, side: :left
232
+ range: resolver_signature.source_range, side: :left
233
233
  )
234
234
  corrector.remove(range_to_remove)
235
235
  end
@@ -74,11 +74,11 @@ module RuboCop
74
74
  suggested_hash_key_name = hash_key_to_use(method_definition)
75
75
 
76
76
  corrector.insert_after(
77
- node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}"
77
+ node.source_range, ", hash_key: #{suggested_hash_key_name.inspect}"
78
78
  )
79
79
 
80
80
  range = range_with_surrounding_space(
81
- range: method_definition.loc.expression, side: :left
81
+ range: method_definition.source_range, side: :left
82
82
  )
83
83
 
84
84
  corrector.remove(range)
@@ -68,10 +68,10 @@ module RuboCop
68
68
  method_definition = suggest_method_name_for(field)
69
69
  suggested_method_name = method_to_use(method_definition)
70
70
 
71
- corrector.insert_after(node.loc.expression, ", method: :#{suggested_method_name}")
71
+ corrector.insert_after(node.source_range, ", method: :#{suggested_method_name}")
72
72
 
73
73
  range = range_with_surrounding_space(
74
- range: method_definition.loc.expression, side: :left
74
+ range: method_definition.source_range, side: :left
75
75
  )
76
76
  corrector.remove(range)
77
77
  end
@@ -20,6 +20,20 @@ module RuboCop
20
20
  # end
21
21
  # end
22
22
  #
23
+ # # good
24
+ #
25
+ # class UserType < BaseType
26
+ # implements GraphQL::Types::Relay::Node
27
+ #
28
+ # field :uuid, ID, null: false
29
+ #
30
+ # class << self
31
+ # def authorized?(object, context)
32
+ # super && object.owner == context[:viewer]
33
+ # end
34
+ # end
35
+ # end
36
+ #
23
37
  # # bad
24
38
  #
25
39
  # class UserType < BaseType
@@ -42,7 +56,7 @@ module RuboCop
42
56
 
43
57
  # @!method has_authorized_method?(node)
44
58
  def_node_matcher :has_authorized_method?, <<~PATTERN
45
- `(:defs (:self) :authorized? ...)
59
+ {`(:defs (:self) :authorized? ...) | `(:sclass (:self) `(:def :authorized? ...))}
46
60
  PATTERN
47
61
 
48
62
  def on_class(node)
@@ -3,32 +3,48 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module GraphQL
6
- # This cop checks if a field has an unnecessary alias.
6
+ # This cop prevents defining an unnecessary alias, method, or resolver_method.
7
7
  #
8
8
  # @example
9
9
  # # good
10
10
  #
11
- # class UserType < BaseType
12
- # field :name, String, "Name of the user", null: true, alias: :real_name
13
- # end
11
+ # field :name, String, "Name of the user", null: true, alias: :real_name
12
+ # field :name, String, "Name of the user", null: true, method: :real_name
13
+ # field :name, String, "Name of the user", null: true, resolver_method: :real_name
14
+ # field :name, String, "Name of the user", null: true, hash_key: :real_name
14
15
  #
15
16
  # # bad
16
17
  #
17
- # class UserType < BaseType
18
- # field :name, "Name of the user" String, null: true, alias: :name
19
- # end
18
+ # field :name, "Name of the user" String, null: true, alias: :name
19
+ # field :name, String, "Name of the user", null: true, method: :name
20
+ # field :name, String, "Name of the user", null: true, resolver_method: :name
21
+ # field :name, String, "Name of the user", null: true, hash_key: :name
20
22
  #
21
23
  class UnnecessaryFieldAlias < Base
22
24
  include RuboCop::GraphQL::NodePattern
23
25
 
24
- MSG = "Unnecessary field alias"
26
+ MSG = "Unnecessary :%<kwarg>s configured"
25
27
 
26
28
  def on_send(node)
27
29
  return unless field_definition?(node)
28
30
 
29
31
  field = RuboCop::GraphQL::Field.new(node)
30
32
 
31
- add_offense(node) if field.name == field.kwargs.alias
33
+ if (unnecessary_kwarg = validate_kwargs(field))
34
+ message = format(self.class::MSG, kwarg: unnecessary_kwarg)
35
+ add_offense(node, message: message)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def validate_kwargs(field) # rubocop:disable Metrics/CyclomaticComplexity
42
+ case field.name
43
+ when field.kwargs.alias then "alias"
44
+ when field.kwargs.method&.value&.value then "method"
45
+ when field.kwargs.resolver_method_name then "resolver_method"
46
+ when field.kwargs.hash_key&.value&.value then "hash_key"
47
+ end
32
48
  end
33
49
  end
34
50
  end
@@ -144,7 +144,7 @@ module RuboCop
144
144
  end
145
145
 
146
146
  def arg_end(node)
147
- node.loc.expression.end
147
+ node.source_range.end
148
148
  end
149
149
 
150
150
  def inferred_arg_name(name_as_string)
@@ -84,7 +84,10 @@ module RuboCop
84
84
  end
85
85
 
86
86
  def root_node?(node)
87
- node.parent.nil? || node.class_type? || root_with_siblings?(node.parent)
87
+ node.parent.nil? ||
88
+ node.module_type? && node.children.none?(&:module_type?) ||
89
+ node.class_type? ||
90
+ root_with_siblings?(node.parent)
88
91
  end
89
92
 
90
93
  def root_with_siblings?(node)
@@ -13,7 +13,7 @@ module RuboCop
13
13
  heredoc?(kwarg.value)
14
14
  end&.value
15
15
 
16
- range = node.loc.expression
16
+ range = node.source_range
17
17
  range = range.join(last_heredoc.loc.heredoc_end) if last_heredoc
18
18
 
19
19
  range_by_whole_lines(range)
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "1.0.0".freeze
3
+ VERSION = "1.1.0".freeze
4
4
  end
5
5
  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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Tsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-26 00:00:00.000000000 Z
11
+ date: 2023-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler