class Solargraph::Source::Chain

def infer_first_defined pins, context, api_map, locals

Returns:
  • (ComplexType) -

Parameters:
  • locals (::Enumerable) --
  • api_map (ApiMap) --
  • context (Pin::Base) --
  • pins (::Array) --
def infer_first_defined pins, context, api_map, locals
  possibles = []
  # @todo this param tag shouldn't be needed to probe the type

  # @todo ...but given it is needed, typecheck should complain that it is needed

  # @param pin [Pin::Base]

  pins.each do |pin|
    # Avoid infinite recursion

    next if @@inference_stack.include?(pin.identity)
    @@inference_stack.push pin.identity
    type = pin.typify(api_map)
    @@inference_stack.pop
    if type.defined?
      if type.generic?
        # @todo even at strong, no typechecking complaint

        #   happens when a [Pin::Base,nil] is passed into a method

        #   that accepts only [Pin::Namespace] as an argument

        type = type.resolve_generics(pin.closure, context.binder)
      end
      if type.defined?
        possibles.push type
        break if pin.is_a?(Pin::Method)
      end
    end
  end
  if possibles.empty?
    # Limit method inference recursion

    return ComplexType::UNDEFINED if @@inference_depth >= 10 && pins.first.is_a?(Pin::Method)
    @@inference_depth += 1
    # @param pin [Pin::Base]

    pins.each do |pin|
      # Avoid infinite recursion

      next if @@inference_stack.include?(pin.identity)
      @@inference_stack.push pin.identity
      type = pin.probe(api_map)
      @@inference_stack.pop
      if type.defined?
        possibles.push type
        break if pin.is_a?(Pin::Method)
      end
    end
    @@inference_depth -= 1
  end
  return ComplexType::UNDEFINED if possibles.empty?
  type = if possibles.length > 1
    # Move nil to the end by convention

    sorted = possibles.sort { |a, _| a.tag == 'nil' ? 1 : 0 }
    ComplexType.new(sorted.uniq)
  else
    ComplexType.new(possibles)
  end
  return type if context.nil? || context.return_type.undefined?
  type.self_to_type(context.return_type)
end