23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/yoda/model/environment/value_factory.rb', line 23
def resolve_value_by_rbs_type(type, context: ValueResolveContext.empty)
return resolve_value_by_rbs_type(type.wrapped_type, context: type.context) if type.respond_to?(:act_as_type_wrapper?)
case type
when RBS::Types::Bases::Any
Values::EmptyValue.new
when RBS::Types::Bases::Top
Values::EmptyValue.new
when RBS::Types::Bases::Bottom
Values::EmptyValue.new
when RBS::Types::Bases::Void
Values::EmptyValue.new
when RBS::Types::Bases::Bool
Values::UnionValue.new(
resolve_instance_by_rbs_type_name(TypeName("::TrueClass")),
resolve_instance_by_rbs_type_name(TypeName("::FalseClass")),
)
when RBS::Types::Bases::Nil
resolve_instance_by_rbs_type_name(TypeName("::NilClass"))
when RBS::Types::ClassSingleton
resolve_singleton_by_rbs_type_name(type.name)
when RBS::Types::ClassInstance
resolve_instance_by_rbs_type_name(type.name, args: type.args)
when RBS::Types::Interface
args = type.args.map {|arg| resolve_value_by_rbs_type(arg, context: context) }
resolve_interface_by_rbs_type_name(type.name, args: type.args)
when RBS::Types::Union
Values::UnionValue.new(
*type.types.flat_map { |ty| resolve_value_by_rbs_type(ty, context: context) }
)
when RBS::Types::Optional
Values::UnionValue.new(
resolve_value_by_rbs_type(type.type),
resolve_instance_by_rbs_type_name(TypeName("::NilClass")),
)
when RBS::Types::Intersection
Values::IntersectionValue.new(
*type.types.map { |ty| resolve_value_by_rbs_type(ty, context: context) }
)
when RBS::Types::Literal
literal_instance(type.literal)
when RBS::Types::Tuple
resolve_instance_by_rbs_type_name(TypeName("::Array"))
when RBS::Types::Record
resolve_instance_by_rbs_type_name(TypeName("::Hash"))
when RBS::Types::Proc
resolve_instance_by_rbs_type_name(TypeName("::Proc"))
when RBS::Types::Alias
rbs_alias = environment.rbs_environment.alias_decls[type.name]&.decl
if rbs_alias
resolve_value_by_rbs_type(rbs_alias.type)
else
Values::EmptyValue.new
end
when RBS::Types::Variable
Logger.warn("Value factory does not has proper information to convert #{type}.")
Values::EmptyValue.new
when RBS::Types::Bases::Self
if context.self_type
resolve_value_by_rbs_type(context.self_type)
else
Logger.warn("Value factory does not has proper information to convert #{type}.")
Values::EmptyValue.new
end
when RBS::Types::Bases::Class
if context.self_type
resolve_value_by_rbs_type(context.self_type).singleton_class_value
else
Logger.warn("Value factory does not has proper information to convert #{type}.")
Values::EmptyValue.new
end
when RBS::Types::Bases::Instance
if context.self_type
resolve_value_by_rbs_type(context.self_type).instance_value
else
Logger.warn("Value factory does not has proper information to convert #{type}.")
Values::EmptyValue.new
end
else
raise "Unexpected type given: #{type}"
end
end
|