rubocop-graphql 1.8.0 → 1.9.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: f7108c819a363b06725b9de204f6e1eb26a19c0e022d07a51f7cac3570ccc041
4
- data.tar.gz: 5b5b231155cf6ac19e58df7ab0ded5060517f0a59ed5b3bcef77db04adf9355a
3
+ metadata.gz: 3c1e7c4af09f89eaeec1b7f744a8bb16ed5ccd185d3a71ede88805c433d0941a
4
+ data.tar.gz: 92e2ee950b4e0064a07cc93a340916b82a33e030ea6375b22a81fa9e456a8908
5
5
  SHA512:
6
- metadata.gz: 2bd922b4a7234a4ca1315dc42fd6b3adf7fff850bce9ec71abf41df175fd56889cd76be254dcdb4bbec09615dc85a8317a87884778fc62797ab991f63eee04bf
7
- data.tar.gz: 282acda7b0c3f54e51a7e77245964e55d3d9d0847009490bfa096959cb49d5de72536e0e49b94f148dbfe1c72c40877494ce8edc9d5187c9b8eacdcf33be6129
6
+ metadata.gz: b6b54618340dc27b21f79907d0346d2d121d09a2dd0a195c4a48f5127cfdf2e7aa146c829364c51ec67ad6e587df64aab4e43c261611d02ed5918a27583f0fe1
7
+ data.tar.gz: 72675db7e637d72eebf96ff9199d4e2e79fb150cb334177c238292b9da67ae21e9490d8970bdd2c315284030ba2547ffdc6b977d44505a84c60c20f53bf7f17a
data/config/default.yml CHANGED
@@ -173,6 +173,10 @@ GraphQL/ObjectDescription:
173
173
  # conventional ones. Use when your base types are named unconventionally, e.g.
174
174
  # `class Types::UserType < ApplicationType`.
175
175
  AdditionalTypeBases: []
176
+ # Suffixes to treat as GraphQL bases, on top of Mutation, Subscription and
177
+ # Resolver. Use when your base types share a suffix that is ambiguous by default,
178
+ # e.g. `Object` for `class Types::UserType < PermissionedObject`.
179
+ AdditionalTypeBaseSuffixes: []
176
180
  Exclude:
177
181
  - "spec/**/*"
178
182
  - "test/**/*"
@@ -84,13 +84,19 @@ module RuboCop
84
84
 
85
85
  return field_name(node.parent) unless is_field_block
86
86
 
87
- first_argument = node.send_node.first_argument
88
- if first_argument.lvar_type?
89
- variable_name = first_argument.children.first
90
- return "with name from `#{variable_name}` variable"
91
- end
87
+ describe_field_name(node.send_node.first_argument)
88
+ end
92
89
 
93
- "`#{first_argument.value}`"
90
+ def describe_field_name(name_node)
91
+ if name_node.nil?
92
+ "without a name"
93
+ elsif name_node.type?(:sym, :str)
94
+ "`#{name_node.value}`"
95
+ elsif name_node.lvar_type?
96
+ "with name from `#{name_node.children.first}` variable"
97
+ else
98
+ "with name from `#{name_node.source}` expression"
99
+ end
94
100
  end
95
101
 
96
102
  # @!method argument_declarations(node)
@@ -106,11 +106,27 @@ module RuboCop
106
106
  next if node.sibling_index == first_field.sibling_index + idx
107
107
 
108
108
  add_offense(node, message: GROUP_DEFS_MSG) do |corrector|
109
+ next if references_constant_defined_in_between?(body, first_field, node)
110
+
109
111
  group_field_declarations(corrector, node)
110
112
  end
111
113
  end
112
114
  end
113
115
 
116
+ def references_constant_defined_in_between?(body, first_field, node)
117
+ crossed = body[(first_field.sibling_index + 1)...node.sibling_index]
118
+ defined = crossed.filter_map { |sibling| defined_constant_name(sibling) }
119
+
120
+ node.each_node(:const).any? { |const| defined.include?(const.short_name) }
121
+ end
122
+
123
+ def defined_constant_name(node)
124
+ case node.type
125
+ when :class, :module then node.identifier.short_name
126
+ when :casgn then node.name
127
+ end
128
+ end
129
+
114
130
  def group_field_declarations(corrector, node)
115
131
  field = RuboCop::GraphQL::Field.new(node)
116
132
  first_field = field.schema_member.body.find do |node|
@@ -213,7 +229,7 @@ module RuboCop
213
229
  end
214
230
 
215
231
  def insert_new_resolver(corrector, field_definition, resolver_definition)
