rbi 0.3.14 → 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/Gemfile +1 -1
- data/lib/rbi/loc.rb +7 -0
- data/lib/rbi/rbs/method_type_translator.rb +25 -5
- data/lib/rbi/rbs/type_translator.rb +48 -29
- data/lib/rbi/rbs_printer.rb +14 -5
- data/lib/rbi/version.rb +1 -1
- data/rbi/rbi.rbi +31 -4
- 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
|
data/Gemfile
CHANGED
|
@@ -10,7 +10,7 @@ group(:development, :test) do
|
|
|
10
10
|
gem("minitest")
|
|
11
11
|
gem("minitest-reporters")
|
|
12
12
|
gem("rake", "~> 13.4")
|
|
13
|
-
gem("rubocop", "~> 1.
|
|
13
|
+
gem("rubocop", "~> 1.88", require: false)
|
|
14
14
|
gem("rubocop-shopify", require: false)
|
|
15
15
|
gem("rubocop-sorbet", require: false)
|
|
16
16
|
gem("sorbet", ">= 0.5.9204", require: false)
|
data/lib/rbi/loc.rb
CHANGED
|
@@ -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/rbs_printer.rb
CHANGED
|
@@ -25,9 +25,17 @@ module RBI
|
|
|
25
25
|
#| ?indent: Integer,
|
|
26
26
|
#| ?print_locs: bool,
|
|
27
27
|
#| ?positional_names: bool,
|
|
28
|
-
#| ?max_line_length: Integer
|
|
28
|
+
#| ?max_line_length: Integer?,
|
|
29
|
+
#| ?force_multiline_signatures: bool
|
|
29
30
|
#| ) -> void
|
|
30
|
-
def initialize(
|
|
31
|
+
def initialize(
|
|
32
|
+
out: $stdout,
|
|
33
|
+
indent: 0,
|
|
34
|
+
print_locs: false,
|
|
35
|
+
positional_names: true,
|
|
36
|
+
max_line_length: nil,
|
|
37
|
+
force_multiline_signatures: false
|
|
38
|
+
)
|
|
31
39
|
super()
|
|
32
40
|
@out = out
|
|
33
41
|
@current_indent = indent
|
|
@@ -36,6 +44,7 @@ module RBI
|
|
|
36
44
|
@previous_node = nil #: Node?
|
|
37
45
|
@positional_names = positional_names #: bool
|
|
38
46
|
@max_line_length = max_line_length #: Integer?
|
|
47
|
+
@force_multiline_signatures = force_multiline_signatures #: bool
|
|
39
48
|
end
|
|
40
49
|
|
|
41
50
|
# Printing
|
|
@@ -406,10 +415,10 @@ module RBI
|
|
|
406
415
|
@out = old_out
|
|
407
416
|
|
|
408
417
|
max_line_length = @max_line_length
|
|
409
|
-
if
|
|
410
|
-
print(new_out.string)
|
|
411
|
-
else
|
|
418
|
+
if @force_multiline_signatures || (max_line_length && new_out.string.size > max_line_length)
|
|
412
419
|
print_method_sig_multiline(node, sig)
|
|
420
|
+
else
|
|
421
|
+
print(new_out.string)
|
|
413
422
|
end
|
|
414
423
|
end
|
|
415
424
|
|
data/lib/rbi/version.rb
CHANGED
data/rbi/rbi.rbi
CHANGED
|
@@ -600,6 +600,9 @@ class RBI::Loc
|
|
|
600
600
|
sig { params(other: ::RBI::Loc).returns(::RBI::Loc) }
|
|
601
601
|
def join(other); end
|
|
602
602
|
|
|
603
|
+
sig { returns(T::Boolean) }
|
|
604
|
+
def multiline?; end
|
|
605
|
+
|
|
603
606
|
sig { returns(T.nilable(::String)) }
|
|
604
607
|
def source; end
|
|
605
608
|
|
|
@@ -1386,8 +1389,8 @@ RBI::Public::DEFAULT = T.let(T.unsafe(nil), RBI::Public)
|
|
|
1386
1389
|
module RBI::RBS; end
|
|
1387
1390
|
|
|
1388
1391
|
class RBI::RBS::MethodTypeTranslator
|
|
1389
|
-
sig { params(method: ::RBI::Method).void }
|
|
1390
|
-
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
|
|
1391
1394
|
|
|
1392
1395
|
sig { returns(::RBI::Sig) }
|
|
1393
1396
|
def result; end
|
|
@@ -1417,7 +1420,23 @@ end
|
|
|
1417
1420
|
|
|
1418
1421
|
class RBI::RBS::MethodTypeTranslator::Error < ::RBI::Error; end
|
|
1419
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
|
+
|
|
1420
1436
|
class RBI::RBS::TypeTranslator
|
|
1437
|
+
sig { params(options: ::RBI::RBS::MethodTypeTranslator::Options).void }
|
|
1438
|
+
def initialize(options: T.unsafe(nil)); end
|
|
1439
|
+
|
|
1421
1440
|
sig do
|
|
1422
1441
|
params(
|
|
1423
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)
|
|
@@ -1427,6 +1446,9 @@ class RBI::RBS::TypeTranslator
|
|
|
1427
1446
|
|
|
1428
1447
|
private
|
|
1429
1448
|
|
|
1449
|
+
sig { params(type_name: ::String).returns(::String) }
|
|
1450
|
+
def erase_t_generic_type(type_name); end
|
|
1451
|
+
|
|
1430
1452
|
sig { params(type: ::RBS::Types::ClassInstance).returns(::RBI::Type) }
|
|
1431
1453
|
def translate_class_instance(type); end
|
|
1432
1454
|
|
|
@@ -1449,7 +1471,11 @@ class RBI::RBS::TypeTranslator
|
|
|
1449
1471
|
end
|
|
1450
1472
|
end
|
|
1451
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)
|
|
1452
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)
|
|
1453
1479
|
|
|
1454
1480
|
class RBI::RBSComment < ::RBI::Comment
|
|
1455
1481
|
sig { params(other: ::Object).returns(T::Boolean) }
|
|
@@ -1463,10 +1489,11 @@ class RBI::RBSPrinter < ::RBI::Visitor
|
|
|
1463
1489
|
indent: ::Integer,
|
|
1464
1490
|
print_locs: T::Boolean,
|
|
1465
1491
|
positional_names: T::Boolean,
|
|
1466
|
-
max_line_length: T.nilable(::Integer)
|
|
1492
|
+
max_line_length: T.nilable(::Integer),
|
|
1493
|
+
force_multiline_signatures: T::Boolean
|
|
1467
1494
|
).void
|
|
1468
1495
|
end
|
|
1469
|
-
def initialize(out: T.unsafe(nil), indent: T.unsafe(nil), print_locs: T.unsafe(nil), positional_names: T.unsafe(nil), max_line_length: T.unsafe(nil)); end
|
|
1496
|
+
def initialize(out: T.unsafe(nil), indent: T.unsafe(nil), print_locs: T.unsafe(nil), positional_names: T.unsafe(nil), max_line_length: T.unsafe(nil), force_multiline_signatures: T.unsafe(nil)); end
|
|
1470
1497
|
|
|
1471
1498
|
sig { returns(::Integer) }
|
|
1472
1499
|
def current_indent; end
|