Class: GraphQL::Upgrader::Member::FieldFinder

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
lib/graphql/upgrader/member.rb

Constant Summary collapse

DEFINITION_METHODS =

These methods are definition DSLs which may accept a block, each of these definitions is passed for transformation in its own right. field and connection take priority. In fact, they upgrade their own arguments, so those upgrades turn out to be no-ops.

[:field, :connection, :input_field, :return_field, :argument]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFieldFinder

Returns a new instance of FieldFinder.



901
902
903
904
905
# File 'lib/graphql/upgrader/member.rb', line 901

def initialize
  # Pairs of `{ { method_name => { name => [start, end] } }`,
  # since fields/arguments are unique by name, within their category
  @locations = Hash.new { |h,k| h[k] = {} }
end

Instance Attribute Details

#locationsObject (readonly)

Returns the value of attribute locations.



899
900
901
# File 'lib/graphql/upgrader/member.rb', line 899

def locations
  @locations
end

Instance Method Details

#add_location(send_node:, source_node:) ⇒ Object

Parameters:

  • send_node (node)

    The node which might be a field call, etc

  • source_node (node)

    The node whose source defines the bounds of the definition (eg, the surrounding block)



909
910
911
912
913
914
915
916
917
918
919
920
921
922
# File 'lib/graphql/upgrader/member.rb', line 909

def add_location(send_node:,source_node:)
  receiver_node, method_name, *arg_nodes = *send_node
  # Implicit self and one of the recognized methods
  if receiver_node.nil? && DEFINITION_METHODS.include?(method_name)
    name = arg_nodes[0]
    # This field may have already been added because
    # we find `(block ...)` nodes _before_ we find `(send ...)` nodes.
    if @locations[method_name][name].nil?
      starting_idx = source_node.loc.expression.begin.begin_pos
      ending_idx = source_node.loc.expression.end.end_pos
      @locations[method_name][name] = [starting_idx, ending_idx]
    end
  end
end

#on_block(node) ⇒ Object



924
925
926
927
928
# File 'lib/graphql/upgrader/member.rb', line 924

def on_block(node)
  send_node, _args_node, _body_node = *node
  add_location(send_node: send_node, source_node: node)
  super(node)
end

#on_send(node) ⇒ Object



930
931
932
933
# File 'lib/graphql/upgrader/member.rb', line 930

def on_send(node)
  add_location(send_node: node, source_node: node)
  super(node)
end