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.


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

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


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

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)


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

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


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

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


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

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