repl_type_completor 0.1.15 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7396517907163fafe31927aecac03274cbfaeea45a76105d8e44fadccba7ea98
|
|
4
|
+
data.tar.gz: 4c7e2f53c8cc769b955183a43a6be74ff56c3edd69362a5291340544979d12bc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 21c015fd50e3c0e35d62a8d60b055e38590c28811e69921940be67c26abe3c5d2b2ba1ab5b85ac45bffedfc1bb58f1ff1d76a38d14853b0b6d7c19718d4d327f
|
|
7
|
+
data.tar.gz: 24e8a55efc092fa2906fb5d0c2258c8b0ef403e53ac55c018429555533ff2842696f0edc1b7bb1f41f0a4739a7a5a2112e897b58ac9e9a7093c7a46e6980ec01
|
|
@@ -46,7 +46,7 @@ module ReplTypeCompletor
|
|
|
46
46
|
in [:require_relative, name]
|
|
47
47
|
RequirePaths.require_relative_completions(name, @source_file)
|
|
48
48
|
in [:call_or_const, name, type, self_call]
|
|
49
|
-
((self_call ? type.all_methods : type.methods).map(&:to_s) - HIDDEN_METHODS) | type.constants
|
|
49
|
+
((self_call ? type.all_methods : type.methods).map(&:to_s) - HIDDEN_METHODS).sort | type.constants.sort
|
|
50
50
|
in [:const, name, type, scope]
|
|
51
51
|
if type
|
|
52
52
|
scope_constants = type.types.flat_map do |t|
|
|
@@ -60,9 +60,9 @@ module ReplTypeCompletor
|
|
|
60
60
|
ivars = scope.instance_variables.sort
|
|
61
61
|
name == '@' ? ivars + scope.class_variables.sort : ivars
|
|
62
62
|
in [:cvar, name, scope]
|
|
63
|
-
scope.class_variables
|
|
63
|
+
scope.class_variables.sort
|
|
64
64
|
in [:gvar, name, scope]
|
|
65
|
-
scope.global_variables
|
|
65
|
+
scope.global_variables.sort
|
|
66
66
|
in [:symbol, name]
|
|
67
67
|
filter_symbol_candidates(Symbol.all_symbols, name, limit: 100)
|
|
68
68
|
in [:aref, key_type, name, receiver_type]
|
|
@@ -70,18 +70,22 @@ module ReplTypeCompletor
|
|
|
70
70
|
keys = receiver_type.types.grep(Types::InstanceType).select do |t|
|
|
71
71
|
Hash == t.klass
|
|
72
72
|
end.flat_map do |t|
|
|
73
|
-
t.instances.flat_map(&:keys).grep(key_type).uniq
|
|
73
|
+
t.instances.flat_map(&:keys).grep(key_type).uniq
|
|
74
74
|
end
|
|
75
75
|
if key_type == Symbol
|
|
76
76
|
keys = Symbol.all_symbols if keys.empty? && name.size >= 1
|
|
77
77
|
filter_symbol_candidates(keys, name, limit: 100)
|
|
78
78
|
else
|
|
79
|
-
keys.
|
|
79
|
+
keys.sort
|
|
80
80
|
end
|
|
81
81
|
in [:call, name, type, self_call]
|
|
82
|
-
(self_call ? type.all_methods : type.methods).map(&:to_s) - HIDDEN_METHODS
|
|
82
|
+
methods = (self_call ? type.all_methods : type.methods).map(&:to_s) - HIDDEN_METHODS
|
|
83
|
+
# Alphabetical order, but internal methods (leading underscore) last.
|
|
84
|
+
# `_1[0] == '_'` because `start_with?('_')` raises EncodingError if the
|
|
85
|
+
# method name's encoding is incompatible with US-ASCII.
|
|
86
|
+
methods.sort_by { [_1[0] == '_' ? 1 : 0, _1] }
|
|
83
87
|
in [:lvar_or_method, name, scope]
|
|
84
|
-
scope.self_type.all_methods.map(&:to_s)
|
|
88
|
+
scope.local_variables.sort | scope.self_type.all_methods.map(&:to_s).sort | RESERVED_WORDS
|
|
85
89
|
else
|
|
86
90
|
[]
|
|
87
91
|
end
|
|
@@ -177,15 +177,15 @@ module ReplTypeCompletor
|
|
|
177
177
|
hash = method_call hash, :to_hash, [], nil, nil, scope
|
|
178
178
|
end
|
|
179
179
|
if hash.is_a?(Types::InstanceType) && hash.klass == Hash
|
|
180
|
-
keys << hash.params[
|
|
181
|
-
values << hash.params[
|
|
180
|
+
keys << hash.params[0] if hash.params[0]
|
|
181
|
+
values << hash.params[1] if hash.params[1]
|
|
182
182
|
end
|
|
183
183
|
end
|
|
184
184
|
end
|
|
185
185
|
if keys.empty? && values.empty?
|
|
186
|
-
Types::InstanceType.new
|
|
186
|
+
Types::InstanceType.new(Hash)
|
|
187
187
|
else
|
|
188
|
-
Types::InstanceType.new
|
|
188
|
+
Types::InstanceType.new(Hash, [Types::UnionType[*keys], Types::UnionType[*values]])
|
|
189
189
|
end
|
|
190
190
|
end
|
|
191
191
|
|
|
@@ -706,7 +706,7 @@ module ReplTypeCompletor
|
|
|
706
706
|
inner_scope = Scope.new scope, { Scope::BREAK_RESULT => nil }
|
|
707
707
|
ary_type = method_call collection, :to_ary, [], nil, nil, nil, name_match: false
|
|
708
708
|
element_types = ary_type.types.filter_map do |ary|
|
|
709
|
-
ary.params[
|
|
709
|
+
ary.params[0] if ary.is_a?(Types::InstanceType) && ary.klass == Array
|
|
710
710
|
end
|
|
711
711
|
element_type = Types::UnionType[*element_types]
|
|
712
712
|
inner_scope.conditional do |s|
|
|
@@ -761,7 +761,7 @@ module ReplTypeCompletor
|
|
|
761
761
|
beg_type = evaluate node.left, scope if node.left
|
|
762
762
|
end_type = evaluate node.right, scope if node.right
|
|
763
763
|
elem = (Types::UnionType[*[beg_type, end_type].compact]).nonnillable
|
|
764
|
-
Types::InstanceType.new
|
|
764
|
+
Types::InstanceType.new(Range, [elem])
|
|
765
765
|
end
|
|
766
766
|
|
|
767
767
|
def evaluate_defined_node(node, scope)
|
|
@@ -958,7 +958,7 @@ module ReplTypeCompletor
|
|
|
958
958
|
end
|
|
959
959
|
# node.keyword_rest is Prism::KeywordRestParameterNode or Prism::ForwardingParameterNode or Prism::NoKeywordsParameterNode
|
|
960
960
|
if node.keyword_rest.is_a?(Prism::KeywordRestParameterNode) && node.keyword_rest.name
|
|
961
|
-
scope[node.keyword_rest.name.to_s] = Types::InstanceType.new(Hash,
|
|
961
|
+
scope[node.keyword_rest.name.to_s] = Types::InstanceType.new(Hash, [Types::SYMBOL, Types::UnionType[*kwargs.values]])
|
|
962
962
|
end
|
|
963
963
|
if node.block&.name
|
|
964
964
|
# node.block is Prism::BlockParameterNode
|
|
@@ -1143,7 +1143,7 @@ module ReplTypeCompletor
|
|
|
1143
1143
|
true
|
|
1144
1144
|
end
|
|
1145
1145
|
end
|
|
1146
|
-
array_elem = arrays.empty? ? nil : Types::UnionType[*arrays.map { _1.params[
|
|
1146
|
+
array_elem = arrays.empty? ? nil : Types::UnionType[*arrays.map { _1.params[0] || Types::OBJECT }]
|
|
1147
1147
|
non_array = non_arrays.empty? ? nil : Types::UnionType[*non_arrays]
|
|
1148
1148
|
[array_elem, non_array]
|
|
1149
1149
|
end
|
|
@@ -1152,7 +1152,7 @@ module ReplTypeCompletor
|
|
|
1152
1152
|
methods = Types.rbs_methods receiver, method_name.to_sym, args, kwargs, !!block
|
|
1153
1153
|
block_called = false
|
|
1154
1154
|
type_breaks = methods.map do |method, given_params, method_params|
|
|
1155
|
-
receiver_vars = receiver.is_a?(Types::InstanceType) ? receiver.
|
|
1155
|
+
receiver_vars = receiver.is_a?(Types::InstanceType) || receiver.is_a?(Types::InterfaceType) ? receiver.named_params : {}
|
|
1156
1156
|
free_vars = method.type.free_variables - receiver_vars.keys.to_set
|
|
1157
1157
|
vars = receiver_vars.merge Types.match_free_variables(free_vars, method_params, given_params)
|
|
1158
1158
|
if block && method.block && method.block.type.respond_to?(:required_positionals)
|
|
@@ -8,6 +8,7 @@ require_relative 'methods'
|
|
|
8
8
|
module ReplTypeCompletor
|
|
9
9
|
module Types
|
|
10
10
|
OBJECT_TO_TYPE_SAMPLE_SIZE = 50
|
|
11
|
+
EXPANSION_NESTING_LIMIT = 3
|
|
11
12
|
|
|
12
13
|
singleton_class.attr_reader :rbs_builder, :rbs_load_error
|
|
13
14
|
|
|
@@ -75,6 +76,11 @@ module ReplTypeCompletor
|
|
|
75
76
|
def self.rbs_search_method(klass, method_name, singleton)
|
|
76
77
|
return unless rbs_builder
|
|
77
78
|
|
|
79
|
+
if klass.is_a? InterfaceType
|
|
80
|
+
definition = klass.definition
|
|
81
|
+
return definition && definition.methods[method_name]
|
|
82
|
+
end
|
|
83
|
+
|
|
78
84
|
klass.ancestors.each do |ancestor|
|
|
79
85
|
next unless (name = Methods::MODULE_NAME_METHOD.bind_call(ancestor))
|
|
80
86
|
|
|
@@ -93,6 +99,8 @@ module ReplTypeCompletor
|
|
|
93
99
|
[t, t.module_or_class, true]
|
|
94
100
|
in InstanceType
|
|
95
101
|
[t, t.klass, false]
|
|
102
|
+
in InterfaceType
|
|
103
|
+
[t, t, false]
|
|
96
104
|
end
|
|
97
105
|
end
|
|
98
106
|
types = receivers.flat_map do |receiver_type, klass, singleton|
|
|
@@ -115,6 +123,8 @@ module ReplTypeCompletor
|
|
|
115
123
|
t.module_or_class
|
|
116
124
|
in InstanceType
|
|
117
125
|
t.instances
|
|
126
|
+
in InterfaceType
|
|
127
|
+
nil
|
|
118
128
|
end
|
|
119
129
|
end.flatten
|
|
120
130
|
instances = instances.sample(OBJECT_TO_TYPE_SAMPLE_SIZE) if instances.size > OBJECT_TO_TYPE_SAMPLE_SIZE
|
|
@@ -136,6 +146,8 @@ module ReplTypeCompletor
|
|
|
136
146
|
[t, t.module_or_class, true]
|
|
137
147
|
in InstanceType
|
|
138
148
|
[t, t.klass, false]
|
|
149
|
+
in InterfaceType
|
|
150
|
+
[t, t, false]
|
|
139
151
|
end
|
|
140
152
|
end
|
|
141
153
|
has_splat = args_types.include?(nil)
|
|
@@ -157,7 +169,7 @@ module ReplTypeCompletor
|
|
|
157
169
|
args = args_types
|
|
158
170
|
if kwargs_type&.any? && keyreqs.empty? && keyopts.empty? && keyrest.nil?
|
|
159
171
|
kw_value_type = UnionType[*kwargs_type.values]
|
|
160
|
-
args += [InstanceType.new(Hash,
|
|
172
|
+
args += [InstanceType.new(Hash, [SYMBOL, kw_value_type])]
|
|
161
173
|
end
|
|
162
174
|
if has_splat
|
|
163
175
|
score += 1 if args.count(&:itself) <= reqs.size + opts.size + trailings.size
|
|
@@ -193,7 +205,14 @@ module ReplTypeCompletor
|
|
|
193
205
|
end
|
|
194
206
|
|
|
195
207
|
aa, bb = [atypes, btypes].map {|types| (types[InstanceType] || []).map(&:klass) }
|
|
196
|
-
(aa.flat_map(&:ancestors) & bb).any?
|
|
208
|
+
return true if (aa.flat_map(&:ancestors) & bb).any?
|
|
209
|
+
|
|
210
|
+
[[atypes[InterfaceType], b], [btypes[InterfaceType], a]].any? do |interfaces, other|
|
|
211
|
+
interfaces&.any? do |interface|
|
|
212
|
+
methods = interface.methods
|
|
213
|
+
!methods.empty? && other.types.any? { |t| (methods - t.methods).empty? }
|
|
214
|
+
end
|
|
215
|
+
end
|
|
197
216
|
end
|
|
198
217
|
|
|
199
218
|
def self.type_from_object(object)
|
|
@@ -216,6 +235,13 @@ module ReplTypeCompletor
|
|
|
216
235
|
UnionType[*class_instances.map { InstanceType.new _1, nil, _2 }, *modules.uniq.map { SingletonType.new _1 }]
|
|
217
236
|
end
|
|
218
237
|
|
|
238
|
+
def self.union_type_from_objects_list(objects_list)
|
|
239
|
+
objects = objects_list.flat_map do |objects|
|
|
240
|
+
objects.size < OBJECT_TO_TYPE_SAMPLE_SIZE ? objects : objects.sample(OBJECT_TO_TYPE_SAMPLE_SIZE)
|
|
241
|
+
end
|
|
242
|
+
union_type_from_objects(objects)
|
|
243
|
+
end
|
|
244
|
+
|
|
219
245
|
class SingletonType
|
|
220
246
|
attr_reader :module_or_class
|
|
221
247
|
def initialize(module_or_class)
|
|
@@ -263,20 +289,29 @@ module ReplTypeCompletor
|
|
|
263
289
|
@params ||= expand_params
|
|
264
290
|
end
|
|
265
291
|
|
|
292
|
+
def named_params
|
|
293
|
+
return {} if params.empty?
|
|
294
|
+
if Types.rbs_builder
|
|
295
|
+
type_name = Types.rbs_absolute_type_name(Types.class_name_of(@klass))
|
|
296
|
+
names = Types.rbs_builder.build_instance(type_name)&.type_params rescue nil
|
|
297
|
+
end
|
|
298
|
+
names ? names.zip(params).to_h.compact : {}
|
|
299
|
+
end
|
|
300
|
+
|
|
266
301
|
def expand_params
|
|
267
|
-
params = @raw_params ||
|
|
302
|
+
params = @raw_params || []
|
|
268
303
|
return params unless @instances
|
|
269
304
|
|
|
270
305
|
if @klass == Array
|
|
271
|
-
type = Types.
|
|
272
|
-
|
|
306
|
+
type = Types.union_type_from_objects_list(@instances)
|
|
307
|
+
[UnionType[*params[0], *type]]
|
|
273
308
|
elsif @klass == Hash
|
|
274
|
-
key = Types.
|
|
275
|
-
value = Types.
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
309
|
+
key = Types.union_type_from_objects_list(@instances.map(&:keys))
|
|
310
|
+
value = Types.union_type_from_objects_list(@instances.map(&:values))
|
|
311
|
+
[
|
|
312
|
+
UnionType[*params[0], key],
|
|
313
|
+
UnionType[*params[1], value]
|
|
314
|
+
]
|
|
280
315
|
else
|
|
281
316
|
params
|
|
282
317
|
end
|
|
@@ -301,7 +336,12 @@ module ReplTypeCompletor
|
|
|
301
336
|
elsif params.empty?
|
|
302
337
|
inspect_without_params
|
|
303
338
|
else
|
|
304
|
-
|
|
339
|
+
named = named_params
|
|
340
|
+
if named.empty? && !params.empty?
|
|
341
|
+
params_string = "[#{params.map(&:inspect).join(', ')}]"
|
|
342
|
+
else
|
|
343
|
+
params_string = "[#{named.map { "#{_1}: #{_2.inspect}" }.join(', ')}]"
|
|
344
|
+
end
|
|
305
345
|
"#{inspect_without_params}#{params_string}"
|
|
306
346
|
end
|
|
307
347
|
end
|
|
@@ -319,6 +359,38 @@ module ReplTypeCompletor
|
|
|
319
359
|
end
|
|
320
360
|
end
|
|
321
361
|
|
|
362
|
+
class InterfaceType
|
|
363
|
+
attr_reader :name, :args
|
|
364
|
+
|
|
365
|
+
def initialize(name, args = [])
|
|
366
|
+
@name = name
|
|
367
|
+
@args = args
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def definition
|
|
371
|
+
return @definition if defined?(@definition)
|
|
372
|
+
|
|
373
|
+
@definition = (Types.rbs_builder&.build_interface(@name) rescue nil)
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
def transform() = yield(self)
|
|
377
|
+
def methods() = definition ? definition.methods.keys : []
|
|
378
|
+
def all_methods() = methods
|
|
379
|
+
|
|
380
|
+
def named_params
|
|
381
|
+
definition ? definition.type_params.zip(@args).to_h.compact : {}
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def constants() = []
|
|
385
|
+
def types() = [self]
|
|
386
|
+
def nillable?() = false
|
|
387
|
+
def nonnillable() = self
|
|
388
|
+
|
|
389
|
+
def inspect
|
|
390
|
+
args.empty? ? name.name.to_s : "#{name.name}[#{args.map(&:inspect).join(', ')}]"
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
322
394
|
NIL = InstanceType.new NilClass
|
|
323
395
|
OBJECT = InstanceType.new Object
|
|
324
396
|
TRUE = InstanceType.new TrueClass
|
|
@@ -343,24 +415,27 @@ module ReplTypeCompletor
|
|
|
343
415
|
def initialize(*types)
|
|
344
416
|
@types = []
|
|
345
417
|
singleton_types = []
|
|
418
|
+
interface_types = {}
|
|
346
419
|
instance_types = {}
|
|
347
420
|
collect = -> type do
|
|
348
421
|
case type
|
|
349
422
|
in UnionType
|
|
350
423
|
type.types.each(&collect)
|
|
351
424
|
in InstanceType
|
|
352
|
-
params, instances = (instance_types[type.klass] ||= [
|
|
425
|
+
params, instances = (instance_types[type.klass] ||= [[], []])
|
|
353
426
|
type.instances&.each { instances << _1 }
|
|
354
|
-
type.raw_params&.
|
|
355
|
-
(params[
|
|
427
|
+
type.raw_params&.each_with_index do |v, index|
|
|
428
|
+
(params[index] ||= []) << v
|
|
356
429
|
end
|
|
357
430
|
in SingletonType
|
|
358
431
|
singleton_types << type
|
|
432
|
+
in InterfaceType
|
|
433
|
+
interface_types[[type.name, type.args]] ||= type
|
|
359
434
|
end
|
|
360
435
|
end
|
|
361
436
|
types.each(&collect)
|
|
362
|
-
@types = singleton_types.uniq + instance_types.map do |klass, (params, instances)|
|
|
363
|
-
params = params.
|
|
437
|
+
@types = singleton_types.uniq + interface_types.values + instance_types.map do |klass, (params, instances)|
|
|
438
|
+
params = params.map { |v| UnionType[*v] }
|
|
364
439
|
InstanceType.new(klass, params, instances)
|
|
365
440
|
end
|
|
366
441
|
end
|
|
@@ -398,10 +473,18 @@ module ReplTypeCompletor
|
|
|
398
473
|
|
|
399
474
|
def self.array_of(*types)
|
|
400
475
|
type = types.size >= 2 ? UnionType[*types] : types.first || OBJECT
|
|
401
|
-
InstanceType.new
|
|
476
|
+
InstanceType.new(Array, [type])
|
|
402
477
|
end
|
|
403
478
|
|
|
404
|
-
|
|
479
|
+
# Alias may validly recurse through type args (`type json = Integer | Array[json]`)
|
|
480
|
+
# and can expand exponentially (`type b = a | [a]` chains), so expansion is depth limited.
|
|
481
|
+
def self.expand_alias_type(rbs_type, nesting)
|
|
482
|
+
return if nesting >= EXPANSION_NESTING_LIMIT
|
|
483
|
+
|
|
484
|
+
rbs_builder.expand_alias2 rbs_type.name, rbs_type.args rescue nil
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
def self.from_rbs_type(return_type, self_type, extra_vars = {}, nesting = 0)
|
|
405
488
|
case return_type
|
|
406
489
|
when RBS::Types::Bases::Self
|
|
407
490
|
self_type
|
|
@@ -413,12 +496,13 @@ module ReplTypeCompletor
|
|
|
413
496
|
self_type.transform do |type|
|
|
414
497
|
case type
|
|
415
498
|
in SingletonType
|
|
416
|
-
InstanceType.new(
|
|
499
|
+
InstanceType.new(type.module_or_class.is_a?(Class) ? Class : Module)
|
|
417
500
|
in InstanceType
|
|
418
501
|
SingletonType.new type.klass
|
|
502
|
+
in InterfaceType
|
|
503
|
+
CLASS
|
|
419
504
|
end
|
|
420
505
|
end
|
|
421
|
-
UnionType[*types]
|
|
422
506
|
when RBS::Types::Bases::Bool
|
|
423
507
|
BOOLEAN
|
|
424
508
|
when RBS::Types::Bases::Instance
|
|
@@ -433,54 +517,43 @@ module ReplTypeCompletor
|
|
|
433
517
|
end
|
|
434
518
|
when RBS::Types::Union, RBS::Types::Intersection
|
|
435
519
|
# Intersection is unsupported. fallback to union type
|
|
436
|
-
UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars }]
|
|
520
|
+
UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars, nesting }]
|
|
437
521
|
when RBS::Types::Proc
|
|
438
522
|
PROC
|
|
439
523
|
when RBS::Types::Tuple
|
|
440
|
-
elem = UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars }]
|
|
441
|
-
InstanceType.new
|
|
524
|
+
elem = UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars, nesting }]
|
|
525
|
+
InstanceType.new(Array, [elem])
|
|
442
526
|
when RBS::Types::Record
|
|
443
|
-
InstanceType.new
|
|
527
|
+
InstanceType.new(Hash, [SYMBOL, OBJECT])
|
|
444
528
|
when RBS::Types::Literal
|
|
445
529
|
InstanceType.new return_type.literal.class
|
|
446
530
|
when RBS::Types::Variable
|
|
447
531
|
if extra_vars.key? return_type.name
|
|
448
532
|
extra_vars[return_type.name]
|
|
449
|
-
elsif self_type.is_a?
|
|
450
|
-
self_type.
|
|
533
|
+
elsif self_type.is_a?(InstanceType) || self_type.is_a?(InterfaceType)
|
|
534
|
+
self_type.named_params[return_type.name] || OBJECT
|
|
451
535
|
elsif self_type.is_a? UnionType
|
|
452
536
|
types = self_type.types.filter_map do |t|
|
|
453
|
-
t.
|
|
537
|
+
t.named_params[return_type.name] if t.is_a?(InstanceType) || t.is_a?(InterfaceType)
|
|
454
538
|
end
|
|
455
539
|
UnionType[*types]
|
|
456
540
|
else
|
|
457
541
|
OBJECT
|
|
458
542
|
end
|
|
459
543
|
when RBS::Types::Optional
|
|
460
|
-
UnionType[from_rbs_type(return_type.type, self_type, extra_vars), NIL]
|
|
544
|
+
UnionType[from_rbs_type(return_type.type, self_type, extra_vars, nesting), NIL]
|
|
461
545
|
when RBS::Types::Alias
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
INTEGER
|
|
465
|
-
when :boolish
|
|
466
|
-
BOOLEAN
|
|
467
|
-
when :string
|
|
468
|
-
STRING
|
|
469
|
-
else
|
|
470
|
-
# TODO: ???
|
|
471
|
-
OBJECT
|
|
472
|
-
end
|
|
546
|
+
expanded = expand_alias_type(return_type, nesting)
|
|
547
|
+
expanded ? from_rbs_type(expanded, self_type, extra_vars, nesting + 1) : OBJECT
|
|
473
548
|
when RBS::Types::Interface
|
|
474
|
-
|
|
475
|
-
|
|
549
|
+
args = return_type.args.map { from_rbs_type _1, self_type, extra_vars, nesting }
|
|
550
|
+
InterfaceType.new return_type.name, args
|
|
476
551
|
when RBS::Types::ClassInstance
|
|
477
552
|
klass = return_type.name.to_namespace.path.reduce(Object) { _1.const_get _2 }
|
|
478
553
|
if return_type.args
|
|
479
|
-
|
|
480
|
-
names = rbs_builder.build_singleton(return_type.name).type_params
|
|
481
|
-
params = names.map.with_index { [_1, args[_2] || OBJECT] }.to_h
|
|
554
|
+
params = return_type.args.map { from_rbs_type _1, self_type, extra_vars, nesting }
|
|
482
555
|
end
|
|
483
|
-
InstanceType.new
|
|
556
|
+
InstanceType.new(klass, params || [])
|
|
484
557
|
else
|
|
485
558
|
OBJECT
|
|
486
559
|
end
|
|
@@ -498,24 +571,24 @@ module ReplTypeCompletor
|
|
|
498
571
|
accumulator.transform_values { UnionType[*_1] }
|
|
499
572
|
end
|
|
500
573
|
|
|
501
|
-
def self._match_free_variable(vars, rbs_type, value, accumulator)
|
|
574
|
+
def self._match_free_variable(vars, rbs_type, value, accumulator, nesting = 0)
|
|
502
575
|
case [rbs_type, value]
|
|
503
576
|
in [RBS::Types::Variable,]
|
|
504
577
|
(accumulator[rbs_type.name] ||= []) << value if vars.include? rbs_type.name
|
|
505
578
|
in [RBS::Types::ClassInstance, InstanceType]
|
|
506
579
|
names = rbs_builder.build_singleton(rbs_type.name).type_params
|
|
507
580
|
names.zip(rbs_type.args).each do |name, arg|
|
|
508
|
-
v = value.
|
|
509
|
-
_match_free_variable vars, arg, v, accumulator if v
|
|
581
|
+
v = value.named_params[name]
|
|
582
|
+
_match_free_variable vars, arg, v, accumulator, nesting if v
|
|
510
583
|
end
|
|
511
584
|
in [RBS::Types::Tuple, InstanceType] if value.klass == Array
|
|
512
|
-
v = value.params[
|
|
585
|
+
v = value.params[0]
|
|
513
586
|
rbs_type.types.each do |t|
|
|
514
|
-
_match_free_variable vars, t, v, accumulator
|
|
587
|
+
_match_free_variable vars, t, v, accumulator, nesting
|
|
515
588
|
end
|
|
516
589
|
in [RBS::Types::Record, InstanceType] if value.klass == Hash
|
|
517
590
|
# TODO
|
|
518
|
-
in [RBS::Types::Interface,]
|
|
591
|
+
in [RBS::Types::Interface,] if nesting < EXPANSION_NESTING_LIMIT
|
|
519
592
|
definition = rbs_builder.build_interface rbs_type.name
|
|
520
593
|
convert = {}
|
|
521
594
|
definition.type_params.zip(rbs_type.args).each do |from, arg|
|
|
@@ -527,13 +600,20 @@ module ReplTypeCompletor
|
|
|
527
600
|
return_type = method_return_type value, method_name
|
|
528
601
|
method.defs.each do |method_def|
|
|
529
602
|
interface_return_type = method_def.type.type.return_type
|
|
530
|
-
_match_free_variable convert, interface_return_type, return_type, ac
|
|
603
|
+
_match_free_variable convert, interface_return_type, return_type, ac, nesting + 1
|
|
531
604
|
end
|
|
532
605
|
end
|
|
533
606
|
convert.each do |from, to|
|
|
534
607
|
values = ac[from]
|
|
535
608
|
(accumulator[to] ||= []).concat values if values
|
|
536
609
|
end
|
|
610
|
+
in [RBS::Types::Union,]
|
|
611
|
+
rbs_type.types.each do |t|
|
|
612
|
+
_match_free_variable vars, t, value, accumulator, nesting
|
|
613
|
+
end
|
|
614
|
+
in [RBS::Types::Alias,]
|
|
615
|
+
expanded = expand_alias_type(rbs_type, nesting)
|
|
616
|
+
_match_free_variable vars, expanded, value, accumulator, nesting + 1 if expanded
|
|
537
617
|
else
|
|
538
618
|
end
|
|
539
619
|
end
|