216
- resolver_definition_range = source_range_with_comment(resolver_definition)
232
+ resolver_definition_range = source_range_with_comment_and_heredoc(resolver_definition)
217
233
  source_to_insert =
218
234
  "\n#{signature_to_insert(resolver_definition)}\n" \
219
235
  "#{indent(resolver_definition)}#{resolver_definition_range.source.strip}\n"
@@ -223,7 +239,7 @@ module RuboCop
223
239
  end
224
240
 
225
241
  def remove_old_resolver(corrector, resolver_definition)
226
- resolver_definition_range = source_range_with_comment(resolver_definition)
242
+ resolver_definition_range = source_range_with_comment_and_heredoc(resolver_definition)
227
243
  range_to_remove = range_with_surrounding_space(
228
244
  range: resolver_definition_range, side: :left
229
245
  )
@@ -240,6 +256,15 @@ module RuboCop
240
256
  corrector.remove(range_to_remove)
241
257
  end
242
258
 
259
+ # A node's own range stops at the opening `<<~TAG` of a heredoc, so a field with a
260
+ # heredoc argument must be widened to take the heredoc body and terminator along.
261
+ def source_range_with_comment_and_heredoc(node)
262
+ range = source_range_with_comment(node)
263
+ heredoc_end_pos = range_including_heredoc(node).end_pos
264
+
265
+ heredoc_end_pos > range.end_pos ? range.with(end_pos: heredoc_end_pos) : range
266
+ end
267
+
243
268
  def signature_to_insert(node)
244
269
  signature = sorbet_signature_for(node)
245
270
 
@@ -75,7 +75,7 @@ module RuboCop
75
75
  suggested_hash_key_name = hash_key_to_use(method_definition)
76
76
 
77
77
  corrector.insert_after(
78
- node, ", hash_key: #{suggested_hash_key_name.inspect}"
78
+ field.last_non_block_argument, ", hash_key: #{suggested_hash_key_name.inspect}"
79
79
  )
80
80
 
