rbi 0.2.4 → 0.3.0

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.
@@ -0,0 +1,120 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module RBS
6
+ class MethodTypeTranslator
7
+ class Error < RBI::Error; end
8
+
9
+ class << self
10
+ #: (Method, ::RBS::MethodType) -> Sig
11
+ def translate(method, type)
12
+ translator = new(method)
13
+ translator.visit(type)
14
+ translator.result
15
+ end
16
+ end
17
+
18
+ #: Sig
19
+ attr_reader :result
20
+
21
+ #: (Method) -> void
22
+ def initialize(method)
23
+ @method = method
24
+ @result = T.let(Sig.new, Sig)
25
+ end
26
+
27
+ #: (::RBS::MethodType) -> void
28
+ def visit(type)
29
+ type.type_params.each do |param|
30
+ result.type_params << param.name
31
+ end
32
+
33
+ visit_function_type(type.type)
34
+
35
+ block = type.block
36
+ visit_block_type(block) if block
37
+ end
38
+
39
+ private
40
+
41
+ #: (::RBS::Types::Block) -> void
42
+ def visit_block_type(type)
43
+ block_param = @method.params.grep(RBI::BlockParam).first
44
+ raise Error, "No block param found" unless block_param
45
+
46
+ block_name = block_param.name
47
+ block_type = T.cast(translate_type(type.type), RBI::Type::Proc)
48
+
49
+ bind = type.self_type
50
+ block_type.bind(translate_type(bind)) if bind
51
+ block_type = block_type.nilable unless type.required
52
+
53
+ @result.params << SigParam.new(block_name, block_type)
54
+ end
55
+
56
+ #: (::RBS::Types::Function) -> void
57
+ def visit_function_type(type)
58
+ index = 0
59
+
60
+ type.required_positionals.each do |param|
61
+ result.params << translate_function_param(param, index)
62
+ index += 1
63
+ end
64
+
65
+ type.optional_positionals.each do |param|
66
+ result.params << translate_function_param(param, index)
67
+ index += 1
68
+ end
69
+
70
+ rest_positional = type.rest_positionals
71
+ if rest_positional
72
+ result.params << translate_function_param(rest_positional, index)
73
+ index += 1
74
+ end
75
+
76
+ type.trailing_positionals.each do |param|
77
+ result.params << translate_function_param(param, index)
78
+ index += 1
79
+ end
80
+
81
+ type.required_keywords.each do |name, param|
82
+ result.params << SigParam.new(name.to_s, translate_type(param.type))
83
+ index += 1
84
+ end
85
+
86
+ type.optional_keywords.each do |name, param|
87
+ result.params << SigParam.new(name.to_s, translate_type(param.type))
88
+ index += 1
89
+ end
90
+
91
+ rest_keyword = type.rest_keywords
92
+ if rest_keyword
93
+ result.params << translate_function_param(rest_keyword, index)
94
+ end
95
+
96
+ result.return_type = translate_type(type.return_type)
97
+ end
98
+
99
+ #: (::RBS::Types::Function::Param, Integer) -> SigParam
100
+ def translate_function_param(param, index)
101
+ param_type = translate_type(param.type)
102
+ param_name = param.name&.to_s
103
+
104
+ unless param_name
105
+ method_param_name = @method.params[index]
106
+ raise Error, "No method param name found for parameter ##{index}" unless method_param_name
107
+
108
+ param_name = method_param_name.name
109
+ end
110
+
111
+ SigParam.new(param_name, param_type)
112
+ end
113
+
114
+ #: (untyped) -> Type
115
+ def translate_type(type)
116
+ TypeTranslator.translate(type)
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,181 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module RBS
6
+ class TypeTranslator
7
+ class << self
8
+ NodeType = T.type_alias do
9
+ T.any(
10
+ ::RBS::Types::Alias,
11
+ ::RBS::Types::Bases::Any,
12
+ ::RBS::Types::Bases::Bool,
13
+ ::RBS::Types::Bases::Bottom,
14
+ ::RBS::Types::Bases::Class,
15
+ ::RBS::Types::Bases::Instance,
16
+ ::RBS::Types::Bases::Nil,
17
+ ::RBS::Types::Bases::Self,
18
+ ::RBS::Types::Bases::Top,
19
+ ::RBS::Types::Bases::Void,
20
+ ::RBS::Types::ClassSingleton,
21
+ ::RBS::Types::ClassInstance,
22
+ ::RBS::Types::Function,
23
+ ::RBS::Types::Interface,
24
+ ::RBS::Types::Intersection,
25
+ ::RBS::Types::Literal,
26
+ ::RBS::Types::Optional,
27
+ ::RBS::Types::Proc,
28
+ ::RBS::Types::Record,
29
+ ::RBS::Types::Tuple,
30
+ ::RBS::Types::Union,
31
+ ::RBS::Types::UntypedFunction,
32
+ ::RBS::Types::Variable,
33
+ )
34
+ end
35
+
36
+ #: (NodeType) -> Type
37
+ def translate(type)
38
+ case type
39
+ when ::RBS::Types::Alias
40
+ # TODO: unsupported yet
41
+ Type.untyped
42
+ when ::RBS::Types::Bases::Any
43
+ Type.untyped
44
+ when ::RBS::Types::Bases::Bool
45
+ Type.boolean
46
+ when ::RBS::Types::Bases::Bottom
47
+ Type.noreturn
48
+ when ::RBS::Types::Bases::Class
49
+ # TODO: unsupported yet
50
+ Type.untyped
51
+ when ::RBS::Types::Bases::Instance
52
+ Type.attached_class
53
+ when ::RBS::Types::Bases::Nil
54
+ Type.simple("NilClass")
55
+ when ::RBS::Types::Bases::Self
56
+ Type.self_type
57
+ when ::RBS::Types::Bases::Top
58
+ Type.anything
59
+ when ::RBS::Types::Bases::Void
60
+ Type.void
61
+ when ::RBS::Types::ClassSingleton
62
+ Type.class_of(Type.simple(type.name.to_s))
63
+ when ::RBS::Types::ClassInstance
64
+ translate_class_instance(type)
65
+ when ::RBS::Types::Function
66
+ translate_function(type)
67
+ when ::RBS::Types::Interface
68
+ # TODO: unsupported yet
69
+ Type.untyped
70
+ when ::RBS::Types::Intersection
71
+ Type.all(*type.types.map { |t| translate(t) })
72
+ when ::RBS::Types::Literal
73
+ # TODO: unsupported yet
74
+ Type.untyped
75
+ when ::RBS::Types::Optional
76
+ Type.nilable(translate(type.type))
77
+ when ::RBS::Types::Proc
78
+ proc = T.cast(translate(type.type), Type::Proc)
79
+ proc.bind(translate(type.self_type)) if type.self_type
80
+ proc
81
+ when ::RBS::Types::Record
82
+ Type.shape(type.fields.map { |name, type| [name, translate(type)] }.to_h)
83
+ when ::RBS::Types::Tuple
84
+ Type.tuple(type.types.map { |t| translate(t) })
85
+ when ::RBS::Types::Union
86
+ Type.any(*type.types.map { |t| translate(t) })
87
+ when ::RBS::Types::UntypedFunction
88
+ Type.proc.params(arg0: Type.untyped).returns(Type.untyped)
89
+ when ::RBS::Types::Variable
90
+ Type.type_parameter(type.name)
91
+ else
92
+ T.absurd(type)
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ #: (::RBS::Types::ClassInstance) -> Type
99
+ def translate_class_instance(type)
100
+ return Type.simple(type.name.to_s) if type.args.empty?
101
+
102
+ type_name = translate_t_generic_type(type.name.to_s)
103
+ T.unsafe(Type).generic(type_name, *type.args.map { |arg| translate(arg) })
104
+ end
105
+
106
+ #: (::RBS::Types::Function) -> Type
107
+ def translate_function(type)
108
+ proc = Type.proc
109
+
110
+ index = 0
111
+
112
+ type.required_positionals.each do |param|
113
+ proc.proc_params[param.name || :"arg#{index}"] = translate(param.type)
114
+ index += 1
115
+ end
116
+
117
+ type.optional_positionals.each do |param|
118
+ proc.proc_params[param.name || :"arg#{index}"] = translate(param.type)
119
+ index += 1
120
+ end
121
+
122
+ rest_positional = type.rest_positionals
123
+ if rest_positional
124
+ proc.proc_params[rest_positional.name || :"arg#{index}"] = translate(rest_positional.type)
125
+ index += 1
126
+ end
127
+
128
+ type.trailing_positionals.each do |param|
129
+ proc.proc_params[param.name || :"arg#{index}"] = translate(param.type)
130
+ index += 1
131
+ end
132
+
133
+ type.required_keywords.each do |name, param|
134
+ proc.proc_params[name.to_sym] = translate(param.type)
135
+ index += 1
136
+ end
137
+
138
+ type.optional_keywords.each do |name, param|
139
+ proc.proc_params[name.to_sym] = translate(param.type)
140
+ index += 1
141
+ end
142
+
143
+ rest_keyword = type.rest_keywords
144
+ if rest_keyword
145
+ proc.proc_params[rest_keyword.name || :"arg_#{index}"] = translate(rest_keyword.type)
146
+ index += 1
147
+ end
148
+
149
+ proc.returns(translate(type.return_type))
150
+ proc
151
+ end
152
+
153
+ #: (String type_name) -> String
154
+ def translate_t_generic_type(type_name)
155
+ case type_name.delete_prefix("::")
156
+ when "Array"
157
+ "T::Array"
158
+ when "Class"
159
+ "T::Class"
160
+ when "Enumerable"
161
+ "T::Enumerable"
162
+ when "Enumerator"
163
+ "T::Enumerator"
164
+ when "Enumerator::Chain"
165
+ "T::Enumerator::Chain"
166
+ when "Enumerator::Lazy"
167
+ "T::Enumerator::Lazy"
168
+ when "Hash"
169
+ "T::Hash"
170
+ when "Set"
171
+ "T::Set"
172
+ when "Range"
173
+ "T::Range"
174
+ else
175
+ type_name
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end