Class: GraphQL::Modernize::Rules::RemovedFieldOptions

Inherits:
Base
  • Object
show all
Defined in:
lib/graphql/modernize/rules/legacy.rb

Constant Summary collapse

TARGETS =
%i[field function].freeze

Instance Attribute Summary

Attributes inherited from Base

#offenses

Instance Method Summary collapse

Methods inherited from Base

#initialize, node_pattern, node_types, rule, #visit

Constructor Details

This class inherits a constructor from GraphQL::Modernize::Rules::Base

Instance Method Details

#on_match(node, _captures) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/graphql/modernize/rules/legacy.rb', line 269

def on_match(node, _captures)
  return unless inside?(node, :object, :interface)

  keyword_hash = node.arguments&.arguments&.find { |argument| argument.is_a?(Prism::KeywordHashNode) }
  return unless keyword_hash

  arguments = keyword_hash.elements.select do |argument|
    argument.is_a?(Prism::AssocNode) && argument.key.is_a?(Prism::SymbolNode) &&
      TARGETS.include?(argument.key.unescaped.to_sym)
  end
  return if arguments.empty?

  unless arguments.all? { |argument| safe_to_discard?(argument.value) } && !dynamic_keyword_arguments?(node)
    message = "Remove `field:` and `function:` manually; preserve side effects"
    return add_offense(arguments.first.location, safety: :manual, message: message)
  end

  add_offense(arguments.first.location, message: "Remove the deleted `field:` and `function:` options") do |rw|
    if arguments.length == keyword_hash.elements.length
      if node.arguments.arguments.one? && !node.opening_loc
        rw.remove(node.message_loc.end_offset...keyword_hash.location.end_offset)
      else
        rw.remove_list_element(node.arguments, keyword_hash)
      end
    else
      rw.remove_list_elements(keyword_hash, arguments)
    end
  end
end