Class: Solargraph::Source::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/source/chain.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, Variable

Constant Summary collapse

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

Chain#infer uses the inference stack to avoid recursing into itself. See Chain#active_signature for more information.

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links) ⇒ Chain

Returns a new instance of Chain.

Parameters:



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

Returns:



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

def links
  @links
end

Instance Method Details

#baseChain

Returns:



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

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

#constant?Boolean

Returns:

  • (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>

Parameters:

Returns:



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

def define api_map, name_pin, 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, api_map)
    return [] if type.undefined?
    working_pin = Pin::ProxyType.anonymous(type)
  end
  links.last.resolve(api_map, working_pin, locals)
end

#infer(api_map, name_pin, locals) ⇒ ComplexType

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/solargraph/source/chain.rb', line 63

def infer api_map, name_pin, locals
  return ComplexType::UNDEFINED if undefined? || @@inference_stack.include?(active_signature(name_pin))
  @@inference_stack.push active_signature(name_pin)
  type = ComplexType::UNDEFINED
  pins = define(api_map, name_pin, locals)
  pins.each do |pin|
    type = pin.infer(api_map)
    break unless type.undefined?
  end
  @@inference_stack.pop
  type
end

#literal?Boolean

Returns:

  • (Boolean)


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

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

#undefined?Boolean

Returns:

  • (Boolean)


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

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