forked from soutaro/rbs-inline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclarations.rb
More file actions
599 lines (506 loc) · 18.5 KB
/
declarations.rb
File metadata and controls
599 lines (506 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# rbs_inline: enabled
module RBS
module Inline
module AST
module Declarations
module ConstantUtil
# @rbs node: Prism::Node
# @rbs return: TypeName?
def type_name(node)
case node
when Prism::ConstantReadNode, Prism::ConstantPathNode
TypeName.parse(node.full_name)
end
end
# @rbs (Prism::Node) -> Prism::Node?
def value_node(node)
case node
when Prism::ConstantWriteNode
value_node(node.value)
when Prism::LocalVariableWriteNode
value_node(node.value)
else
node
end
end
end
# @rbs!
# type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl | BlockDecl | DataAssignDecl | StructAssignDecl
#
# interface _WithComments
# def comments: () -> AnnotationParser::ParsingResult?
# end
# @rbs module-self _WithComments
module Generics
# @rbs return: Array[RBS::AST::TypeParam]
def type_params
if comments = comments()
comments.each_annotation.filter_map do |annotation|
if annotation.is_a?(Annotations::Generic)
annotation.type_param
end
end
else
[]
end
end
end
class Base
end
# @rbs generic NODE < Prism::Node
class ModuleOrClass < Base
# The node that represents the declaration
attr_reader :node #: NODE
# Leading comment
attr_reader :comments #: AnnotationParser::ParsingResult?
# Members included in the declaration
attr_reader :members #: Array[Members::t | t]
# @rbs node: NODE
# @rbs comments: AnnotationParser::ParsingResult?
def initialize(node, comments) #: void
@node = node
@comments = comments
@members = []
end
# Type parameters for the declaration
def type_params #: Array[RBS::AST::TypeParam]
if comments = comments()
comments.each_annotation.filter_map do |annotation|
if annotation.is_a?(Annotations::Generic)
annotation.type_param
end
end
else
[]
end
end
def start_line #: Integer
node.location.start_line
end
end
class ClassDecl < ModuleOrClass #[Prism::ClassNode]
include ConstantUtil
# Type application for super class
attr_reader :super_app #: Annotations::Application?
# @rbs node: Prism::ClassNode
# @rbs comments: AnnotationParser::ParsingResult?
# @rbs super_app: Annotations::Application?
# @rbs return: void
def initialize(node, comments, super_app)
super(node, comments)
@super_app = super_app
end
# @rbs %a{pure}
def class_name #: TypeName?
type_name(node.constant_path)
end
# @rbs %a{pure}
def super_class #: RBS::AST::Declarations::Class::Super?
if comments
if inherits = comments.each_annotation.find {|a| a.is_a?(Annotations::Inherits) } #: Annotations::Inherits?
super_name = inherits.super_name
super_args = inherits.args
if super_name && super_args
return RBS::AST::Declarations::Class::Super.new(
name: super_name,
args: super_args,
location: nil
)
end
end
end
if node.superclass
super_name = nil #: TypeName?
super_args = nil #: Array[Types::t]?
if super_app
super_args = super_app.types
end
super_name = type_name(node.superclass)
if super_name
return RBS::AST::Declarations::Class::Super.new(
name: super_name,
args: super_args || [],
location: nil
)
end
end
end
end
class ModuleDecl < ModuleOrClass #[Prism::ModuleNode]
include ConstantUtil
# @rbs %a{pure}
def module_name #: TypeName?
type_name(node.constant_path)
end
# @rbs %a{pure}
def module_selfs #: Array[Annotations::ModuleSelf]
if comments
comments.each_annotation.filter_map do |ann|
if ann.is_a?(AST::Annotations::ModuleSelf)
ann
end
end
else
[]
end
end
end
class ConstantDecl < Base
include ConstantUtil
attr_reader :node #: Prism::ConstantWriteNode
attr_reader :comments #: AnnotationParser::ParsingResult?
attr_reader :assertion #: Annotations::TypeAssertion?
# @rbs node: Prism::ConstantWriteNode
# @rbs comments: AnnotationParser::ParsingResult?
# @rbs assertion: Annotations::TypeAssertion?
def initialize(node, comments, assertion) #: void
@node = node
@comments = comments
@assertion = assertion
end
# @rbs %a{pure}
# @rbs return: Types::t
def type(default_type)
if assertion
case assertion.type
when MethodType, nil
# skip
else
return assertion.type
end
end
if literal = literal_type
return literal
end
default_type
end
# @rbs %a{pure}
# @rbs return: Types::t?
def literal_type
infer_type_from_node(node.value)
end
# @rbs %a{pure}
# @rbs return: TypeName?
def constant_name
TypeName.new(name: node.name, namespace: Namespace.empty)
end
def start_line #: Integer
node.location.start_line
end
private
# @rbs node: Prism::Node
# @rbs return: Types::t?
def infer_type_from_node(node)
case node
when Prism::StringNode, Prism::InterpolatedStringNode
BuiltinNames::String.instance_type
when Prism::SymbolNode, Prism::InterpolatedSymbolNode
BuiltinNames::Symbol.instance_type
when Prism::RegularExpressionNode, Prism::InterpolatedRegularExpressionNode
BuiltinNames::Regexp.instance_type
when Prism::IntegerNode
BuiltinNames::Integer.instance_type
when Prism::FloatNode
BuiltinNames::Float.instance_type
when Prism::ArrayNode
infer_array_element_type(node)
when Prism::HashNode
infer_hash_element_type(node)
when Prism::TrueNode, Prism::FalseNode
Types::Bases::Bool.new(location: nil)
end
end
# @rbs node: Prism::ArrayNode
# @rbs return: Types::t
def infer_array_element_type(node)
return BuiltinNames::Array.instance_type if node.elements.empty?
element_types = [] #: Array[Types::t]
node.elements.each do |elem|
type = infer_type_from_node(elem)
return BuiltinNames::Array.instance_type unless type
element_types << type
end
element_types.uniq!(&:to_s)
# Union types are not currently supported.
case element_types.size
when 1
BuiltinNames::Array.instance_type(element_types.first || raise)
else
BuiltinNames::Array.instance_type(
Types::Bases::Any.new(location: nil)
)
end
end
# @rbs node: Prism::HashNode
# @rbs return: Types::t
def infer_hash_element_type(node)
return BuiltinNames::Hash.instance_type if node.elements.empty?
key_types = [] #: Array[Types::t]
value_types = [] #: Array[Types::t]
node.elements.each do |elem|
case elem
when Prism::AssocNode
key_type = infer_type_from_node(elem.key)
value_type = infer_type_from_node(elem.value)
return BuiltinNames::Hash.instance_type unless key_type && value_type
key_types << key_type
value_types << value_type
else
return BuiltinNames::Hash.instance_type
end
end
key_types.uniq!(&:to_s)
value_types.uniq!(&:to_s)
# Union types are not currently supported.
key_type = case key_types.size
when 1
key_types.first || raise
else
Types::Bases::Any.new(location: nil)
end
value_type = case value_types.size
when 1
value_types.first || raise
else
Types::Bases::Any.new(location: nil)
end
BuiltinNames::Hash.instance_type(key_type, value_type)
end
end
class SingletonClassDecl < ModuleOrClass #[Prism::SingletonClassNode]
# @rbs (AST::Members::RubyDef) -> (:private | nil)
def visibility(def_member)
current_visibility = nil
members.each do |member|
case member
when AST::Members::RubyPublic
current_visibility = nil
when AST::Members::RubyPrivate
current_visibility = :private
end
break if member == def_member
end
current_visibility
end
end
class BlockDecl < Base
attr_reader :node #: Prism::BlockNode
attr_reader :comments #: AnnotationParser::ParsingResult?
# Members included in the declaration
attr_reader :members #: Array[Members::t | t]
# @rbs (Prism::BlockNode, AnnotationParser::ParsingResult?) -> void
def initialize(node, comments)
@node = node
@members = []
@comments = comments
end
def start_line #: Integer
node.location.start_line
end
def module_class_annotation #: Annotations::ModuleDecl | Annotations::ClassDecl | nil
if comments
comments.each_annotation.each do |annotation|
if annotation.is_a?(Annotations::ModuleDecl)
return annotation
end
if annotation.is_a?(Annotations::ClassDecl)
return annotation
end
end
nil
end
end
end
# @rbs module-self _WithTypeDecls
module DataStructUtil
# @rbs!
# interface _WithTypeDecls
# def type_decls: () -> Hash[Integer, Annotations::TypeAssertion]
#
# def each_attribute_argument: () { (Prism::Node) -> void } -> void
#
# def comments: %a{pure} () -> AnnotationParser::ParsingResult?
# end
# @rbs %a{pure}
# @rbs () { ([Symbol, Annotations::TypeAssertion?]) -> void } -> void
# | () -> Enumerator[[Symbol, Annotations::TypeAssertion?], void]
def each_attribute(&block)
if block
each_attribute_argument do |arg|
if arg.is_a?(Prism::SymbolNode)
if name = arg.value
type = type_decls.fetch(arg.location.start_line, nil)
yield [name.to_sym, type]
end
end
end
else
enum_for :each_attribute
end
end
def class_annotations #: Array[RBS::AST::Annotation]
annotations = [] #: Array[RBS::AST::Annotation]
comments&.each_annotation do |annotation|
if annotation.is_a?(Annotations::RBSAnnotation)
annotations.concat annotation.annotations
end
end
annotations
end
end
class DataAssignDecl < Base
extend ConstantUtil
include DataStructUtil
attr_reader :node #: Prism::ConstantWriteNode
attr_reader :comments #: AnnotationParser::ParsingResult?
attr_reader :type_decls #: Hash[Integer, Annotations::TypeAssertion]
attr_reader :data_define_node #: Prism::CallNode
# @rbs (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
def initialize(node, data_define_node, comments, type_decls)
@node = node
@comments = comments
@type_decls = type_decls
@data_define_node = data_define_node
end
def start_line #: Integer
node.location.start_line
end
# @rbs %a{pure}
# @rbs () -> TypeName?
def constant_name
TypeName.new(name: node.name, namespace: Namespace.empty)
end
# @rbs (Prism::ConstantWriteNode) -> Prism::CallNode?
def self.data_define?(node)
value = value_node(node)
if value.is_a?(Prism::CallNode)
if value.receiver.is_a?(Prism::ConstantReadNode)
if value.receiver.full_name.delete_prefix("::") == "Data"
if value.name == :define
return value
end
end
end
end
end
# @rbs () { (Prism::Node) -> void } -> void
def each_attribute_argument(&block)
if args = data_define_node.arguments
args.arguments.each(&block)
end
end
end
class StructAssignDecl < Base
extend ConstantUtil
include DataStructUtil
attr_reader :node #: Prism::ConstantWriteNode
attr_reader :comments #: AnnotationParser::ParsingResult?
attr_reader :type_decls #: Hash[Integer, Annotations::TypeAssertion]
attr_reader :struct_new_node #: Prism::CallNode
# @rbs (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
def initialize(node, struct_new_node, comments, type_decls)
@node = node
@comments = comments
@type_decls = type_decls
@struct_new_node = struct_new_node
end
def start_line #: Integer
node.location.start_line
end
# @rbs %a{pure}
# @rbs () -> TypeName?
def constant_name
TypeName.new(name: node.name, namespace: Namespace.empty)
end
# @rbs () { (Prism::Node) -> void } -> void
def each_attribute_argument(&block)
if args = struct_new_node.arguments
args.arguments.each do |arg|
next if arg.is_a?(Prism::KeywordHashNode)
next if arg.is_a?(Prism::StringNode)
yield arg
end
end
end
# @rbs (Prism::ConstantWriteNode) -> Prism::CallNode?
def self.struct_new?(node)
value = value_node(node)
if value.is_a?(Prism::CallNode)
if value.receiver.is_a?(Prism::ConstantReadNode)
if value.receiver.full_name.delete_prefix("::") == "Struct"
if value.name == :new
return value
end
end
end
end
end
# @rbs %a{pure}
def keyword_init? #: bool
if args = struct_new_node.arguments
args.arguments.each do |arg|
if arg.is_a?(Prism::KeywordHashNode)
arg.elements.each do |assoc|
if assoc.is_a?(Prism::AssocNode)
if (key = assoc.key).is_a?(Prism::SymbolNode)
if key.value == "keyword_init"
value = assoc.value
if value.is_a?(Prism::FalseNode)
return false
end
end
end
end
end
end
end
end
true
end
# @rbs %a{pure}
def positional_init? #: bool
if args = struct_new_node.arguments
args.arguments.each do |arg|
if arg.is_a?(Prism::KeywordHashNode)
arg.elements.each do |assoc|
if assoc.is_a?(Prism::AssocNode)
if (key = assoc.key).is_a?(Prism::SymbolNode)
if key.value == "keyword_init"
value = assoc.value
if value.is_a?(Prism::TrueNode)
return false
end
end
end
end
end
end
end
end
true
end
# Returns `true` is annotation is given to make all attributes *readonly*
#
# Add `# @rbs %a{rbs-inline:readonly-attributes=true}` to the class to make all attributes `attr_reader`, instead of `attr_accessor`.
#
# @rbs %a{pure}
def readonly_attributes? #: bool
class_annotations.any? do |annotation|
annotation.string == "rbs-inline:readonly-attributes=true"
end
end
# Returns `true` if annotation is given to make all `.new` arguments required
#
# Add `# @rbs %a{rbs-inline:new-args=required}` to the class to make all of the parameters required.
#
# @rbs %a{pure}
def required_new_args? #: bool
class_annotations.any? do |annotation|
annotation.string == "rbs-inline:new-args=required"
end
end
end
end
end
end
end