81
81
  range = range_with_surrounding_space(
@@ -70,7 +70,9 @@ module RuboCop
70
70
  method_definition = suggest_method_name_for(field)
71
71
  suggested_method_name = method_to_use(method_definition)
72
72
 
73
- corrector.insert_after(node, ", method: :#{suggested_method_name}")
73
+ corrector.insert_after(
74
+ field.last_non_block_argument, ", method: :#{suggested_method_name}"
75
+ )
74
76
 
75
77
  range = range_with_surrounding_space(
76
78
  range: method_definition.source_range, side: :left
@@ -116,10 +116,11 @@ module RuboCop
116
116
  end
117
117
 
118
118
  def possible_parent_classes(node)
119
- klass = node.child_nodes[1].const_name
119
+ parent_class = node.parent_class
120
+ klass = parent_class&.const_name
120
121
 
121
122
  return [] if klass.nil?
122
- return [klass] if node.child_nodes[1].absolute?
123
+ return [klass] if parent_class.absolute?
123
124
 
124
125
  parent_module = "#{@parent_modules.join('::')}::"
125
126
  [klass, parent_module + klass]
@@ -58,6 +58,20 @@ module RuboCop
58
58
  # class Types::UserType < ApplicationType
59
59
  # end
60
60
  #
61
+ # @example AdditionalTypeBaseSuffixes: [] (default)
62
+ # # good - only `Object` exactly, or with a `Base` prefix, is a recognized
63
+ # # object base, so a plain Ruby `ValueObject` is not mistaken for one
64
+ #
65
+ # class Money < ValueObject
66
+ # end
67
+ #
68
+ # @example AdditionalTypeBaseSuffixes: ['Object']
69
+ # # bad - every base whose name ends in `Object` is now treated as a GraphQL
70
+ # # base, which suits an app whose bases carry a domain prefix
71
+ #
72
+ # class Types::UserType < PermissionedObject
73
+ # end
74
+ #
61
75
  class ObjectDescription < Base
62
76
  include RuboCop::GraphQL::DescriptionMethod
63
77
 
@@ -66,13 +80,15 @@ module RuboCop
66
80
  # Base class names that mark a GraphQL type when they match exactly, or with
67
81
  # a `Base` prefix (`GraphQL::Schema::Object`, `Types::BaseObject`).
68
82
  # Ambiguous words are deliberately not matched as suffixes, so a plain Ruby
69
- # `ValueObject` base is not mistaken for a GraphQL one. `Interface` is absent
70
- # on purpose: graphql-ruby interfaces are modules, so a *class* inheriting an
71
- # `*::Interface` constant is always some other abstract base.
83
+ # `ValueObject` base is not mistaken for a GraphQL one; an app whose own bases
84
+ # do carry such a suffix opts in via `AdditionalTypeBaseSuffixes`. `Interface`
85
+ # is absent on purpose: graphql-ruby interfaces are modules, so a *class*
86
+ # inheriting an `*::Interface` constant is always some other abstract base.
72
87
  EXACT_BASES = %w[Object InputObject Union Enum Scalar].freeze
73
88
 
74
89
  # These read unambiguously as GraphQL even inside a longer name, so they are
75
90
  # matched as suffixes (`RelayClassicMutation`, `Base::PermissionedMutation`).
91
+ # `AdditionalTypeBaseSuffixes` extends this list per app.
76
92
  SUFFIX_BASES = %w[Mutation Subscription Resolver].freeze
77
93
 
78
94
  # A base named exactly `Base` (`Resolvers::Base`) is only a GraphQL base when
@@ -128,16 +144,21 @@ module RuboCop
128
144
  def graphql_type_class?(node)
129
145
  superclass = node.parent_class
130
146
  return false if superclass.nil? || !superclass.const_type?
131
- return false if superclass.namespace&.short_name == SORBET_NAMESPACE
147
+ return false if sorbet_namespace?(superclass.namespace)
132
148
 
133
149
  graphql_base?(superclass)
134
150
  end
135
151
 
152
+ # `namespace` is a `cbase` node (not a `const`) for a top-level `::Foo`.
153
+ def sorbet_namespace?(namespace)
154
+ namespace&.const_type? && namespace.short_name == SORBET_NAMESPACE
155
+ end
156
+
136
157
  def graphql_base?(const_node)
137
158
  name = const_node.short_name.to_s
138
159
  return true if additional_type_bases.include?(name)
139
160
  return graphql_namespace?(const_node) if name == "Base"
140
- return true if SUFFIX_BASES.any? { |suffix| name.end_with?(suffix) }
161
+ return true if name.end_with?(*SUFFIX_BASES, *additional_type_base_suffixes)
141
162
 
142
163
  EXACT_BASES.include?(name.delete_prefix("Base"))
143
164
  end
@@ -176,7 +197,9 @@ module RuboCop
176
197
  return false unless arg&.const_type?
177
198
 
178
199
  name = arg.short_name.to_s
179
- name.end_with?("Interface") || additional_type_bases.include?(name)
200
+ return true if additional_type_bases.include?(name)
201
+
202
+ name.end_with?("Interface", *additional_type_base_suffixes)
180
203
  end
181
204
 
182
205
  # Single pass over the class body: a `description` satisfies the cop, and a
@@ -212,6 +235,10 @@ module RuboCop
212
235
  def additional_type_bases
213
236
  @additional_type_bases ||= Array(cop_config["AdditionalTypeBases"])
214
237
  end
238
+
239
+ def additional_type_base_suffixes
240
+ @additional_type_base_suffixes ||= Array(cop_config["AdditionalTypeBaseSuffixes"])
241
+ end
215
242
  end
216
243
  end
217
244
  end
@@ -40,8 +40,8 @@ module RuboCop
40
40
  "section. "\
41
41
  "Field `%<current>s` should appear before `%<previous>s`."
42
42
 
43
- # @!method field_declarations(node)
44
- def_node_search :field_declarations, <<~PATTERN
43
+ # @!method field_nodes(node)
44
+ def_node_search :field_nodes, <<~PATTERN
45
45
  {
46
46
  (send nil? :field (:sym _) ...)
47
47
  (block
@@ -77,6 +77,14 @@ module RuboCop
77
77
  end
78
78
  end
79
79
 
80
+ def field_declarations(node)
81
+ field_nodes(node).reject { |field| block_field_send?(field) }
82
+ end
83
+
84
+ def block_field_send?(node)
85
+ node.parent&.block_type? && node.parent.send_node == node
86
+ end
87
+
80
88
  def direct_field_declarations(node)
81
89
  field_declarations(node).select { |field| direct_child_field?(field, node) }
82
90
  end
@@ -22,6 +22,8 @@ module RuboCop
22
22
  #
23
23
  class UnnecessaryFieldAlias < Base
24
24
  extend AutoCorrector
25
+ include RangeHelp
26
+ include RuboCop::GraphQL::Heredoc
25
27
  include RuboCop::GraphQL::NodePattern
26
28
 
27
29
  MSG = "Unnecessary :%<kwarg>s configured"
@@ -35,25 +37,80 @@ module RuboCop
35
37
  if (unnecessary_kwarg = validate_kwargs(field))
36
38
  message = format(self.class::MSG, kwarg: unnecessary_kwarg)
37
39
  add_offense(node, message: message) do |corrector|
40
+ next if node.each_descendant(:str, :dstr).any? { |str| heredoc?(str) }
41
+
38
42
  kwarg_node = node.last_argument.pairs.find do |pair|
39
43
  pair.key.value == unnecessary_kwarg.to_sym
40
44
  end
41
- corrector.remove_preceding(kwarg_node, 2)
42
- corrector.remove(kwarg_node)
45
+ remove_pair(corrector, kwarg_node)
43
46
  end
44
47
  end
45
48
  end
46
49
 
47
50
  private
48
51
 
49
- def validate_kwargs(field) # rubocop:disable Metrics/CyclomaticComplexity
52
+ def remove_pair(corrector, pair)
53
+ comment = comment_before_last_pair(pair)
54
+ comma = comma_before(comment) if comment
55
+
56
+ if comma
57
+ replaced = range_between(comma.begin_pos, end_of_line(pair))
58
+ corrector.replace(replaced, "#{code_after(pair)} #{comment.source}")
59
+ else
60
+ corrector.remove(range_to_remove(pair))
61
+ end
62
+ end
63
+
64
+ def range_to_remove(pair)
65
+ if (next_pair = pair.right_sibling)
66
+ range_between(pair.source_range.begin_pos, next_pair.source_range.begin_pos)
67
+ else
68
+ previous = pair.left_sibling || pair.parent.left_sibling
69
+ range_between(previous.source_range.end_pos, pair.source_range.end_pos)
70
+ end
71
+ end
72
+
73
+ def comment_before_last_pair(pair)
74
+ return if pair.right_sibling
75
+
76
+ previous = pair.left_sibling || pair.parent.left_sibling
77
+ comment = processed_source.comment_at_line(previous.last_line)
78
+ comment if comment && comment.source_range.end_pos <= pair.source_range.begin_pos
79
+ end
80
+
81
+ def code_after(pair)
82
+ own_comment = processed_source.comment_at_line(pair.last_line)
83
+ stop = own_comment ? own_comment.source_range.begin_pos : end_of_line(pair)
84
+
85
+ range_between(pair.source_range.end_pos, stop).source.rstrip
86
+ end
87
+
88
+ def end_of_line(node)
89
+ processed_source.buffer.line_range(node.last_line).end_pos
90
+ end
91
+
92
+ def comma_before(comment)
93
+ comment_begin = comment.source_range.begin_pos
94
+ line_begin = comment_begin - comment.loc.column
95
+ offset = processed_source.buffer.source[line_begin...comment_begin].rindex(/,\s*\z/)
96
+ return unless offset
97
+
98
+ range_between(line_begin + offset, line_begin + offset + 1)
99
+ end
100
+
101
+ def validate_kwargs(field)
50
102
  case field.name
51
103
  when field.kwargs.alias then "alias"
52
- when field.kwargs.method&.value&.value then "method"
104
+ when symbol_value(field.kwargs.method) then "method"
53
105
  when field.kwargs.resolver_method_name then "resolver_method"
54
- when field.kwargs.hash_key&.value&.value then "hash_key"
106
+ when symbol_value(field.kwargs.hash_key) then "hash_key"
55
107
  end
56
108
  end
109
+
110
+ def symbol_value(kwarg)
111
+ value = kwarg&.value
112
+ value.value if value&.sym_type?
113
+ end
57
114
  end
58
115
  end
59
116
  end
@@ -7,7 +7,7 @@ module RuboCop
7
7
 
8
8
  # @!method argument_description(node)
9
9
  def_node_matcher :argument_description, <<~PATTERN
10
- (send nil? :argument _ _ (:str $_) ...)
10
+ (send nil? :argument _ _ {(:str $_)|(:dstr $...)} ...)
11
11
  PATTERN
12
12
 
13
13
  # @!method argument_name(node)
@@ -73,6 +73,10 @@ module RuboCop
73
73
  @block ||= Field::Block.new(@node.parent)
74
74
  end
75
75
 
76
+ def last_non_block_argument
77
+ @node.arguments.reject(&:block_pass_type?).last
78
+ end
79
+
76
80
  def schema_member
77
81
  @schema_member ||= SchemaMember.new(root_node)
78
82
  end
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "1.8.0".freeze
3
+ VERSION = "1.9.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,28 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Tsepelev
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-08-20 00:00:00.000000000 Z
10
+ date: 2026-11-07 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: bundler
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '2.0'
19
- type: :development
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '2.0'
26
12
  - !ruby/object:Gem::Dependency
27
13
  name: rake
28
14
  requirement: !ruby/object:Gem::Requirement