yoda-language-server 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/client/vscode/package-lock.json +6 -6
- data/client/vscode/src/check-versions.ts +5 -11
- data/client/vscode/src/install-tools.ts +1 -1
- data/lib/yoda/cli/analyze_deps.rb +46 -25
- data/lib/yoda/id_mask.rb +84 -0
- data/lib/yoda/model/descriptions/require_path_description.rb +45 -0
- data/lib/yoda/model/descriptions.rb +1 -0
- data/lib/yoda/model/node_signatures/node.rb +9 -1
- data/lib/yoda/model/values/literal_value.rb +3 -0
- data/lib/yoda/parsing/location.rb +9 -0
- data/lib/yoda/services/loadable_path_resolver.rb +33 -0
- data/lib/yoda/services.rb +1 -0
- data/lib/yoda/store/actions/read_project_files.rb +9 -7
- data/lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb +1 -1
- data/lib/yoda/store/adapters/memory_adapter.rb +1 -1
- data/lib/yoda/store/objects/libraries_status.rb +1 -1
- data/lib/yoda/store/objects/library/core.rb +8 -0
- data/lib/yoda/store/objects/library/gem.rb +14 -3
- data/lib/yoda/store/objects/library/path_resolvable.rb +29 -0
- data/lib/yoda/store/objects/library/std.rb +9 -0
- data/lib/yoda/store/objects/library.rb +1 -0
- data/lib/yoda/store/objects/patch.rb +1 -1
- data/lib/yoda/store/objects/patch_set.rb +2 -2
- data/lib/yoda/store/project/dependency.rb +22 -4
- data/lib/yoda/store/project/file_finder.rb +20 -0
- data/lib/yoda/store/project.rb +2 -0
- data/lib/yoda/store/registry/cache.rb +2 -2
- data/lib/yoda/store/registry/composer.rb +9 -7
- data/lib/yoda/store/registry/index.rb +14 -10
- data/lib/yoda/store/registry/library_registry.rb +1 -1
- data/lib/yoda/store/registry.rb +1 -1
- data/lib/yoda/typing/constant_resolver/code_query.rb +25 -0
- data/lib/yoda/typing/constant_resolver/query.rb +12 -1
- data/lib/yoda/typing/constant_resolver.rb +13 -8
- data/lib/yoda/typing/inferencer/load_resolver.rb +37 -0
- data/lib/yoda/typing/inferencer/tracer.rb +32 -0
- data/lib/yoda/typing/inferencer.rb +3 -2
- data/lib/yoda/typing/node_info.rb +5 -0
- data/lib/yoda/typing/tree/{defined.rb → ask_defined.rb} +3 -2
- data/lib/yoda/typing/tree/base.rb +65 -20
- data/lib/yoda/typing/tree/begin.rb +5 -5
- data/lib/yoda/typing/tree/block_call.rb +26 -0
- data/lib/yoda/typing/tree/case.rb +8 -19
- data/lib/yoda/typing/tree/class_tree.rb +10 -18
- data/lib/yoda/typing/tree/conditional_loop.rb +15 -0
- data/lib/yoda/typing/tree/constant.rb +19 -0
- data/lib/yoda/typing/tree/constant_assignment.rb +2 -2
- data/lib/yoda/typing/tree/ensure.rb +17 -0
- data/lib/yoda/typing/tree/for.rb +7 -0
- data/lib/yoda/typing/tree/hash_tree.rb +32 -0
- data/lib/yoda/typing/tree/if.rb +10 -5
- data/lib/yoda/typing/tree/interpolation_text.rb +21 -0
- data/lib/yoda/typing/tree/literal.rb +8 -36
- data/lib/yoda/typing/tree/literal_inferable.rb +48 -0
- data/lib/yoda/typing/tree/local_exit.rb +15 -0
- data/lib/yoda/typing/tree/logical_assignment.rb +5 -5
- data/lib/yoda/typing/tree/logical_operator.rb +6 -5
- data/lib/yoda/typing/tree/method_def.rb +41 -0
- data/lib/yoda/typing/tree/method_inferable.rb +51 -0
- data/lib/yoda/typing/tree/module_tree.rb +7 -20
- data/lib/yoda/typing/tree/multiple_assignment.rb +6 -10
- data/lib/yoda/typing/tree/namespace_inferable.rb +20 -0
- data/lib/yoda/typing/tree/rescue.rb +18 -0
- data/lib/yoda/typing/tree/rescue_clause.rb +42 -0
- data/lib/yoda/typing/tree/self.rb +2 -1
- data/lib/yoda/typing/tree/send.rb +8 -60
- data/lib/yoda/typing/tree/send_inferable.rb +89 -0
- data/lib/yoda/typing/tree/singleton_class_tree.rb +24 -0
- data/lib/yoda/typing/tree/singleton_method_def.rb +41 -0
- data/lib/yoda/typing/tree/super.rb +9 -2
- data/lib/yoda/typing/tree/variable.rb +5 -10
- data/lib/yoda/typing/tree/variable_assignment.rb +11 -8
- data/lib/yoda/typing/tree/yield.rb +9 -2
- data/lib/yoda/typing/tree.rb +55 -22
- data/lib/yoda/typing.rb +1 -0
- data/lib/yoda/version.rb +1 -1
- data/lib/yoda.rb +1 -0
- metadata +25 -13
- data/lib/yoda/typing/inferencer/ast_traverser.rb +0 -408
- data/lib/yoda/typing/tree/block.rb +0 -12
- data/lib/yoda/typing/tree/const.rb +0 -12
- data/lib/yoda/typing/tree/escape.rb +0 -12
- data/lib/yoda/typing/tree/hash_body.rb +0 -36
- data/lib/yoda/typing/tree/literal_with_interpolation.rb +0 -21
- data/lib/yoda/typing/tree/method.rb +0 -43
- data/lib/yoda/typing/tree/rescue_body.rb +0 -12
- data/lib/yoda/typing/tree/singleton_method.rb +0 -47
- data/lib/yoda/typing/tree/while.rb +0 -12
@@ -1,408 +0,0 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
|
-
module Yoda
|
4
|
-
module Typing
|
5
|
-
class Inferencer
|
6
|
-
class AstTraverser
|
7
|
-
extend Forwardable
|
8
|
-
|
9
|
-
# @return [Tracer]
|
10
|
-
attr_reader :tracer
|
11
|
-
|
12
|
-
# @return [Contexts::BaseContext]
|
13
|
-
attr_reader :context
|
14
|
-
|
15
|
-
delegate [:bind_context, :bind_type, :bind_send] => :tracer
|
16
|
-
|
17
|
-
# @return [Types::Generator]
|
18
|
-
delegate [:generator] => :context
|
19
|
-
|
20
|
-
# @param tracer [Tracer]
|
21
|
-
# @param context [ContextsBaseContext]
|
22
|
-
def initialize(tracer:, context:)
|
23
|
-
@tracer = tracer
|
24
|
-
@context = context
|
25
|
-
end
|
26
|
-
|
27
|
-
# @param node [AST::Vnode]
|
28
|
-
# @return [RBS::Types::t]
|
29
|
-
def traverse(node)
|
30
|
-
bind_context(node: node, context: context)
|
31
|
-
Logger.trace("Traversing #{node}")
|
32
|
-
type = infer_node(node)
|
33
|
-
Logger.trace("Traversed #{node} -> #{type.to_s}")
|
34
|
-
bind_type(node: node, type: type, context: context)
|
35
|
-
|
36
|
-
type
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
# @param context [Contexts::BaseContext]
|
42
|
-
# @return [self]
|
43
|
-
def derive(context:)
|
44
|
-
self.class.new(tracer: tracer, context: context)
|
45
|
-
end
|
46
|
-
|
47
|
-
# @param node [AST::Vnode]
|
48
|
-
# @return [Types::Type]
|
49
|
-
def infer_node(node)
|
50
|
-
case node.type
|
51
|
-
when :lvasgn, :ivasgn, :cvasgn, :gvasgn
|
52
|
-
infer_assignment_node(node)
|
53
|
-
when :casgn
|
54
|
-
# TODO
|
55
|
-
traverse(node.content)
|
56
|
-
when :masgn
|
57
|
-
# TODO
|
58
|
-
traverse(node.content)
|
59
|
-
when :op_asgn, :or_asgn, :and_asgn
|
60
|
-
# TODO
|
61
|
-
traverse(node.content)
|
62
|
-
when :and, :or, :not
|
63
|
-
# TODO
|
64
|
-
generator.union_type(*node.children.map { |node| traverse(node) })
|
65
|
-
when :if
|
66
|
-
infer_branch_nodes(node.children.first, node.children.slice(1..2).compact)
|
67
|
-
when :while, :until, :while_post, :until_post
|
68
|
-
# TODO
|
69
|
-
traverse(node.body)
|
70
|
-
when :for
|
71
|
-
# TODO
|
72
|
-
traverse(node.body)
|
73
|
-
when :case
|
74
|
-
infer_case_node(node)
|
75
|
-
when :super, :zsuper, :yield
|
76
|
-
# TODO
|
77
|
-
type_for_literal_sexp(node)
|
78
|
-
when :return, :break, :next
|
79
|
-
# TODO
|
80
|
-
node.arguments[0] ? traverse(node.arguments[0]) : generator.nil_type
|
81
|
-
when :ensure
|
82
|
-
infer_ensure_node(node)
|
83
|
-
when :rescue
|
84
|
-
infer_rescue_node(node)
|
85
|
-
when :resbody
|
86
|
-
infer_resbody_node(node)
|
87
|
-
when :csend, :send
|
88
|
-
infer_send_node(node)
|
89
|
-
when :block
|
90
|
-
infer_block_call_node(node)
|
91
|
-
when :const
|
92
|
-
infer_const_node(node)
|
93
|
-
when :lvar, :cvar, :ivar, :gvar
|
94
|
-
context.type_binding.resolve(node.name) || generator.any_type
|
95
|
-
when :begin, :kwbegin, :block
|
96
|
-
node.children.map { |node| traverse(node) }.last
|
97
|
-
when :dstr, :dsym, :xstr
|
98
|
-
node.children.each { |node| traverse(node) }
|
99
|
-
type_for_literal_sexp(node)
|
100
|
-
when :def
|
101
|
-
infer_method_node(node)
|
102
|
-
when :defs
|
103
|
-
infer_smethod_node(node)
|
104
|
-
when :hash
|
105
|
-
infer_hash_node(node)
|
106
|
-
when :self
|
107
|
-
context.receiver
|
108
|
-
when :defined
|
109
|
-
generator.boolean_type
|
110
|
-
when :class
|
111
|
-
infer_class_node(node)
|
112
|
-
when :module
|
113
|
-
infer_namespace_node(node)
|
114
|
-
when :sclass
|
115
|
-
infer_singleton_class_node(node)
|
116
|
-
else
|
117
|
-
type_for_literal_sexp(node)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
# @param node [AST::ArgumentNode]
|
122
|
-
# @return [Store::Types::Base]
|
123
|
-
def infer_assignment_node(node)
|
124
|
-
body_type = traverse(node.content)
|
125
|
-
context.type_binding.bind(node.assignee.name, body_type)
|
126
|
-
body_type
|
127
|
-
end
|
128
|
-
|
129
|
-
# @param node [AST::ConstantBaseNode]h
|
130
|
-
# @return [Types::Base]
|
131
|
-
def infer_const_base_node(node)
|
132
|
-
object = Store::Query::FindConstant.new(context.registry).find('::')
|
133
|
-
|
134
|
-
case object
|
135
|
-
when Store::Objects::NamespaceObject
|
136
|
-
generator.singleton_type_at(object.path)
|
137
|
-
when Store::Objects::ValueObject
|
138
|
-
# TODO
|
139
|
-
generator.any_type
|
140
|
-
else
|
141
|
-
generator.any_type
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
# @param node [AST::ConstantNode]
|
146
|
-
# @return [Types::Base]
|
147
|
-
def infer_const_node(node)
|
148
|
-
context.constant_resolver.resolve_node(node, tracer: tracer)
|
149
|
-
end
|
150
|
-
|
151
|
-
# @param node [AST::DefNode]
|
152
|
-
# @return [Types::Base]
|
153
|
-
def infer_method_node(node)
|
154
|
-
traverse_method(node, receiver_type: context.method_receiver)
|
155
|
-
end
|
156
|
-
|
157
|
-
# @param node [AST::DefSingletonNode]
|
158
|
-
# @return [Types::Base]
|
159
|
-
def infer_smethod_node(node)
|
160
|
-
traverse_method(node, receiver_type: traverse(node.receiver))
|
161
|
-
end
|
162
|
-
|
163
|
-
# @param node [AST::DefNode, AST::DefSingletonNode]
|
164
|
-
# @param receiver_type [Types::Type]
|
165
|
-
# @return [Types::Base]
|
166
|
-
def traverse_method(node, receiver_type:)
|
167
|
-
value_resolve_context = generator.value_resolve_context(self_type: receiver_type)
|
168
|
-
|
169
|
-
method_candidates = receiver_type.value.select_method(node.name.to_s, visibility: %i(private protected public))
|
170
|
-
method_types = method_candidates.map(&:rbs_type).map { |type| value_resolve_context.wrap(type) }
|
171
|
-
|
172
|
-
# TODO: Support overloads
|
173
|
-
method_bind = method_types.reduce({}) do |all_bind, method_type|
|
174
|
-
bind = ParameterBinder.new(node.parameters.parameter).bind(type: method_type, generator: generator)
|
175
|
-
all_bind.merge(bind.to_h) { |_key, v1, v2| generator.union_type(v1, v2) }
|
176
|
-
end
|
177
|
-
|
178
|
-
Logger.trace("method_candidates: [#{method_candidates.join(', ')}]")
|
179
|
-
Logger.trace("bind arguments: #{method_bind.map { |key, value| [key, value.to_s] }.to_h }")
|
180
|
-
|
181
|
-
tracer.bind_method_definition(node: node, method_candidates: method_candidates)
|
182
|
-
|
183
|
-
method_context = context.derive_method_context(receiver_type: receiver_type, binds: method_bind)
|
184
|
-
derive(context: method_context).traverse(node.body)
|
185
|
-
|
186
|
-
generator.symbol_type(node.name.to_sym)
|
187
|
-
end
|
188
|
-
|
189
|
-
# @param node [AST::SingletonClassNode]
|
190
|
-
# @return [Types::Base]
|
191
|
-
def infer_singleton_class_node(node)
|
192
|
-
receiver_type = traverse(node.receiver)
|
193
|
-
|
194
|
-
new_context = context.derive_class_context(class_type: receiver_type.singleton_type)
|
195
|
-
derive(context: new_context).traverse(node.body)
|
196
|
-
|
197
|
-
generator.nil_type
|
198
|
-
end
|
199
|
-
|
200
|
-
# @param node [AST::ClassNode]
|
201
|
-
# @return [Types::Base]
|
202
|
-
def infer_class_node(node)
|
203
|
-
if super_class_node = node.super_class
|
204
|
-
traverse(super_class_node)
|
205
|
-
end
|
206
|
-
|
207
|
-
infer_namespace_node(node)
|
208
|
-
end
|
209
|
-
|
210
|
-
# @param node [AST::ModuleNode, AST::ClassNode]
|
211
|
-
# @return [Types::Base]
|
212
|
-
def infer_namespace_node(node)
|
213
|
-
namespace_type = traverse(node.receiver)
|
214
|
-
|
215
|
-
new_context = context.derive_class_context(class_type: namespace_type)
|
216
|
-
derive(context: new_context).traverse(node.body)
|
217
|
-
|
218
|
-
namespace_type
|
219
|
-
end
|
220
|
-
|
221
|
-
# @param node [AST::HashNode]
|
222
|
-
# @return [Model::TypeExpressions::Base]
|
223
|
-
def infer_hash_node(node)
|
224
|
-
hash = node.contents.each_with_object({}) do |node, memo|
|
225
|
-
case node.type
|
226
|
-
when :pair
|
227
|
-
case node.key.type
|
228
|
-
when :sym
|
229
|
-
memo[node.key.value.to_sym] = traverse(node.value)
|
230
|
-
when :str
|
231
|
-
memo[node.key.value.to_s] = traverse(node.value)
|
232
|
-
else
|
233
|
-
# TODO: Support other key types.
|
234
|
-
end
|
235
|
-
when :kwsplat
|
236
|
-
traverse(node.content)
|
237
|
-
# TODO: merge infered result
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
generator.record_type(hash)
|
242
|
-
end
|
243
|
-
|
244
|
-
# @param node [AST::BlockCallNode]
|
245
|
-
def infer_block_call_node(node)
|
246
|
-
infer_send_node(node.send_clause, node.parameters, node.body)
|
247
|
-
end
|
248
|
-
|
249
|
-
# @param node [AST::SendNode]
|
250
|
-
# @param block_param_node [AST::ParametersNode, nil]
|
251
|
-
# @param block_node [AST::Vnode, nil]
|
252
|
-
# @return [Types::Base]
|
253
|
-
def infer_send_node(node, block_param_node = nil, block_node = nil)
|
254
|
-
receiver_type = node.implicit_receiver? ? context.receiver : traverse(node.receiver)
|
255
|
-
argument_types = infer_argument_nodes(node.arguments)
|
256
|
-
value_resolve_context = generator.value_resolve_context(self_type: receiver_type)
|
257
|
-
|
258
|
-
visibility = node.implicit_receiver? ? %i(private protected public) : %i(public)
|
259
|
-
# TODO: Support overloads
|
260
|
-
method_candidates = receiver_type.value.select_method(node.selector_name, visibility: visibility)
|
261
|
-
method_types = method_candidates.map(&:rbs_type).map { |type| value_resolve_context.wrap(type) }
|
262
|
-
|
263
|
-
if block_node
|
264
|
-
# TODO: Resolve type variables by matching argument_types with arguments
|
265
|
-
binds = ArgumentsBinder.new(generator: generator).bind(types: method_types, arguments: block_param_node.parameter)
|
266
|
-
new_context = context.derive_block_context(binds: binds)
|
267
|
-
derive(context: new_context).traverse(block_node)
|
268
|
-
end
|
269
|
-
|
270
|
-
Logger.trace("method_candidates: [#{method_candidates.join(', ')}]")
|
271
|
-
Logger.trace("receiver_type: #{receiver_type}")
|
272
|
-
|
273
|
-
bind_send(node: node, method_candidates: method_candidates, receiver_type: receiver_type)
|
274
|
-
if method_types.empty?
|
275
|
-
generator.unknown_type(reason: "method not found")
|
276
|
-
else
|
277
|
-
generator.union_type(*method_types.map { |method_type| value_resolve_context.wrap(method_type.type.return_type) })
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
# @param nodes [Array<::AST::Node>]
|
282
|
-
# @return [{Symbol => Types::Base}]
|
283
|
-
def infer_argument_nodes(argument_nodes)
|
284
|
-
argument_nodes.each_with_object({}) do |node, obj|
|
285
|
-
case node.type
|
286
|
-
when :splat
|
287
|
-
# TODO
|
288
|
-
traverse(node)
|
289
|
-
when :block_pass
|
290
|
-
obj[:block_argument] = traverse(node.children.first)
|
291
|
-
else
|
292
|
-
obj[:arguments] ||= []
|
293
|
-
obj[:arguments].push(traverse(node))
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
# @param condition_node [::AST::Node]
|
299
|
-
# @param branch_nodes [Array<::AST::Node>]
|
300
|
-
# @return [Types::Base]
|
301
|
-
def infer_branch_nodes(condition_node, branch_nodes)
|
302
|
-
traverse(condition_node)
|
303
|
-
generator.union_type(*branch_nodes.map { |node| traverse(node) })
|
304
|
-
end
|
305
|
-
|
306
|
-
# @param node [::AST::Node]
|
307
|
-
# @return [[Store::Types::Base, Environment]]
|
308
|
-
def infer_case_node(node)
|
309
|
-
# TODO
|
310
|
-
subject_node, *when_nodes, else_node = node.children
|
311
|
-
when_body_nodes = when_nodes.map { |node| node.children.last }
|
312
|
-
generator.union_type(*[*when_body_nodes, else_node].compact.map { |node| traverse(node) })
|
313
|
-
end
|
314
|
-
|
315
|
-
# @param node [AST::EnsureNode]
|
316
|
-
# @return [Types::Base]
|
317
|
-
def infer_ensure_node(node)
|
318
|
-
type = traverse(node.body)
|
319
|
-
traverse(node.ensure_body)
|
320
|
-
type
|
321
|
-
end
|
322
|
-
|
323
|
-
# @param node [AST::RescueNode]
|
324
|
-
# @return [Types::Base]
|
325
|
-
def infer_rescue_node(node)
|
326
|
-
type = traverse(node.body)
|
327
|
-
node.rescue_clauses.each { |rescue_clause| traverse(rescue_clause) }
|
328
|
-
traverse(node.else_clause)
|
329
|
-
type
|
330
|
-
end
|
331
|
-
|
332
|
-
# @param node [AST::RescueClauseNode]
|
333
|
-
# @return [Types::Base]
|
334
|
-
def infer_resbody_node(node)
|
335
|
-
binds = {}
|
336
|
-
|
337
|
-
exception_type = begin
|
338
|
-
if node.match_clause
|
339
|
-
case node.match_clause.type
|
340
|
-
when :array
|
341
|
-
generator.union_type(*node.match_clause.contents.map { |content| traverse(content).instance_type })
|
342
|
-
when :empty
|
343
|
-
generator.standard_error_type
|
344
|
-
else
|
345
|
-
# Unexpected
|
346
|
-
generator.standard_error_type
|
347
|
-
end
|
348
|
-
else
|
349
|
-
generator.standard_error_type
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
|
354
|
-
if node.assignee
|
355
|
-
case node.assignee.type
|
356
|
-
when :lvasgn
|
357
|
-
binds[node.assignee.assignee.name] = exception_type
|
358
|
-
end
|
359
|
-
end
|
360
|
-
|
361
|
-
new_context = context.derive_block_context(binds: binds)
|
362
|
-
derive(context: new_context).traverse(node.body)
|
363
|
-
end
|
364
|
-
|
365
|
-
# @param [AST::LiteralNode]
|
366
|
-
# @return [Types::Base]
|
367
|
-
def type_for_literal_sexp(node)
|
368
|
-
case node.type
|
369
|
-
when :dstr, :xstr
|
370
|
-
generator.string_type
|
371
|
-
when :str, :string
|
372
|
-
generator.string_type(node.value.to_s)
|
373
|
-
when :dsym
|
374
|
-
generator.symbol_type
|
375
|
-
when :sym
|
376
|
-
generator.symbol_type(node.value.to_sym)
|
377
|
-
when :array, :splat
|
378
|
-
generator.array_type
|
379
|
-
when :hash
|
380
|
-
generator.hash_type
|
381
|
-
when :irange, :erange
|
382
|
-
generator.range_type
|
383
|
-
when :regexp
|
384
|
-
generator.regexp_type
|
385
|
-
when :true
|
386
|
-
generator.true_type
|
387
|
-
when :false
|
388
|
-
generator.false_type
|
389
|
-
when :nil
|
390
|
-
generator.nil_type
|
391
|
-
when :int
|
392
|
-
generator.integer_type(node.value.to_i)
|
393
|
-
when :float
|
394
|
-
generator.float_type
|
395
|
-
when :complex
|
396
|
-
generator.numeric_type
|
397
|
-
when :rational
|
398
|
-
generator.numeric_type
|
399
|
-
when :empty
|
400
|
-
generator.nil_type
|
401
|
-
else
|
402
|
-
generator.any_type
|
403
|
-
end
|
404
|
-
end
|
405
|
-
end
|
406
|
-
end
|
407
|
-
end
|
408
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Yoda
|
2
|
-
module Typing
|
3
|
-
module Tree
|
4
|
-
class HashBody < Base
|
5
|
-
def type
|
6
|
-
infer_hash_node(node)
|
7
|
-
end
|
8
|
-
|
9
|
-
# @param node [::AST::Node]
|
10
|
-
# @return [Model::TypeExpressions::Base]
|
11
|
-
def infer_hash_node(node)
|
12
|
-
hash = node.children.each_with_object({}) do |node, memo|
|
13
|
-
case node.type
|
14
|
-
when :pair
|
15
|
-
pair_key, pair_value = node.children
|
16
|
-
case pair_key.type
|
17
|
-
when :sym
|
18
|
-
memo[pair_key.children.first.to_sym] = infer(pair_value)
|
19
|
-
when :str
|
20
|
-
memo[pair_key.children.first.to_s] = infer(pair_value)
|
21
|
-
else
|
22
|
-
# TODO: Support other key types.
|
23
|
-
end
|
24
|
-
when :kwsplat
|
25
|
-
content_node = node.children.first
|
26
|
-
content_type = infer(content_node)
|
27
|
-
# TODO: merge infered result
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
Types::AssociativeArray.new(contents: hash)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Yoda
|
2
|
-
module Typing
|
3
|
-
module Tree
|
4
|
-
class LiteralWithInterpolation < Base
|
5
|
-
def children
|
6
|
-
@children ||= node.children.map(&method(:build_child))
|
7
|
-
end
|
8
|
-
|
9
|
-
def type
|
10
|
-
children.map(&:type)
|
11
|
-
case node.type
|
12
|
-
when :xstr, :dstr
|
13
|
-
generator.string_type
|
14
|
-
when :dsym
|
15
|
-
generator.symbol_type
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module Yoda
|
2
|
-
module Typing
|
3
|
-
module Tree
|
4
|
-
class Method < Base
|
5
|
-
def name
|
6
|
-
node.children.first
|
7
|
-
end
|
8
|
-
|
9
|
-
def argument
|
10
|
-
@argument ||= build_child(node.children[1])
|
11
|
-
end
|
12
|
-
|
13
|
-
def body
|
14
|
-
@body ||= node.children[2] && build_child(node.children[2], context: method_context)
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [Types::Base]
|
18
|
-
def type
|
19
|
-
@type ||= generator.symbol_type
|
20
|
-
end
|
21
|
-
|
22
|
-
def receiver_type
|
23
|
-
@receiver_type ||= generator.union(context.current_objects.map { |object| generator.object_type(object) })
|
24
|
-
end
|
25
|
-
|
26
|
-
def method_context
|
27
|
-
@method_context ||= method_definition_provider.generate_method_context(context: context, args_node: args_node)
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def method_definition_provider
|
33
|
-
@method_definition_provider ||= MethodDefinitionResolver.new(
|
34
|
-
receiver_type: receiver_type,
|
35
|
-
name: name,
|
36
|
-
registry: context.registry,
|
37
|
-
generator: generator,
|
38
|
-
)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module Yoda
|
2
|
-
module Typing
|
3
|
-
module Tree
|
4
|
-
class SingletonMethod < Base
|
5
|
-
def receiver
|
6
|
-
node.children[0]
|
7
|
-
end
|
8
|
-
|
9
|
-
def name
|
10
|
-
node.children[1]
|
11
|
-
end
|
12
|
-
|
13
|
-
def argument
|
14
|
-
@argument ||= build_child(node.children[2])
|
15
|
-
end
|
16
|
-
|
17
|
-
def body
|
18
|
-
@body ||= node.children[3] && build_child(node.children[3], context: method_context)
|
19
|
-
end
|
20
|
-
|
21
|
-
# @return [Types::Base]
|
22
|
-
def type
|
23
|
-
@type ||= generator.symbol_type
|
24
|
-
end
|
25
|
-
|
26
|
-
def receiver_type
|
27
|
-
@receiver_type ||= receiver.type
|
28
|
-
end
|
29
|
-
|
30
|
-
def method_context
|
31
|
-
@method_context ||= method_definition_provider.generate_method_context(context: context, args_node: args_node)
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def method_definition_provider
|
37
|
-
@method_definition_provider ||= MethodDefinitionResolver.new(
|
38
|
-
receiver_type: receiver_type,
|
39
|
-
name: name,
|
40
|
-
registry: context.registry,
|
41
|
-
generator: generator,
|
42
|
-
)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|