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

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

Constant Summary

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



858
859
860
861
862
# File 'lib/graphql/upgrader/member.rb', line 858

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



856
857
858
# File 'lib/graphql/upgrader/member.rb', line 856

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)



866
867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'lib/graphql/upgrader/member.rb', line 866

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



881
882
883
884
885
# File 'lib/graphql/upgrader/member.rb', line 881

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



887
888
889
890
# File 'lib/graphql/upgrader/member.rb', line 887

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