Class: Solargraph::Source::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/source/chain.rb,
lib/solargraph/source/chain/or.rb,
lib/solargraph/source/chain/call.rb,
lib/solargraph/source/chain/head.rb,
lib/solargraph/source/chain/link.rb,
lib/solargraph/source/chain/literal.rb,
lib/solargraph/source/chain/constant.rb,
lib/solargraph/source/chain/variable.rb,
lib/solargraph/source/chain/class_variable.rb,
lib/solargraph/source/chain/global_variable.rb,
lib/solargraph/source/chain/instance_variable.rb

Overview

A chain of constants, variables, and method calls for inferring types of values.

Defined Under Namespace

Classes: Call, ClassVariable, Constant, GlobalVariable, Head, InstanceVariable, Link, Literal, Or, Variable

Constant Summary collapse

UNDEFINED_CALL =
Chain::Call.new('<undefined>')
UNDEFINED_CONSTANT =
Chain::Constant.new('<undefined>')
@@inference_stack =
[]
@@inference_depth =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links) ⇒ Chain


31
32
33
34
# File 'lib/solargraph/source/chain.rb', line 31

def initialize links
  @links = links
  @links.push UNDEFINED_CALL if @links.empty?
end

Instance Attribute Details


28
29
30
# File 'lib/solargraph/source/chain.rb', line 28

def links
  @links
end

Instance Method Details

#baseChain


37
38
39
# File 'lib/solargraph/source/chain.rb', line 37

def base
  @base ||= Chain.new(links[0..-2])
end

#constant?Boolean


87
88
89
# File 'lib/solargraph/source/chain.rb', line 87

def constant?
  links.last.is_a?(Chain::Constant)
end

#define(api_map, name_pin, locals) ⇒ Array<Pin::Base>


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/solargraph/source/chain.rb', line 45

def define api_map, name_pin, locals
  rebind_block name_pin, api_map, locals
  return [] if undefined?
  working_pin = name_pin
  links[0..-2].each do |link|
    pins = link.resolve(api_map, working_pin, locals)
    # Locals are only used when resolving the first link
    locals = []
    type = infer_first_defined(pins, working_pin, api_map)
    return [] if type.undefined?
    working_pin = Pin::ProxyType.anonymous(type)
  end
  links.last.last_context = working_pin
  links.last.resolve(api_map, working_pin, locals)
end

#defined?Boolean


82
83
84
# File 'lib/solargraph/source/chain.rb', line 82

def defined?
  !undefined?
end

#infer(api_map, name_pin, locals) ⇒ ComplexType


65
66
67
68
69
70
71
# File 'lib/solargraph/source/chain.rb', line 65

def infer api_map, name_pin, locals
  rebind_block name_pin, api_map, locals
  type = ComplexType::UNDEFINED
  pins = define(api_map, name_pin, locals)
  type = infer_first_defined(pins, links.last.last_context, api_map)
  type
end

#literal?Boolean


74
75
76
# File 'lib/solargraph/source/chain.rb', line 74

def literal?
  links.last.is_a?(Chain::Literal)
end

#undefined?Boolean


78
79
80
# File 'lib/solargraph/source/chain.rb', line 78

def undefined?
  links.any?(&:undefined?)
end