rbi 0.3.5 → 0.3.7
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/formatter.rb +9 -1
- data/lib/rbi/index.rb +1 -1
- data/lib/rbi/loc.rb +7 -1
- data/lib/rbi/model.rb +116 -15
- data/lib/rbi/parser.rb +19 -16
- data/lib/rbi/printer.rb +2 -0
- data/lib/rbi/rbs/type_translator.rb +14 -2
- data/lib/rbi/rbs_printer.rb +2 -0
- data/lib/rbi/rewriters/attr_to_methods.rb +9 -2
- data/lib/rbi/rewriters/merge_trees.rb +5 -1
- data/lib/rbi/type.rb +50 -4
- data/lib/rbi/type_parser.rb +32 -0
- data/lib/rbi/type_visitor.rb +5 -0
- data/lib/rbi/version.rb +1 -1
- data/rbi/rbi.rbi +77 -489
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d0977bf3c0892a76c8d66ec2f295a62948530bcd2eee11911c50e36ac1bf5402
|
|
4
|
+
data.tar.gz: ae51e44eb13fe8045f200a8a0353daa9969c4389f99c73758713fa0a9b2b9b0f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32bafbbfffee840fe60a1633eb8b408aca4c8e39b5359ce3deca5185ab3003ac43b6c6cab4c4639d9e87e12afabcf1ee042bc9b5a56a231302354f884b2c27ea
|
|
7
|
+
data.tar.gz: da025803fafaf40714356d02dfe77a705a0de38050bdce9616c6e35f68479b958aa0eb5b596ddf146ef1493932a70fead22f7cb745a28e7c15a40ee85a850c2e
|
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.3")
|
|
13
|
-
gem("rubocop", "~> 1.
|
|
13
|
+
gem("rubocop", "~> 1.81", 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/formatter.rb
CHANGED
|
@@ -6,7 +6,15 @@ module RBI
|
|
|
6
6
|
#: Integer?
|
|
7
7
|
attr_accessor :max_line_length
|
|
8
8
|
|
|
9
|
-
#: (
|
|
9
|
+
#: (
|
|
10
|
+
#| ?add_sig_templates: bool,
|
|
11
|
+
#| ?group_nodes: bool,
|
|
12
|
+
#| ?max_line_length: Integer?,
|
|
13
|
+
#| ?nest_singleton_methods: bool,
|
|
14
|
+
#| ?nest_non_public_members: bool,
|
|
15
|
+
#| ?sort_nodes: bool,
|
|
16
|
+
#| ?replace_attributes_with_methods: bool
|
|
17
|
+
#| ) -> void
|
|
10
18
|
def initialize(
|
|
11
19
|
add_sig_templates: false,
|
|
12
20
|
group_nodes: false,
|
data/lib/rbi/index.rb
CHANGED
data/lib/rbi/loc.rb
CHANGED
|
@@ -22,7 +22,13 @@ module RBI
|
|
|
22
22
|
#: Integer?
|
|
23
23
|
attr_reader :begin_line, :end_line, :begin_column, :end_column
|
|
24
24
|
|
|
25
|
-
#: (
|
|
25
|
+
#: (
|
|
26
|
+
#| ?file: String?,
|
|
27
|
+
#| ?begin_line: Integer?,
|
|
28
|
+
#| ?end_line: Integer?,
|
|
29
|
+
#| ?begin_column: Integer?,
|
|
30
|
+
#| ?end_column: Integer?
|
|
31
|
+
#| ) -> void
|
|
26
32
|
def initialize(file: nil, begin_line: nil, end_line: nil, begin_column: nil, end_column: nil)
|
|
27
33
|
@file = file
|
|
28
34
|
@begin_line = begin_line
|
data/lib/rbi/model.rb
CHANGED
|
@@ -163,7 +163,7 @@ module RBI
|
|
|
163
163
|
class Scope < Tree
|
|
164
164
|
# @abstract
|
|
165
165
|
#: -> String
|
|
166
|
-
def fully_qualified_name
|
|
166
|
+
def fully_qualified_name = raise NotImplementedError, "Abstract method called"
|
|
167
167
|
|
|
168
168
|
# @override
|
|
169
169
|
#: -> String
|
|
@@ -240,7 +240,13 @@ module RBI
|
|
|
240
240
|
#: bool
|
|
241
241
|
attr_accessor :keyword_init
|
|
242
242
|
|
|
243
|
-
#: (
|
|
243
|
+
#: (
|
|
244
|
+
#| String name,
|
|
245
|
+
#| ?members: Array[Symbol],
|
|
246
|
+
#| ?keyword_init: bool,
|
|
247
|
+
#| ?loc: Loc?,
|
|
248
|
+
#| ?comments: Array[Comment]
|
|
249
|
+
#| ) ?{ (Struct struct) -> void } -> void
|
|
244
250
|
def initialize(name, members: [], keyword_init: false, loc: nil, comments: [], &block)
|
|
245
251
|
super(loc: loc, comments: comments) {}
|
|
246
252
|
@name = name
|
|
@@ -299,7 +305,14 @@ module RBI
|
|
|
299
305
|
#: Array[Sig]
|
|
300
306
|
attr_reader :sigs
|
|
301
307
|
|
|
302
|
-
#: (
|
|
308
|
+
#: (
|
|
309
|
+
#| Symbol name,
|
|
310
|
+
#| Array[Symbol] names,
|
|
311
|
+
#| ?visibility: Visibility,
|
|
312
|
+
#| ?sigs: Array[Sig],
|
|
313
|
+
#| ?loc: Loc?,
|
|
314
|
+
#| ?comments: Array[Comment]
|
|
315
|
+
#| ) -> void
|
|
303
316
|
def initialize(name, names, visibility: Public.new, sigs: [], loc: nil, comments: [])
|
|
304
317
|
super(loc: loc, comments: comments)
|
|
305
318
|
@names = [name, *names] #: Array[Symbol]
|
|
@@ -309,11 +322,18 @@ module RBI
|
|
|
309
322
|
|
|
310
323
|
# @abstract
|
|
311
324
|
#: -> Array[String]
|
|
312
|
-
def fully_qualified_names
|
|
325
|
+
def fully_qualified_names = raise NotImplementedError, "Abstract method called"
|
|
313
326
|
end
|
|
314
327
|
|
|
315
328
|
class AttrAccessor < Attr
|
|
316
|
-
#: (
|
|
329
|
+
#: (
|
|
330
|
+
#| Symbol name,
|
|
331
|
+
#| *Symbol names,
|
|
332
|
+
#| ?visibility: Visibility,
|
|
333
|
+
#| ?sigs: Array[Sig],
|
|
334
|
+
#| ?loc: Loc?,
|
|
335
|
+
#| ?comments: Array[Comment]
|
|
336
|
+
#| ) ?{ (AttrAccessor node) -> void } -> void
|
|
317
337
|
def initialize(name, *names, visibility: Public.new, sigs: [], loc: nil, comments: [], &block)
|
|
318
338
|
super(name, names, loc: loc, visibility: visibility, sigs: sigs, comments: comments)
|
|
319
339
|
block&.call(self)
|
|
@@ -335,7 +355,14 @@ module RBI
|
|
|
335
355
|
end
|
|
336
356
|
|
|
337
357
|
class AttrReader < Attr
|
|
338
|
-
#: (
|
|
358
|
+
#: (
|
|
359
|
+
#| Symbol name,
|
|
360
|
+
#| *Symbol names,
|
|
361
|
+
#| ?visibility: Visibility,
|
|
362
|
+
#| ?sigs: Array[Sig],
|
|
363
|
+
#| ?loc: Loc?,
|
|
364
|
+
#| ?comments: Array[Comment]
|
|
365
|
+
#| ) ?{ (AttrReader node) -> void } -> void
|
|
339
366
|
def initialize(name, *names, visibility: Public.new, sigs: [], loc: nil, comments: [], &block)
|
|
340
367
|
super(name, names, loc: loc, visibility: visibility, sigs: sigs, comments: comments)
|
|
341
368
|
block&.call(self)
|
|
@@ -357,7 +384,14 @@ module RBI
|
|
|
357
384
|
end
|
|
358
385
|
|
|
359
386
|
class AttrWriter < Attr
|
|
360
|
-
#: (
|
|
387
|
+
#: (
|
|
388
|
+
#| Symbol name,
|
|
389
|
+
#| *Symbol names,
|
|
390
|
+
#| ?visibility: Visibility,
|
|
391
|
+
#| ?sigs: Array[Sig],
|
|
392
|
+
#| ?loc: Loc?,
|
|
393
|
+
#| ?comments: Array[Comment]
|
|
394
|
+
#| ) ?{ (AttrWriter node) -> void } -> void
|
|
361
395
|
def initialize(name, *names, visibility: Public.new, sigs: [], loc: nil, comments: [], &block)
|
|
362
396
|
super(name, names, loc: loc, visibility: visibility, sigs: sigs, comments: comments)
|
|
363
397
|
block&.call(self)
|
|
@@ -396,7 +430,15 @@ module RBI
|
|
|
396
430
|
#: Array[Sig]
|
|
397
431
|
attr_accessor :sigs
|
|
398
432
|
|
|
399
|
-
#: (
|
|
433
|
+
#: (
|
|
434
|
+
#| String name,
|
|
435
|
+
#| ?params: Array[Param],
|
|
436
|
+
#| ?is_singleton: bool,
|
|
437
|
+
#| ?visibility: Visibility,
|
|
438
|
+
#| ?sigs: Array[Sig],
|
|
439
|
+
#| ?loc: Loc?,
|
|
440
|
+
#| ?comments: Array[Comment]
|
|
441
|
+
#| ) ?{ (Method node) -> void } -> void
|
|
400
442
|
def initialize(
|
|
401
443
|
name,
|
|
402
444
|
params: [],
|
|
@@ -456,7 +498,15 @@ module RBI
|
|
|
456
498
|
@params << BlockParam.new(name)
|
|
457
499
|
end
|
|
458
500
|
|
|
459
|
-
#: (
|
|
501
|
+
#: (
|
|
502
|
+
#| ?params: Array[SigParam],
|
|
503
|
+
#| ?return_type: (String | Type),
|
|
504
|
+
#| ?is_abstract: bool,
|
|
505
|
+
#| ?is_override: bool,
|
|
506
|
+
#| ?is_overridable: bool,
|
|
507
|
+
#| ?is_final: bool,
|
|
508
|
+
#| ?type_params: Array[String],
|
|
509
|
+
#| ?checked: Symbol?) ?{ (Sig node) -> void } -> void
|
|
460
510
|
def add_sig(
|
|
461
511
|
params: [],
|
|
462
512
|
return_type: "void",
|
|
@@ -832,7 +882,25 @@ module RBI
|
|
|
832
882
|
attr_accessor :return_type
|
|
833
883
|
|
|
834
884
|
#: bool
|
|
835
|
-
attr_accessor :is_abstract
|
|
885
|
+
attr_accessor :is_abstract
|
|
886
|
+
|
|
887
|
+
#: bool
|
|
888
|
+
attr_accessor :is_override
|
|
889
|
+
|
|
890
|
+
#: bool
|
|
891
|
+
attr_accessor :is_overridable
|
|
892
|
+
|
|
893
|
+
#: bool
|
|
894
|
+
attr_accessor :is_final
|
|
895
|
+
|
|
896
|
+
#: bool
|
|
897
|
+
attr_accessor :allow_incompatible_override
|
|
898
|
+
|
|
899
|
+
#: bool
|
|
900
|
+
attr_accessor :allow_incompatible_override_visibility
|
|
901
|
+
|
|
902
|
+
#: bool
|
|
903
|
+
attr_accessor :without_runtime
|
|
836
904
|
|
|
837
905
|
#: Array[String]
|
|
838
906
|
attr_reader :type_params
|
|
@@ -840,7 +908,21 @@ module RBI
|
|
|
840
908
|
#: Symbol?
|
|
841
909
|
attr_accessor :checked
|
|
842
910
|
|
|
843
|
-
#: (
|
|
911
|
+
#: (
|
|
912
|
+
#| ?params: Array[SigParam],
|
|
913
|
+
#| ?return_type: (Type | String),
|
|
914
|
+
#| ?is_abstract: bool,
|
|
915
|
+
#| ?is_override: bool,
|
|
916
|
+
#| ?is_overridable: bool,
|
|
917
|
+
#| ?is_final: bool,
|
|
918
|
+
#| ?allow_incompatible_override: bool,
|
|
919
|
+
#| ?allow_incompatible_override_visibility: bool,
|
|
920
|
+
#| ?without_runtime: bool,
|
|
921
|
+
#| ?type_params: Array[String],
|
|
922
|
+
#| ?checked: Symbol?,
|
|
923
|
+
#| ?loc: Loc?,
|
|
924
|
+
#| ?comments: Array[Comment]
|
|
925
|
+
#| ) ?{ (Sig node) -> void } -> void
|
|
844
926
|
def initialize(
|
|
845
927
|
params: [],
|
|
846
928
|
return_type: "void",
|
|
@@ -849,6 +931,7 @@ module RBI
|
|
|
849
931
|
is_overridable: false,
|
|
850
932
|
is_final: false,
|
|
851
933
|
allow_incompatible_override: false,
|
|
934
|
+
allow_incompatible_override_visibility: false,
|
|
852
935
|
without_runtime: false,
|
|
853
936
|
type_params: [],
|
|
854
937
|
checked: nil,
|
|
@@ -864,6 +947,7 @@ module RBI
|
|
|
864
947
|
@is_overridable = is_overridable
|
|
865
948
|
@is_final = is_final
|
|
866
949
|
@allow_incompatible_override = allow_incompatible_override
|
|
950
|
+
@allow_incompatible_override_visibility = allow_incompatible_override_visibility
|
|
867
951
|
@without_runtime = without_runtime
|
|
868
952
|
@type_params = type_params
|
|
869
953
|
@checked = checked
|
|
@@ -942,11 +1026,17 @@ module RBI
|
|
|
942
1026
|
|
|
943
1027
|
# @abstract
|
|
944
1028
|
#: -> Array[String]
|
|
945
|
-
def fully_qualified_names
|
|
1029
|
+
def fully_qualified_names = raise NotImplementedError, "Abstract method called"
|
|
946
1030
|
end
|
|
947
1031
|
|
|
948
1032
|
class TStructConst < TStructField
|
|
949
|
-
#: (
|
|
1033
|
+
#: (
|
|
1034
|
+
#| String name,
|
|
1035
|
+
#| (Type | String) type,
|
|
1036
|
+
#| ?default: String?,
|
|
1037
|
+
#| ?loc: Loc?,
|
|
1038
|
+
#| ?comments: Array[Comment]
|
|
1039
|
+
#| ) ?{ (TStructConst node) -> void } -> void
|
|
950
1040
|
def initialize(name, type, default: nil, loc: nil, comments: [], &block)
|
|
951
1041
|
super(name, type, default: default, loc: loc, comments: comments)
|
|
952
1042
|
block&.call(self)
|
|
@@ -967,7 +1057,13 @@ module RBI
|
|
|
967
1057
|
end
|
|
968
1058
|
|
|
969
1059
|
class TStructProp < TStructField
|
|
970
|
-
#: (
|
|
1060
|
+
#: (
|
|
1061
|
+
#| String name,
|
|
1062
|
+
#| (Type | String) type,
|
|
1063
|
+
#| ?default: String?,
|
|
1064
|
+
#| ?loc: Loc?,
|
|
1065
|
+
#| ?comments: Array[Comment]
|
|
1066
|
+
#| ) ?{ (TStructProp node) -> void } -> void
|
|
971
1067
|
def initialize(name, type, default: nil, loc: nil, comments: [], &block)
|
|
972
1068
|
super(name, type, default: default, loc: loc, comments: comments)
|
|
973
1069
|
block&.call(self)
|
|
@@ -1087,7 +1183,12 @@ module RBI
|
|
|
1087
1183
|
end
|
|
1088
1184
|
|
|
1089
1185
|
class MixesInClassMethods < Mixin
|
|
1090
|
-
#: (
|
|
1186
|
+
#: (
|
|
1187
|
+
#| String name,
|
|
1188
|
+
#| *String names,
|
|
1189
|
+
#| ?loc: Loc?,
|
|
1190
|
+
#| ?comments: Array[Comment]
|
|
1191
|
+
#| ) ?{ (MixesInClassMethods node) -> void } -> void
|
|
1091
1192
|
def initialize(name, *names, loc: nil, comments: [], &block)
|
|
1092
1193
|
super(name, names, loc: loc, comments: comments)
|
|
1093
1194
|
block&.call(self)
|
data/lib/rbi/parser.rb
CHANGED
|
@@ -946,22 +946,8 @@ module RBI
|
|
|
946
946
|
end
|
|
947
947
|
when "override"
|
|
948
948
|
@current.is_override = true
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
keywords_hash = args
|
|
953
|
-
&.grep(Prism::KeywordHashNode)
|
|
954
|
-
&.first
|
|
955
|
-
|
|
956
|
-
allow_incompatible_override = keywords_hash
|
|
957
|
-
&.elements
|
|
958
|
-
&.any? do |assoc|
|
|
959
|
-
assoc.is_a?(Prism::AssocNode) &&
|
|
960
|
-
node_string(assoc.key) == "allow_incompatible:" &&
|
|
961
|
-
node_string(assoc.value) == "true"
|
|
962
|
-
end
|
|
963
|
-
|
|
964
|
-
@current.allow_incompatible_override = !!allow_incompatible_override
|
|
949
|
+
@current.allow_incompatible_override = allow_incompatible_override?(node, "true")
|
|
950
|
+
@current.allow_incompatible_override_visibility = allow_incompatible_override?(node, ":visibility")
|
|
965
951
|
when "overridable"
|
|
966
952
|
@current.is_overridable = true
|
|
967
953
|
when "params"
|
|
@@ -995,6 +981,23 @@ module RBI
|
|
|
995
981
|
node_string!(node.value),
|
|
996
982
|
)
|
|
997
983
|
end
|
|
984
|
+
|
|
985
|
+
#: (Prism::CallNode node, String value) -> bool
|
|
986
|
+
def allow_incompatible_override?(node, value)
|
|
987
|
+
args = node.arguments&.arguments
|
|
988
|
+
|
|
989
|
+
keywords_hash = args
|
|
990
|
+
&.grep(Prism::KeywordHashNode)
|
|
991
|
+
&.first
|
|
992
|
+
|
|
993
|
+
!!keywords_hash
|
|
994
|
+
&.elements
|
|
995
|
+
&.any? do |assoc|
|
|
996
|
+
assoc.is_a?(Prism::AssocNode) &&
|
|
997
|
+
node_string(assoc.key) == "allow_incompatible:" &&
|
|
998
|
+
node_string(assoc.value) == value
|
|
999
|
+
end
|
|
1000
|
+
end
|
|
998
1001
|
end
|
|
999
1002
|
|
|
1000
1003
|
class HeredocLocationVisitor < Prism::Visitor
|
data/lib/rbi/printer.rb
CHANGED
|
@@ -800,6 +800,8 @@ module RBI
|
|
|
800
800
|
if node.is_override
|
|
801
801
|
modifiers << if node.allow_incompatible_override
|
|
802
802
|
"override(allow_incompatible: true)"
|
|
803
|
+
elsif node.allow_incompatible_override_visibility
|
|
804
|
+
"override(allow_incompatible: :visibility)"
|
|
803
805
|
else
|
|
804
806
|
"override"
|
|
805
807
|
end
|
|
@@ -34,7 +34,7 @@ module RBI
|
|
|
34
34
|
case type
|
|
35
35
|
when ::RBS::Types::Alias
|
|
36
36
|
# TODO: unsupported yet
|
|
37
|
-
|
|
37
|
+
translate_type_alias(type)
|
|
38
38
|
when ::RBS::Types::Bases::Any
|
|
39
39
|
Type.untyped
|
|
40
40
|
when ::RBS::Types::Bases::Bool
|
|
@@ -85,12 +85,24 @@ module RBI
|
|
|
85
85
|
when ::RBS::Types::Variable
|
|
86
86
|
Type.type_parameter(type.name)
|
|
87
87
|
else
|
|
88
|
-
|
|
88
|
+
type #: absurd
|
|
89
89
|
end
|
|
90
90
|
end
|
|
91
91
|
|
|
92
92
|
private
|
|
93
93
|
|
|
94
|
+
#: (::RBS::Types::Alias) -> Type
|
|
95
|
+
def translate_type_alias(type)
|
|
96
|
+
name = ::RBS::TypeName.new(
|
|
97
|
+
namespace: type.name.namespace,
|
|
98
|
+
name: type.name.name.to_s.gsub(/(?:^|_)([a-z\d]*)/i) do |match|
|
|
99
|
+
match = match.delete_prefix("_")
|
|
100
|
+
!match.empty? ? match[0].upcase.concat(match[1..-1]) : +""
|
|
101
|
+
end,
|
|
102
|
+
)
|
|
103
|
+
Type.simple(name.to_s)
|
|
104
|
+
end
|
|
105
|
+
|
|
94
106
|
#: (::RBS::Types::ClassInstance) -> Type
|
|
95
107
|
def translate_class_instance(type)
|
|
96
108
|
return Type.simple(type.name.to_s) if type.args.empty?
|
data/lib/rbi/rbs_printer.rb
CHANGED
|
@@ -337,6 +337,8 @@ module RBI
|
|
|
337
337
|
if node.sigs.any?(&:is_override)
|
|
338
338
|
if node.sigs.any?(&:allow_incompatible_override)
|
|
339
339
|
printl("# @override(allow_incompatible: true)")
|
|
340
|
+
elsif node.sigs.any?(&:allow_incompatible_override_visibility)
|
|
341
|
+
printl("# @override(allow_incompatible: :visibility)")
|
|
340
342
|
else
|
|
341
343
|
printl("# @override")
|
|
342
344
|
end
|
|
@@ -56,7 +56,7 @@ module RBI
|
|
|
56
56
|
class Attr
|
|
57
57
|
# @abstract
|
|
58
58
|
#: -> Array[Method]
|
|
59
|
-
def convert_to_methods
|
|
59
|
+
def convert_to_methods = raise NotImplementedError, "Abstract method called"
|
|
60
60
|
|
|
61
61
|
private
|
|
62
62
|
|
|
@@ -88,7 +88,14 @@ module RBI
|
|
|
88
88
|
)
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
-
#: (
|
|
91
|
+
#: (
|
|
92
|
+
#| String name,
|
|
93
|
+
#| Sig? sig,
|
|
94
|
+
#| (Type | String)? attribute_type,
|
|
95
|
+
#| Visibility visibility,
|
|
96
|
+
#| Loc? loc,
|
|
97
|
+
#| Array[Comment] comments
|
|
98
|
+
#| ) -> Method
|
|
92
99
|
def create_setter_method(name, sig, attribute_type, visibility, loc, comments) # rubocop:disable Metrics/ParameterLists
|
|
93
100
|
sig = if sig # Modify the original sig to correct the name, and remove the return type
|
|
94
101
|
params = attribute_type ? [SigParam.new(name, attribute_type)] : []
|
|
@@ -330,7 +330,11 @@ module RBI
|
|
|
330
330
|
#: Array[Rewriters::Merge::Conflict]
|
|
331
331
|
attr_reader :conflicts
|
|
332
332
|
|
|
333
|
-
#: (
|
|
333
|
+
#: (
|
|
334
|
+
#| ?loc: Loc?,
|
|
335
|
+
#| ?comments: Array[Comment],
|
|
336
|
+
#| ?conflicts: Array[Rewriters::Merge::Conflict]
|
|
337
|
+
#| ) ?{ (Tree node) -> void } -> void
|
|
334
338
|
def initialize(loc: nil, comments: [], conflicts: [], &block)
|
|
335
339
|
super(loc: loc, comments: comments)
|
|
336
340
|
@conflicts = conflicts
|
data/lib/rbi/type.rb
CHANGED
|
@@ -583,6 +583,46 @@ module RBI
|
|
|
583
583
|
end
|
|
584
584
|
end
|
|
585
585
|
|
|
586
|
+
# A type alias that references another type by name like `MyTypeAlias`.
|
|
587
|
+
class TypeAlias < Type
|
|
588
|
+
#: String
|
|
589
|
+
attr_reader :name
|
|
590
|
+
|
|
591
|
+
#: Type
|
|
592
|
+
attr_reader :aliased_type
|
|
593
|
+
|
|
594
|
+
#: (String name, Type aliased_type) -> void
|
|
595
|
+
def initialize(name, aliased_type)
|
|
596
|
+
super()
|
|
597
|
+
@name = name
|
|
598
|
+
@aliased_type = aliased_type
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
# @override
|
|
602
|
+
#: (BasicObject other) -> bool
|
|
603
|
+
def ==(other)
|
|
604
|
+
TypeAlias === other && @name == other.name && @aliased_type == other.aliased_type
|
|
605
|
+
end
|
|
606
|
+
|
|
607
|
+
# @override
|
|
608
|
+
#: -> String
|
|
609
|
+
def to_rbi
|
|
610
|
+
"#{name} = T.type_alias { #{aliased_type.to_rbi} }"
|
|
611
|
+
end
|
|
612
|
+
|
|
613
|
+
# @override
|
|
614
|
+
#: -> Type
|
|
615
|
+
def normalize
|
|
616
|
+
TypeAlias.new(name, aliased_type.normalize)
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
# @override
|
|
620
|
+
#: -> Type
|
|
621
|
+
def simplify
|
|
622
|
+
TypeAlias.new(name, aliased_type.simplify)
|
|
623
|
+
end
|
|
624
|
+
end
|
|
625
|
+
|
|
586
626
|
# Tuples and shapes
|
|
587
627
|
|
|
588
628
|
# A tuple type like `[String, Integer]`.
|
|
@@ -871,6 +911,12 @@ module RBI
|
|
|
871
911
|
TypeParameter.new(name)
|
|
872
912
|
end
|
|
873
913
|
|
|
914
|
+
# Builds a type that represents a type alias like `MyTypeAlias`.
|
|
915
|
+
#: (String name, Type aliased_type) -> TypeAlias
|
|
916
|
+
def type_alias(name, aliased_type)
|
|
917
|
+
TypeAlias.new(name, aliased_type)
|
|
918
|
+
end
|
|
919
|
+
|
|
874
920
|
# Tuples and shapes
|
|
875
921
|
|
|
876
922
|
# Builds a type that represents a tuple type like `[String, Integer]`.
|
|
@@ -957,7 +1003,7 @@ module RBI
|
|
|
957
1003
|
#
|
|
958
1004
|
# @abstract
|
|
959
1005
|
#: -> Type
|
|
960
|
-
def normalize
|
|
1006
|
+
def normalize = raise NotImplementedError, "Abstract method called"
|
|
961
1007
|
|
|
962
1008
|
# Returns a simplified version of the type.
|
|
963
1009
|
#
|
|
@@ -969,11 +1015,11 @@ module RBI
|
|
|
969
1015
|
#
|
|
970
1016
|
# @abstract
|
|
971
1017
|
#: -> Type
|
|
972
|
-
def simplify
|
|
1018
|
+
def simplify = raise NotImplementedError, "Abstract method called"
|
|
973
1019
|
|
|
974
1020
|
# @abstract
|
|
975
1021
|
#: (BasicObject) -> bool
|
|
976
|
-
def ==(other)
|
|
1022
|
+
def ==(other) = raise NotImplementedError, "Abstract method called"
|
|
977
1023
|
|
|
978
1024
|
#: (BasicObject other) -> bool
|
|
979
1025
|
def eql?(other)
|
|
@@ -988,7 +1034,7 @@ module RBI
|
|
|
988
1034
|
|
|
989
1035
|
# @abstract
|
|
990
1036
|
#: -> String
|
|
991
|
-
def to_rbi
|
|
1037
|
+
def to_rbi = raise NotImplementedError, "Abstract method called"
|
|
992
1038
|
|
|
993
1039
|
# @override
|
|
994
1040
|
#: -> String
|
data/lib/rbi/type_parser.rb
CHANGED
|
@@ -27,6 +27,8 @@ module RBI
|
|
|
27
27
|
case node
|
|
28
28
|
when Prism::ConstantReadNode, Prism::ConstantPathNode
|
|
29
29
|
parse_constant(node)
|
|
30
|
+
when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode
|
|
31
|
+
parse_constant_assignment(node)
|
|
30
32
|
when Prism::CallNode
|
|
31
33
|
parse_call(node)
|
|
32
34
|
when Prism::ArrayNode
|
|
@@ -67,6 +69,29 @@ module RBI
|
|
|
67
69
|
end
|
|
68
70
|
end
|
|
69
71
|
|
|
72
|
+
#: (Prism::ConstantWriteNode | Prism::ConstantPathWriteNode node) -> Type
|
|
73
|
+
def parse_constant_assignment(node)
|
|
74
|
+
if t_type_alias?(node.value)
|
|
75
|
+
value = node.value #: as Prism::CallNode
|
|
76
|
+
name = if node.is_a?(Prism::ConstantPathWriteNode)
|
|
77
|
+
node.target.full_name
|
|
78
|
+
else
|
|
79
|
+
node.full_name
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
body = value.block
|
|
83
|
+
|
|
84
|
+
if body.is_a?(Prism::BlockNode) && (stmts = body.body).is_a?(Prism::StatementsNode)
|
|
85
|
+
body_node = stmts.body.first #: as !nil
|
|
86
|
+
Type::TypeAlias.new(name, parse_node(body_node))
|
|
87
|
+
else
|
|
88
|
+
Type::Simple.new(node.slice)
|
|
89
|
+
end
|
|
90
|
+
else
|
|
91
|
+
Type::Simple.new(node.slice)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
70
95
|
#: (Prism::CallNode node) -> Type
|
|
71
96
|
def parse_call(node)
|
|
72
97
|
recv = node.receiver
|
|
@@ -310,6 +335,13 @@ module RBI
|
|
|
310
335
|
false
|
|
311
336
|
end
|
|
312
337
|
|
|
338
|
+
#: (Prism::Node? node) -> bool
|
|
339
|
+
def t_type_alias?(node)
|
|
340
|
+
return false unless node.is_a?(Prism::CallNode)
|
|
341
|
+
|
|
342
|
+
t?(node.receiver) && node.name == :type_alias
|
|
343
|
+
end
|
|
344
|
+
|
|
313
345
|
#: (Prism::Node? node) -> bool
|
|
314
346
|
def t_boolean?(node)
|
|
315
347
|
return false unless node.is_a?(Prism::ConstantPathNode)
|
data/lib/rbi/type_visitor.rb
CHANGED
|
@@ -45,6 +45,8 @@ module RBI
|
|
|
45
45
|
visit_type_parameter(node)
|
|
46
46
|
when Type::Class
|
|
47
47
|
visit_class(node)
|
|
48
|
+
when Type::TypeAlias
|
|
49
|
+
visit_type_alias(node)
|
|
48
50
|
else
|
|
49
51
|
raise Error, "Unhandled node: #{node.class}"
|
|
50
52
|
end
|
|
@@ -105,6 +107,9 @@ module RBI
|
|
|
105
107
|
|
|
106
108
|
#: (Type::Untyped type) -> void
|
|
107
109
|
def visit_untyped(type); end
|
|
110
|
+
|
|
111
|
+
#: (Type::TypeAlias type) -> void
|
|
112
|
+
def visit_type_alias(type); end
|
|
108
113
|
end
|
|
109
114
|
end
|
|
110
115
|
end
|
data/lib/rbi/version.rb
CHANGED