Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/repl_type_completor/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ def self.union_type_from_objects(objects)
UnionType[*class_instances.map { InstanceType.new _1, nil, _2 }, *modules.uniq.map { SingletonType.new _1 }]
end

def self.union_type_from_objects_list(objects_list)
objects = objects_list.flat_map do |objects|
objects.size < OBJECT_TO_TYPE_SAMPLE_SIZE ? objects : objects.sample(OBJECT_TO_TYPE_SAMPLE_SIZE)
end
union_type_from_objects(objects)
end

class SingletonType
attr_reader :module_or_class
def initialize(module_or_class)
Expand Down Expand Up @@ -268,11 +275,11 @@ def expand_params
return params unless @instances

if @klass == Array
type = Types.union_type_from_objects(@instances.flatten(1))
type = Types.union_type_from_objects_list(@instances)
{ Elem: UnionType[*params[:Elem], *type] }
elsif @klass == Hash
key = Types.union_type_from_objects(@instances.map(&:keys).flatten(1))
value = Types.union_type_from_objects(@instances.map(&:values).flatten(1))
key = Types.union_type_from_objects_list(@instances.map(&:keys))
value = Types.union_type_from_objects_list(@instances.map(&:values))
{
K: UnionType[*params[:K], key],
V: UnionType[*params[:V], value]
Expand Down
Loading