rbs-inline 0.1.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,243 @@
1
+ # rbs_inline: enabled
2
+
3
+ module RBS
4
+ module Inline
5
+ module AST
6
+ module Declarations
7
+ module ConstantUtil
8
+ # @rbs node: Prism::Node
9
+ # @rbs returns TypeName?
10
+ def type_name(node)
11
+ case node
12
+ when Prism::ConstantReadNode
13
+ TypeName(node.name.to_s)
14
+ when Prism::ConstantPathNode
15
+ if node.parent
16
+ if parent = type_name(node.parent)
17
+ if child = type_name(node.child)
18
+ return parent + child
19
+ end
20
+ end
21
+ else
22
+ type_name(node.child)&.absolute!
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ # @rbs! type t = ClassDecl | ModuleDecl | ConstantDecl
29
+
30
+ # @rbs!
31
+ # interface _WithComments
32
+ # def comments: () -> AnnotationParser::ParsingResult?
33
+ # end
34
+
35
+ # @rbs module-self _WithComments
36
+ module Generics
37
+ # @rbs returns Array[RBS::AST::TypeParam]
38
+ def type_params
39
+ if comments = comments()
40
+ comments.annotations.filter_map do |annotation|
41
+ if annotation.is_a?(Annotations::Generic)
42
+ annotation.type_param
43
+ end
44
+ end
45
+ else
46
+ []
47
+ end
48
+ end
49
+ end
50
+
51
+ class Base
52
+ end
53
+
54
+ # @rbs generic NODE < Prism::Node
55
+ class ModuleOrClass < Base
56
+ # The node that represents the declaration
57
+ attr_reader :node #:: NODE
58
+
59
+ # Leading comment
60
+ attr_reader :comments #:: AnnotationParser::ParsingResult?
61
+
62
+ # Members included in the declaration
63
+ attr_reader :members #:: Array[Members::t | t]
64
+
65
+ # @rbs node: NODE
66
+ # @rbs comments: AnnotationParser::ParsingResult?
67
+ def initialize(node, comments) #:: void
68
+ @node = node
69
+ @comments = comments
70
+ @members = []
71
+ end
72
+
73
+ # Type parameters for the declaration
74
+ def type_params #:: Array[RBS::AST::TypeParam]
75
+ if comments = comments()
76
+ comments.annotations.filter_map do |annotation|
77
+ if annotation.is_a?(Annotations::Generic)
78
+ annotation.type_param
79
+ end
80
+ end
81
+ else
82
+ []
83
+ end
84
+ end
85
+
86
+ def start_line #:: Integer
87
+ node.location.start_line
88
+ end
89
+ end
90
+
91
+ class ClassDecl < ModuleOrClass #[Prism::ClassNode]
92
+ include ConstantUtil
93
+
94
+ # Type application for super class
95
+ attr_reader :super_app #:: Annotations::Application?
96
+
97
+ # @rbs node: Prism::ClassNode
98
+ # @rbs comments: AnnotationParser::ParsingResult?
99
+ # @rbs super_app: Annotations::Application?
100
+ # @rbs returns void
101
+ def initialize(node, comments, super_app)
102
+ super(node, comments)
103
+
104
+ @super_app = super_app
105
+ end
106
+
107
+ # @rbs %a{pure}
108
+ def class_name #:: TypeName?
109
+ type_name(node.constant_path)
110
+ end
111
+
112
+ # @rbs %a{pure}
113
+ def super_class #:: RBS::AST::Declarations::Class::Super?
114
+ if comments
115
+ if inherits = comments.annotations.find {|a| a.is_a?(Annotations::Inherits) } #: Annotations::Inherits?
116
+ super_name = inherits.super_name
117
+ super_args = inherits.args
118
+
119
+ if super_name && super_args
120
+ return RBS::AST::Declarations::Class::Super.new(
121
+ name: super_name,
122
+ args: super_args,
123
+ location: nil
124
+ )
125
+ end
126
+ end
127
+ end
128
+
129
+ if node.superclass
130
+ super_name = nil #: TypeName?
131
+ super_args = nil #: Array[Types::t]?
132
+
133
+ if super_app
134
+ super_args = super_app.types
135
+ end
136
+
137
+ super_name = type_name(node.superclass)
138
+
139
+ if super_name
140
+ return RBS::AST::Declarations::Class::Super.new(
141
+ name: super_name,
142
+ args: super_args || [],
143
+ location: nil
144
+ )
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ class ModuleDecl < ModuleOrClass #[Prism::ModuleNode]
151
+ include ConstantUtil
152
+
153
+ # @rbs %a{pure}
154
+ def module_name #:: TypeName?
155
+ type_name(node.constant_path)
156
+ end
157
+
158
+ # @rbs %a{pure}
159
+ def module_selfs #:: Array[Annotations::ModuleSelf]
160
+ if comments
161
+ comments.annotations.filter_map do |ann|
162
+ if ann.is_a?(AST::Annotations::ModuleSelf)
163
+ ann
164
+ end
165
+ end
166
+ else
167
+ []
168
+ end
169
+ end
170
+ end
171
+
172
+ class ConstantDecl < Base
173
+ include ConstantUtil
174
+
175
+ attr_reader :node #:: Prism::ConstantWriteNode
176
+ attr_reader :comments #:: AnnotationParser::ParsingResult?
177
+ attr_reader :assertion #:: Annotations::Assertion?
178
+
179
+ # @rbs node: Prism::ConstantWriteNode
180
+ # @rbs comments: AnnotationParser::ParsingResult?
181
+ # @rbs assertion: Annotations::Assertion?
182
+ def initialize(node, comments, assertion)
183
+ @node = node
184
+ @comments = comments
185
+ @assertion = assertion
186
+ end
187
+
188
+ # @rbs %a{pure}
189
+ # @rbs returns Types::t
190
+ def type
191
+ if assertion
192
+ case assertion.type
193
+ when MethodType, nil
194
+ # skip
195
+ else
196
+ return assertion.type
197
+ end
198
+ end
199
+
200
+ if literal = literal_type
201
+ return literal
202
+ end
203
+
204
+ Types::Bases::Any.new(location: nil)
205
+ end
206
+
207
+ # @rbs %a{pure}
208
+ # @rbs return Types::t?
209
+ def literal_type
210
+ case node.value
211
+ when Prism::StringNode, Prism::InterpolatedStringNode
212
+ BuiltinNames::String.instance_type
213
+ when Prism::SymbolNode, Prism::InterpolatedSymbolNode
214
+ BuiltinNames::Symbol.instance_type
215
+ when Prism::RegularExpressionNode, Prism::InterpolatedRegularExpressionNode
216
+ BuiltinNames::Regexp.instance_type
217
+ when Prism::IntegerNode
218
+ BuiltinNames::Integer.instance_type
219
+ when Prism::FloatNode
220
+ BuiltinNames::Float.instance_type
221
+ when Prism::ArrayNode
222
+ BuiltinNames::Array.instance_type
223
+ when Prism::HashNode
224
+ BuiltinNames::Hash.instance_type
225
+ when Prism::TrueNode, Prism::FalseNode
226
+ Types::Bases::Bool.new(location: nil)
227
+ end
228
+ end
229
+
230
+ # @rbs %a{pure}
231
+ # @rbs returns TypeName?
232
+ def constant_name
233
+ TypeName.new(name: node.name, namespace: Namespace.empty)
234
+ end
235
+
236
+ def start_line #:: Integer
237
+ node.location.start_line
238
+ end
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end