Class: Solargraph::Source::Chain
- Inherits:
-
Object
- Object
- Solargraph::Source::Chain
show all
- Defined in:
- lib/solargraph/source/chain.rb,
lib/solargraph/source/chain/if.rb,
lib/solargraph/source/chain/or.rb,
lib/solargraph/source/chain/call.rb,
lib/solargraph/source/chain/hash.rb,
lib/solargraph/source/chain/head.rb,
lib/solargraph/source/chain/link.rb,
lib/solargraph/source/chain/array.rb,
lib/solargraph/source/chain/q_call.rb,
lib/solargraph/source/chain/literal.rb,
lib/solargraph/source/chain/z_super.rb,
lib/solargraph/source/chain/constant.rb,
lib/solargraph/source/chain/variable.rb,
lib/solargraph/source/chain/block_symbol.rb,
lib/solargraph/source/chain/block_variable.rb,
lib/solargraph/source/chain/class_variable.rb,
lib/solargraph/source/chain/global_variable.rb,
lib/solargraph/source/chain/instance_variable.rb
Overview
Represents an expression as a single call chain at the parse tree level, made up of constants, variables, and method calls, each represented as a Link object.
Computes Pins and/or ComplexTypes representing the interpreted expression.
Defined Under Namespace
Classes: Array, BlockSymbol, BlockVariable, Call, ClassVariable, Constant, GlobalVariable, Hash, Head, If, InstanceVariable, Link, Literal, Or, QCall, Variable, ZSuper
Constant Summary
collapse
- UNDEFINED_CALL =
Chain::Call.new('<undefined>')
- UNDEFINED_CONSTANT =
Chain::Constant.new('<undefined>')
- @@inference_stack =
[]
- @@inference_depth =
0
- @@inference_invalidation_key =
nil
- @@inference_cache =
{}
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(links, node = nil, splat = false) ⇒ Chain
Returns a new instance of Chain.
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/solargraph/source/chain.rb', line 55
def initialize links, node = nil, splat = false
@links = links.clone
@links.push UNDEFINED_CALL if @links.empty?
head = true
@links.map! do |link|
result = (head ? link.clone_head : link.clone_body)
head = false
result
end
@node = node
@splat = splat
end
|
Instance Attribute Details
48
49
50
|
# File 'lib/solargraph/source/chain.rb', line 48
def links
@links
end
|
#node ⇒ Object
Returns the value of attribute node.
50
51
52
|
# File 'lib/solargraph/source/chain.rb', line 50
def node
@node
end
|
Instance Method Details
69
70
71
|
# File 'lib/solargraph/source/chain.rb', line 69
def base
@base ||= Chain.new(links[0..-2])
end
|
#constant? ⇒ Boolean
151
152
153
|
# File 'lib/solargraph/source/chain.rb', line 151
def constant?
links.last.is_a?(Chain::Constant)
end
|
#define(api_map, name_pin, locals) ⇒ ::Array<Pin::Base>
Determine potential Pins returned by this chain of words
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/solargraph/source/chain.rb', line 89
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)
type = infer_first_defined(pins, working_pin, api_map, locals)
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
146
147
148
|
# File 'lib/solargraph/source/chain.rb', line 146
def defined?
!undefined?
end
|
#infer(api_map, name_pin, locals) ⇒ ComplexType
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/solargraph/source/chain.rb', line 114
def infer api_map, name_pin, locals
out = nil
cached = @@inference_cache[[node, node.location, links.map(&:word), name_pin&.return_type, locals]] unless node.nil?
return cached if cached && @@inference_invalidation_key == api_map.hash
out = infer_uncached api_map, name_pin, locals
if @@inference_invalidation_key != api_map.hash
@@inference_cache = {}
@@inference_invalidation_key = api_map.hash
end
@@inference_cache[[node, node.location, links.map(&:word), name_pin&.return_type, locals]] = out unless node.nil?
out
end
|
#infer_uncached(api_map, name_pin, locals) ⇒ ComplexType
131
132
133
134
135
|
# File 'lib/solargraph/source/chain.rb', line 131
def infer_uncached api_map, name_pin, locals
pins = define(api_map, name_pin, locals)
type = infer_first_defined(pins, links.last.last_context, api_map, locals)
maybe_nil(type)
end
|
#literal? ⇒ Boolean
138
139
140
|
# File 'lib/solargraph/source/chain.rb', line 138
def literal?
links.last.is_a?(Chain::Literal)
end
|
#nullable? ⇒ Boolean
159
160
161
|
# File 'lib/solargraph/source/chain.rb', line 159
def nullable?
links.any?(&:nullable?)
end
|
#splat? ⇒ Boolean
155
156
157
|
# File 'lib/solargraph/source/chain.rb', line 155
def splat?
@splat
end
|
#undefined? ⇒ Boolean
142
143
144
|
# File 'lib/solargraph/source/chain.rb', line 142
def undefined?
links.any?(&:undefined?)
end
|