rbi 0.3.15 → 0.3.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 +4 -4
- data/lib/rbi/rbs/method_type_translator.rb +25 -5
- data/lib/rbi/rbs/type_translator.rb +48 -29
- data/lib/rbi/version.rb +1 -1
- data/rbi/rbi.rbi +25 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c7c65440f9d48552a65ab2c087b141ab728a4eb03ba330b55e885df49792379
|
|
4
|
+
data.tar.gz: 4ed1352e4bdf88b220597979bc3b478e7f66227e156ce5fbfe71fcb5bf73ad34
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9d21c501fe9927818ed2afe2c7e68e0e4838c1d835a5c40ccbc57d49144fee05a4e8c2a447893bc4d8d6a0e774d4f5757791ec8202388a37e33fa445ec2946c8
|
|
7
|
+
data.tar.gz: 4b24fcee126547e8d0981e419994a1a39ff8f70a55aa93259fcaf7837d837a8842769da9205db28d414ed9920fb3a651eccdaa36c91dcf67ca323833926bca6d
|
|
@@ -15,20 +15,40 @@ module RBI
|
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
class Options
|
|
19
|
+
#: bool
|
|
20
|
+
attr_reader :erase_generic_types
|
|
21
|
+
|
|
22
|
+
#: (?erase_generic_types: bool) -> void
|
|
23
|
+
def initialize(erase_generic_types: false)
|
|
24
|
+
@erase_generic_types = erase_generic_types
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
@default = new.freeze #: Options
|
|
28
|
+
class << self
|
|
29
|
+
#: Options
|
|
30
|
+
attr_reader :default
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
18
34
|
#: Sig
|
|
19
35
|
attr_reader :result
|
|
20
36
|
|
|
21
|
-
#: (Method) -> void
|
|
22
|
-
def initialize(method)
|
|
37
|
+
#: (Method, ?options: Options) -> void
|
|
38
|
+
def initialize(method, options: Options.default)
|
|
23
39
|
@method = method
|
|
40
|
+
@options = options
|
|
41
|
+
|
|
24
42
|
@result = Sig.new #: Sig
|
|
25
|
-
@type_translator = TypeTranslator.new #: TypeTranslator
|
|
43
|
+
@type_translator = TypeTranslator.new(options: @options) #: TypeTranslator
|
|
26
44
|
end
|
|
27
45
|
|
|
28
46
|
#: (::RBS::MethodType) -> void
|
|
29
47
|
def visit(type)
|
|
30
|
-
|
|
31
|
-
|
|
48
|
+
unless @options.erase_generic_types
|
|
49
|
+
type.type_params.each do |param|
|
|
50
|
+
result.type_params << param.name
|
|
51
|
+
end
|
|
32
52
|
end
|
|
33
53
|
|
|
34
54
|
visit_function_type(type.type)
|
|
@@ -35,6 +35,13 @@ module RBI
|
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
Options = MethodTypeTranslator::Options
|
|
39
|
+
|
|
40
|
+
#: (?options: Options) -> void
|
|
41
|
+
def initialize(options: Options.default)
|
|
42
|
+
@options = options
|
|
43
|
+
end
|
|
44
|
+
|
|
38
45
|
#: (rbs_type) -> Type
|
|
39
46
|
def translate(type)
|
|
40
47
|
case type
|
|
@@ -59,7 +66,8 @@ module RBI
|
|
|
59
66
|
when ::RBS::Types::Bases::Void
|
|
60
67
|
Type.void
|
|
61
68
|
when ::RBS::Types::ClassSingleton
|
|
62
|
-
|
|
69
|
+
type_arg = type.args.first
|
|
70
|
+
type_parameter = translate(type_arg) if type_arg && !@options.erase_generic_types
|
|
63
71
|
Type.class_of(Type.simple(type.name.to_s), type_parameter)
|
|
64
72
|
when ::RBS::Types::ClassInstance
|
|
65
73
|
translate_class_instance(type)
|
|
@@ -99,7 +107,11 @@ module RBI
|
|
|
99
107
|
when ::RBS::Types::UntypedFunction
|
|
100
108
|
Type.proc.params(arg0: Type.untyped).returns(Type.untyped)
|
|
101
109
|
when ::RBS::Types::Variable
|
|
102
|
-
|
|
110
|
+
if @options.erase_generic_types
|
|
111
|
+
Type.anything
|
|
112
|
+
else
|
|
113
|
+
Type.type_parameter(type.name)
|
|
114
|
+
end
|
|
103
115
|
else
|
|
104
116
|
type #: absurd
|
|
105
117
|
end
|
|
@@ -121,10 +133,14 @@ module RBI
|
|
|
121
133
|
|
|
122
134
|
#: (::RBS::Types::ClassInstance) -> Type
|
|
123
135
|
def translate_class_instance(type)
|
|
124
|
-
|
|
136
|
+
type_name = type.name.to_s
|
|
137
|
+
return Type.simple(type_name) if type.args.empty?
|
|
125
138
|
|
|
126
|
-
|
|
127
|
-
|
|
139
|
+
if @options.erase_generic_types
|
|
140
|
+
Type.simple(erase_t_generic_type(type_name))
|
|
141
|
+
else
|
|
142
|
+
Type.generic(translate_t_generic_type(type_name), *type.args.map { |arg| translate(arg) })
|
|
143
|
+
end
|
|
128
144
|
end
|
|
129
145
|
|
|
130
146
|
#: (::RBS::Types::Function) -> Type
|
|
@@ -174,32 +190,35 @@ module RBI
|
|
|
174
190
|
proc
|
|
175
191
|
end
|
|
176
192
|
|
|
193
|
+
RUNTIME_GENERIC_TYPES = [
|
|
194
|
+
"Array",
|
|
195
|
+
"Class",
|
|
196
|
+
"Enumerable",
|
|
197
|
+
"Enumerator",
|
|
198
|
+
"Enumerator::Chain",
|
|
199
|
+
"Enumerator::Lazy",
|
|
200
|
+
"Hash",
|
|
201
|
+
"Module",
|
|
202
|
+
"Set",
|
|
203
|
+
"Range",
|
|
204
|
+
].freeze #: Array[String]
|
|
205
|
+
|
|
206
|
+
GENERIC_TYPE_TO_SORBET_GENERIC_TYPE = RUNTIME_GENERIC_TYPES.flat_map do |type|
|
|
207
|
+
[[type, "::T::#{type}"], ["::#{type}", "::T::#{type}"]]
|
|
208
|
+
end.to_h.freeze #: Hash[String, String]
|
|
209
|
+
|
|
210
|
+
SORBET_GENERIC_TYPE_TO_GENERIC_TYPE = RUNTIME_GENERIC_TYPES.flat_map do |type|
|
|
211
|
+
[["T::#{type}", "::#{type}"], ["::T::#{type}", "::#{type}"]]
|
|
212
|
+
end.to_h.freeze #: Hash[String, String]
|
|
213
|
+
|
|
177
214
|
#: (String type_name) -> String
|
|
178
215
|
def translate_t_generic_type(type_name)
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
"::T::Enumerable"
|
|
186
|
-
when "Enumerator"
|
|
187
|
-
"::T::Enumerator"
|
|
188
|
-
when "Enumerator::Chain"
|
|
189
|
-
"::T::Enumerator::Chain"
|
|
190
|
-
when "Enumerator::Lazy"
|
|
191
|
-
"::T::Enumerator::Lazy"
|
|
192
|
-
when "Hash"
|
|
193
|
-
"::T::Hash"
|
|
194
|
-
when "Module"
|
|
195
|
-
"::T::Module"
|
|
196
|
-
when "Set"
|
|
197
|
-
"::T::Set"
|
|
198
|
-
when "Range"
|
|
199
|
-
"::T::Range"
|
|
200
|
-
else
|
|
201
|
-
type_name
|
|
202
|
-
end
|
|
216
|
+
GENERIC_TYPE_TO_SORBET_GENERIC_TYPE.fetch(type_name, type_name)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
#: (String type_name) -> String
|
|
220
|
+
def erase_t_generic_type(type_name)
|
|
221
|
+
SORBET_GENERIC_TYPE_TO_GENERIC_TYPE.fetch(type_name, type_name)
|
|
203
222
|
end
|
|
204
223
|
end
|
|
205
224
|
end
|
data/lib/rbi/version.rb
CHANGED
data/rbi/rbi.rbi
CHANGED
|
@@ -1389,8 +1389,8 @@ RBI::Public::DEFAULT = T.let(T.unsafe(nil), RBI::Public)
|
|
|
1389
1389
|
module RBI::RBS; end
|
|
1390
1390
|
|
|
1391
1391
|
class RBI::RBS::MethodTypeTranslator
|
|
1392
|
-
sig { params(method: ::RBI::Method).void }
|
|
1393
|
-
def initialize(method); end
|
|
1392
|
+
sig { params(method: ::RBI::Method, options: ::RBI::RBS::MethodTypeTranslator::Options).void }
|
|
1393
|
+
def initialize(method, options: T.unsafe(nil)); end
|
|
1394
1394
|
|
|
1395
1395
|
sig { returns(::RBI::Sig) }
|
|
1396
1396
|
def result; end
|
|
@@ -1420,7 +1420,23 @@ end
|
|
|
1420
1420
|
|
|
1421
1421
|
class RBI::RBS::MethodTypeTranslator::Error < ::RBI::Error; end
|
|
1422
1422
|
|
|
1423
|
+
class RBI::RBS::MethodTypeTranslator::Options
|
|
1424
|
+
sig { params(erase_generic_types: T::Boolean).void }
|
|
1425
|
+
def initialize(erase_generic_types: T.unsafe(nil)); end
|
|
1426
|
+
|
|
1427
|
+
sig { returns(T::Boolean) }
|
|
1428
|
+
def erase_generic_types; end
|
|
1429
|
+
|
|
1430
|
+
class << self
|
|
1431
|
+
sig { returns(::RBI::RBS::MethodTypeTranslator::Options) }
|
|
1432
|
+
def default; end
|
|
1433
|
+
end
|
|
1434
|
+
end
|
|
1435
|
+
|
|
1423
1436
|
class RBI::RBS::TypeTranslator
|
|
1437
|
+
sig { params(options: ::RBI::RBS::MethodTypeTranslator::Options).void }
|
|
1438
|
+
def initialize(options: T.unsafe(nil)); end
|
|
1439
|
+
|
|
1424
1440
|
sig do
|
|
1425
1441
|
params(
|
|
1426
1442
|
type: T.any(::RBS::Types::Alias, ::RBS::Types::Bases::Any, ::RBS::Types::Bases::Bool, ::RBS::Types::Bases::Bottom, ::RBS::Types::Bases::Class, ::RBS::Types::Bases::Instance, ::RBS::Types::Bases::Nil, ::RBS::Types::Bases::Self, ::RBS::Types::Bases::Top, ::RBS::Types::Bases::Void, ::RBS::Types::ClassInstance, ::RBS::Types::ClassSingleton, ::RBS::Types::Function, ::RBS::Types::Interface, ::RBS::Types::Intersection, ::RBS::Types::Literal, ::RBS::Types::Optional, ::RBS::Types::Proc, ::RBS::Types::Record, ::RBS::Types::Tuple, ::RBS::Types::Union, ::RBS::Types::UntypedFunction, ::RBS::Types::Variable)
|
|
@@ -1430,6 +1446,9 @@ class RBI::RBS::TypeTranslator
|
|
|
1430
1446
|
|
|
1431
1447
|
private
|
|
1432
1448
|
|
|
1449
|
+
sig { params(type_name: ::String).returns(::String) }
|
|
1450
|
+
def erase_t_generic_type(type_name); end
|
|
1451
|
+
|
|
1433
1452
|
sig { params(type: ::RBS::Types::ClassInstance).returns(::RBI::Type) }
|
|
1434
1453
|
def translate_class_instance(type); end
|
|
1435
1454
|
|
|
@@ -1452,7 +1471,11 @@ class RBI::RBS::TypeTranslator
|
|
|
1452
1471
|
end
|
|
1453
1472
|
end
|
|
1454
1473
|
|
|
1474
|
+
RBI::RBS::TypeTranslator::GENERIC_TYPE_TO_SORBET_GENERIC_TYPE = T.let(T.unsafe(nil), Hash)
|
|
1475
|
+
RBI::RBS::TypeTranslator::Options = RBI::RBS::MethodTypeTranslator::Options
|
|
1476
|
+
RBI::RBS::TypeTranslator::RUNTIME_GENERIC_TYPES = T.let(T.unsafe(nil), Array)
|
|
1455
1477
|
RBI::RBS::TypeTranslator::RbsType = T.type_alias { T.any(::RBS::Types::Alias, ::RBS::Types::Bases::Any, ::RBS::Types::Bases::Bool, ::RBS::Types::Bases::Bottom, ::RBS::Types::Bases::Class, ::RBS::Types::Bases::Instance, ::RBS::Types::Bases::Nil, ::RBS::Types::Bases::Self, ::RBS::Types::Bases::Top, ::RBS::Types::Bases::Void, ::RBS::Types::ClassInstance, ::RBS::Types::ClassSingleton, ::RBS::Types::Function, ::RBS::Types::Interface, ::RBS::Types::Intersection, ::RBS::Types::Literal, ::RBS::Types::Optional, ::RBS::Types::Proc, ::RBS::Types::Record, ::RBS::Types::Tuple, ::RBS::Types::Union, ::RBS::Types::UntypedFunction, ::RBS::Types::Variable) }
|
|
1478
|
+
RBI::RBS::TypeTranslator::SORBET_GENERIC_TYPE_TO_GENERIC_TYPE = T.let(T.unsafe(nil), Hash)
|
|
1456
1479
|
|
|
1457
1480
|
class RBI::RBSComment < ::RBI::Comment
|
|
1458
1481
|
sig { params(other: ::Object).returns(T::Boolean) }
|