rbi 0.0.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rbi/model.rb ADDED
@@ -0,0 +1,1251 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ class Node
6
+ extend T::Sig
7
+ extend T::Helpers
8
+
9
+ abstract!
10
+
11
+ sig { returns(T.nilable(Tree)) }
12
+ attr_accessor :parent_tree
13
+
14
+ sig { returns(T.nilable(Loc)) }
15
+ attr_accessor :loc
16
+
17
+ sig { params(loc: T.nilable(Loc)).void }
18
+ def initialize(loc: nil)
19
+ @parent_tree = nil
20
+ @loc = loc
21
+ end
22
+
23
+ sig { void }
24
+ def detach
25
+ tree = parent_tree
26
+ return unless tree
27
+ tree.nodes.delete(self)
28
+ self.parent_tree = nil
29
+ end
30
+
31
+ sig { params(node: Node).void }
32
+ def replace(node)
33
+ tree = parent_tree
34
+ raise unless tree
35
+ index = tree.nodes.index(self)
36
+ raise unless index
37
+ tree.nodes[index] = node
38
+ node.parent_tree = tree
39
+ self.parent_tree = nil
40
+ end
41
+
42
+ sig { returns(T.nilable(Scope)) }
43
+ def parent_scope
44
+ parent = T.let(parent_tree, T.nilable(Tree))
45
+ parent = parent.parent_tree until parent.is_a?(Scope) || parent.nil?
46
+ parent
47
+ end
48
+ end
49
+
50
+ class Comment < Node
51
+ extend T::Sig
52
+
53
+ sig { returns(String) }
54
+ attr_accessor :text
55
+
56
+ sig { params(text: String, loc: T.nilable(Loc)).void }
57
+ def initialize(text, loc: nil)
58
+ super(loc: loc)
59
+ @text = text
60
+ end
61
+
62
+ sig { params(other: Object).returns(T::Boolean) }
63
+ def ==(other)
64
+ return false unless other.is_a?(Comment)
65
+ text == other.text
66
+ end
67
+ end
68
+
69
+ class EmptyComment < Comment
70
+ extend T::Sig
71
+
72
+ sig { params(loc: T.nilable(Loc)).void }
73
+ def initialize(loc: nil)
74
+ super("", loc: loc)
75
+ end
76
+ end
77
+
78
+ class NodeWithComments < Node
79
+ extend T::Sig
80
+ extend T::Helpers
81
+
82
+ abstract!
83
+
84
+ sig { returns(T::Array[Comment]) }
85
+ attr_accessor :comments
86
+
87
+ sig { params(loc: T.nilable(Loc), comments: T::Array[Comment]).void }
88
+ def initialize(loc: nil, comments: [])
89
+ super(loc: loc)
90
+ @comments = comments
91
+ end
92
+ end
93
+
94
+ class Tree < NodeWithComments
95
+ extend T::Sig
96
+
97
+ sig { returns(T::Array[Node]) }
98
+ attr_reader :nodes
99
+
100
+ sig do
101
+ params(
102
+ loc: T.nilable(Loc),
103
+ comments: T::Array[Comment],
104
+ block: T.nilable(T.proc.params(node: Tree).void)
105
+ ).void
106
+ end
107
+ def initialize(loc: nil, comments: [], &block)
108
+ super(loc: loc, comments: comments)
109
+ @nodes = T.let([], T::Array[Node])
110
+ block&.call(self)
111
+ end
112
+
113
+ sig { params(node: Node).void }
114
+ def <<(node)
115
+ node.parent_tree = self
116
+ @nodes << node
117
+ end
118
+
119
+ sig { returns(T::Boolean) }
120
+ def empty?
121
+ nodes.empty?
122
+ end
123
+ end
124
+
125
+ class File
126
+ extend T::Sig
127
+
128
+ sig { returns(Tree) }
129
+ attr_reader :root
130
+
131
+ sig { returns(T.nilable(String)) }
132
+ attr_reader :strictness
133
+
134
+ sig { returns(T::Array[Comment]) }
135
+ attr_accessor :comments
136
+
137
+ sig do
138
+ params(
139
+ strictness: T.nilable(String),
140
+ comments: T::Array[Comment],
141
+ block: T.nilable(T.proc.params(file: File).void)
142
+ ).void
143
+ end
144
+ def initialize(strictness: nil, comments: [], &block)
145
+ @root = T.let(Tree.new, Tree)
146
+ @strictness = strictness
147
+ @comments = comments
148
+ block&.call(self)
149
+ end
150
+
151
+ sig { params(node: Node).void }
152
+ def <<(node)
153
+ @root << node
154
+ end
155
+ end
156
+
157
+ # Scopes
158
+
159
+ class Scope < Tree
160
+ extend T::Helpers
161
+
162
+ abstract!
163
+
164
+ sig { abstract.returns(String) }
165
+ def fully_qualified_name; end
166
+
167
+ sig { override.returns(String) }
168
+ def to_s
169
+ fully_qualified_name
170
+ end
171
+ end
172
+
173
+ class Module < Scope
174
+ extend T::Sig
175
+
176
+ sig { returns(String) }
177
+ attr_accessor :name
178
+
179
+ sig do
180
+ params(
181
+ name: String,
182
+ loc: T.nilable(Loc),
183
+ comments: T::Array[Comment],
184
+ block: T.nilable(T.proc.params(node: Module).void)
185
+ ).void
186
+ end
187
+ def initialize(name, loc: nil, comments: [], &block)
188
+ super(loc: loc, comments: comments) {}
189
+ @name = name
190
+ block&.call(self)
191
+ end
192
+
193
+ sig { override.returns(String) }
194
+ def fully_qualified_name
195
+ return name if name.start_with?("::")
196
+ "#{parent_scope&.fully_qualified_name}::#{name}"
197
+ end
198
+ end
199
+
200
+ class Class < Scope
201
+ extend T::Sig
202
+
203
+ sig { returns(String) }
204
+ attr_accessor :name
205
+
206
+ sig { returns(T.nilable(String)) }
207
+ attr_accessor :superclass_name
208
+
209
+ sig do
210
+ params(
211
+ name: String,
212
+ superclass_name: T.nilable(String),
213
+ loc: T.nilable(Loc),
214
+ comments: T::Array[Comment],
215
+ block: T.nilable(T.proc.params(node: Class).void)
216
+ ).void
217
+ end
218
+ def initialize(name, superclass_name: nil, loc: nil, comments: [], &block)
219
+ super(loc: loc, comments: comments) {}
220
+ @name = name
221
+ @superclass_name = superclass_name
222
+ block&.call(self)
223
+ end
224
+
225
+ sig { override.returns(String) }
226
+ def fully_qualified_name
227
+ return name if name.start_with?("::")
228
+ "#{parent_scope&.fully_qualified_name}::#{name}"
229
+ end
230
+ end
231
+
232
+ class SingletonClass < Scope
233
+ extend T::Sig
234
+
235
+ sig do
236
+ params(
237
+ loc: T.nilable(Loc),
238
+ comments: T::Array[Comment],
239
+ block: T.nilable(T.proc.params(node: SingletonClass).void)
240
+ ).void
241
+ end
242
+ def initialize(loc: nil, comments: [], &block)
243
+ super(loc: loc, comments: comments) {}
244
+ block&.call(self)
245
+ end
246
+
247
+ sig { override.returns(String) }
248
+ def fully_qualified_name
249
+ "#{parent_scope&.fully_qualified_name}::<self>"
250
+ end
251
+ end
252
+
253
+ class Struct < Scope
254
+ extend T::Sig
255
+
256
+ sig { returns(String) }
257
+ attr_accessor :name
258
+
259
+ sig { returns(T::Array[Symbol]) }
260
+ attr_accessor :members
261
+
262
+ sig { returns(T::Boolean) }
263
+ attr_accessor :keyword_init
264
+
265
+ sig do
266
+ params(
267
+ name: String,
268
+ members: T::Array[Symbol],
269
+ keyword_init: T::Boolean,
270
+ loc: T.nilable(Loc),
271
+ comments: T::Array[Comment],
272
+ block: T.nilable(T.proc.params(struct: Struct).void)
273
+ ).void
274
+ end
275
+ def initialize(name, members: [], keyword_init: false, loc: nil, comments: [], &block)
276
+ super(loc: loc, comments: comments) {}
277
+ @name = name
278
+ @members = members
279
+ @keyword_init = keyword_init
280
+ block&.call(self)
281
+ end
282
+
283
+ sig { override.returns(String) }
284
+ def fully_qualified_name
285
+ return name if name.start_with?("::")
286
+ "#{parent_scope&.fully_qualified_name}::#{name}"
287
+ end
288
+ end
289
+
290
+ # Consts
291
+
292
+ class Const < NodeWithComments
293
+ extend T::Sig
294
+
295
+ sig { returns(String) }
296
+ attr_reader :name, :value
297
+
298
+ sig do
299
+ params(
300
+ name: String,
301
+ value: String,
302
+ loc: T.nilable(Loc),
303
+ comments: T::Array[Comment],
304
+ block: T.nilable(T.proc.params(node: Const).void)
305
+ ).void
306
+ end
307
+ def initialize(name, value, loc: nil, comments: [], &block)
308
+ super(loc: loc, comments: comments)
309
+ @name = name
310
+ @value = value
311
+ block&.call(self)
312
+ end
313
+
314
+ sig { returns(String) }
315
+ def fully_qualified_name
316
+ return name if name.start_with?("::")
317
+ "#{parent_scope&.fully_qualified_name}::#{name}"
318
+ end
319
+
320
+ sig { override.returns(String) }
321
+ def to_s
322
+ fully_qualified_name
323
+ end
324
+ end
325
+
326
+ # Attributes
327
+
328
+ class Attr < NodeWithComments
329
+ extend T::Sig
330
+ extend T::Helpers
331
+
332
+ abstract!
333
+
334
+ sig { returns(T::Array[Symbol]) }
335
+ attr_accessor :names
336
+
337
+ sig { returns(Visibility) }
338
+ attr_accessor :visibility
339
+
340
+ sig { returns(T::Array[Sig]) }
341
+ attr_reader :sigs
342
+
343
+ sig do
344
+ params(
345
+ name: Symbol,
346
+ names: T::Array[Symbol],
347
+ visibility: Visibility,
348
+ sigs: T::Array[Sig],
349
+ loc: T.nilable(Loc),
350
+ comments: T::Array[Comment]
351
+ ).void
352
+ end
353
+ def initialize(name, names, visibility: Public.new, sigs: [], loc: nil, comments: [])
354
+ super(loc: loc, comments: comments)
355
+ @names = T.let([name, *names], T::Array[Symbol])
356
+ @visibility = visibility
357
+ @sigs = sigs
358
+ end
359
+
360
+ sig { abstract.returns(T::Array[String]) }
361
+ def fully_qualified_names; end
362
+ end
363
+
364
+ class AttrAccessor < Attr
365
+ extend T::Sig
366
+
367
+ sig do
368
+ params(
369
+ name: Symbol,
370
+ names: Symbol,
371
+ visibility: Visibility,
372
+ sigs: T::Array[Sig],
373
+ loc: T.nilable(Loc),
374
+ comments: T::Array[Comment],
375
+ block: T.nilable(T.proc.params(node: AttrAccessor).void)
376
+ ).void
377
+ end
378
+ def initialize(name, *names, visibility: Public.new, sigs: [], loc: nil, comments: [], &block)
379
+ super(name, names, loc: loc, visibility: visibility, sigs: sigs, comments: comments)
380
+ block&.call(self)
381
+ end
382
+
383
+ sig { override.returns(T::Array[String]) }
384
+ def fully_qualified_names
385
+ parent_name = parent_scope&.fully_qualified_name
386
+ names.flat_map { |name| ["#{parent_name}##{name}", "#{parent_name}##{name}="] }
387
+ end
388
+
389
+ sig { override.returns(String) }
390
+ def to_s
391
+ symbols = names.map { |name| ":#{name}" }.join(", ")
392
+ "#{parent_scope&.fully_qualified_name}.attr_accessor(#{symbols})"
393
+ end
394
+ end
395
+
396
+ class AttrReader < Attr
397
+ extend T::Sig
398
+
399
+ sig do
400
+ params(
401
+ name: Symbol,
402
+ names: Symbol,
403
+ visibility: Visibility,
404
+ sigs: T::Array[Sig],
405
+ loc: T.nilable(Loc),
406
+ comments: T::Array[Comment],
407
+ block: T.nilable(T.proc.params(node: AttrReader).void)
408
+ ).void
409
+ end
410
+ def initialize(name, *names, visibility: Public.new, sigs: [], loc: nil, comments: [], &block)
411
+ super(name, names, loc: loc, visibility: visibility, sigs: sigs, comments: comments)
412
+ block&.call(self)
413
+ end
414
+
415
+ sig { override.returns(T::Array[String]) }
416
+ def fully_qualified_names
417
+ parent_name = parent_scope&.fully_qualified_name
418
+ names.map { |name| "#{parent_name}##{name}" }
419
+ end
420
+
421
+ sig { override.returns(String) }
422
+ def to_s
423
+ symbols = names.map { |name| ":#{name}" }.join(", ")
424
+ "#{parent_scope&.fully_qualified_name}.attr_reader(#{symbols})"
425
+ end
426
+ end
427
+
428
+ class AttrWriter < Attr
429
+ extend T::Sig
430
+
431
+ sig do
432
+ params(
433
+ name: Symbol,
434
+ names: Symbol,
435
+ visibility: Visibility,
436
+ sigs: T::Array[Sig],
437
+ loc: T.nilable(Loc),
438
+ comments: T::Array[Comment],
439
+ block: T.nilable(T.proc.params(node: AttrWriter).void)
440
+ ).void
441
+ end
442
+ def initialize(name, *names, visibility: Public.new, sigs: [], loc: nil, comments: [], &block)
443
+ super(name, names, loc: loc, visibility: visibility, sigs: sigs, comments: comments)
444
+ block&.call(self)
445
+ end
446
+
447
+ sig { override.returns(T::Array[String]) }
448
+ def fully_qualified_names
449
+ parent_name = parent_scope&.fully_qualified_name
450
+ names.map { |name| "#{parent_name}##{name}=" }
451
+ end
452
+
453
+ sig { override.returns(String) }
454
+ def to_s
455
+ symbols = names.map { |name| ":#{name}" }.join(", ")
456
+ "#{parent_scope&.fully_qualified_name}.attr_writer(#{symbols})"
457
+ end
458
+ end
459
+
460
+ # Methods and args
461
+
462
+ class Method < NodeWithComments
463
+ extend T::Sig
464
+
465
+ sig { returns(String) }
466
+ attr_accessor :name
467
+
468
+ sig { returns(T::Array[Param]) }
469
+ attr_reader :params
470
+
471
+ sig { returns(T::Boolean) }
472
+ attr_accessor :is_singleton
473
+
474
+ sig { returns(Visibility) }
475
+ attr_accessor :visibility
476
+
477
+ sig { returns(T::Array[Sig]) }
478
+ attr_accessor :sigs
479
+
480
+ sig do
481
+ params(
482
+ name: String,
483
+ params: T::Array[Param],
484
+ is_singleton: T::Boolean,
485
+ visibility: Visibility,
486
+ sigs: T::Array[Sig],
487
+ loc: T.nilable(Loc),
488
+ comments: T::Array[Comment],
489
+ block: T.nilable(T.proc.params(node: Method).void)
490
+ ).void
491
+ end
492
+ def initialize(
493
+ name,
494
+ params: [],
495
+ is_singleton: false,
496
+ visibility: Public.new,
497
+ sigs: [],
498
+ loc: nil,
499
+ comments: [],
500
+ &block
501
+ )
502
+ super(loc: loc, comments: comments)
503
+ @name = name
504
+ @params = params
505
+ @is_singleton = is_singleton
506
+ @visibility = visibility
507
+ @sigs = sigs
508
+ block&.call(self)
509
+ end
510
+
511
+ sig { params(param: Param).void }
512
+ def <<(param)
513
+ @params << param
514
+ end
515
+
516
+ sig { returns(String) }
517
+ def fully_qualified_name
518
+ if is_singleton
519
+ "#{parent_scope&.fully_qualified_name}::#{name}"
520
+ else
521
+ "#{parent_scope&.fully_qualified_name}##{name}"
522
+ end
523
+ end
524
+
525
+ sig { override.returns(String) }
526
+ def to_s
527
+ "#{fully_qualified_name}(#{params.join(", ")})"
528
+ end
529
+ end
530
+
531
+ class Param < NodeWithComments
532
+ extend T::Helpers
533
+ extend T::Sig
534
+
535
+ abstract!
536
+
537
+ sig { returns(String) }
538
+ attr_reader :name
539
+
540
+ sig do
541
+ params(
542
+ name: String,
543
+ loc: T.nilable(Loc),
544
+ comments: T::Array[Comment],
545
+ ).void
546
+ end
547
+ def initialize(name, loc: nil, comments: [])
548
+ super(loc: loc, comments: comments)
549
+ @name = name
550
+ end
551
+
552
+ sig { override.returns(String) }
553
+ def to_s
554
+ name
555
+ end
556
+ end
557
+
558
+ class ReqParam < Param
559
+ extend T::Sig
560
+
561
+ sig do
562
+ params(
563
+ name: String,
564
+ loc: T.nilable(Loc),
565
+ comments: T::Array[Comment],
566
+ block: T.nilable(T.proc.params(node: ReqParam).void)
567
+ ).void
568
+ end
569
+ def initialize(name, loc: nil, comments: [], &block)
570
+ super(name, loc: loc, comments: comments)
571
+ block&.call(self)
572
+ end
573
+
574
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
575
+ def ==(other)
576
+ ReqParam === other && name == other.name
577
+ end
578
+ end
579
+
580
+ class OptParam < Param
581
+ extend T::Sig
582
+
583
+ sig { returns(String) }
584
+ attr_reader :value
585
+
586
+ sig do
587
+ params(
588
+ name: String,
589
+ value: String,
590
+ loc: T.nilable(Loc),
591
+ comments: T::Array[Comment],
592
+ block: T.nilable(T.proc.params(node: OptParam).void)
593
+ ).void
594
+ end
595
+ def initialize(name, value, loc: nil, comments: [], &block)
596
+ super(name, loc: loc, comments: comments)
597
+ @value = value
598
+ block&.call(self)
599
+ end
600
+
601
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
602
+ def ==(other)
603
+ OptParam === other && name == other.name && value == other.value
604
+ end
605
+ end
606
+
607
+ class RestParam < Param
608
+ extend T::Sig
609
+
610
+ sig do
611
+ params(
612
+ name: String,
613
+ loc: T.nilable(Loc),
614
+ comments: T::Array[Comment],
615
+ block: T.nilable(T.proc.params(node: RestParam).void)
616
+ ).void
617
+ end
618
+ def initialize(name, loc: nil, comments: [], &block)
619
+ super(name, loc: loc, comments: comments)
620
+ block&.call(self)
621
+ end
622
+
623
+ sig { override.returns(String) }
624
+ def to_s
625
+ "*#{name}"
626
+ end
627
+
628
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
629
+ def ==(other)
630
+ RestParam === other && name == other.name
631
+ end
632
+ end
633
+
634
+ class KwParam < Param
635
+ extend T::Sig
636
+
637
+ sig do
638
+ params(
639
+ name: String,
640
+ loc: T.nilable(Loc),
641
+ comments: T::Array[Comment],
642
+ block: T.nilable(T.proc.params(node: KwParam).void)
643
+ ).void
644
+ end
645
+ def initialize(name, loc: nil, comments: [], &block)
646
+ super(name, loc: loc, comments: comments)
647
+ block&.call(self)
648
+ end
649
+
650
+ sig { override.returns(String) }
651
+ def to_s
652
+ "#{name}:"
653
+ end
654
+
655
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
656
+ def ==(other)
657
+ KwParam === other && name == other.name
658
+ end
659
+ end
660
+
661
+ class KwOptParam < Param
662
+ extend T::Sig
663
+
664
+ sig { returns(String) }
665
+ attr_reader :value
666
+
667
+ sig do
668
+ params(
669
+ name: String,
670
+ value: String,
671
+ loc: T.nilable(Loc),
672
+ comments: T::Array[Comment],
673
+ block: T.nilable(T.proc.params(node: KwOptParam).void)
674
+ ).void
675
+ end
676
+ def initialize(name, value, loc: nil, comments: [], &block)
677
+ super(name, loc: loc, comments: comments)
678
+ @value = value
679
+ block&.call(self)
680
+ end
681
+
682
+ sig { override.returns(String) }
683
+ def to_s
684
+ "#{name}:"
685
+ end
686
+
687
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
688
+ def ==(other)
689
+ KwOptParam === other && name == other.name && value == other.value
690
+ end
691
+ end
692
+
693
+ class KwRestParam < Param
694
+ extend T::Sig
695
+
696
+ sig do
697
+ params(
698
+ name: String,
699
+ loc: T.nilable(Loc),
700
+ comments: T::Array[Comment],
701
+ block: T.nilable(T.proc.params(node: KwRestParam).void)
702
+ ).void
703
+ end
704
+ def initialize(name, loc: nil, comments: [], &block)
705
+ super(name, loc: loc, comments: comments)
706
+ block&.call(self)
707
+ end
708
+
709
+ sig { override.returns(String) }
710
+ def to_s
711
+ "**#{name}:"
712
+ end
713
+
714
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
715
+ def ==(other)
716
+ KwRestParam === other && name == other.name
717
+ end
718
+ end
719
+
720
+ class BlockParam < Param
721
+ extend T::Sig
722
+
723
+ sig do
724
+ params(
725
+ name: String,
726
+ loc: T.nilable(Loc),
727
+ comments: T::Array[Comment],
728
+ block: T.nilable(T.proc.params(node: BlockParam).void)
729
+ ).void
730
+ end
731
+ def initialize(name, loc: nil, comments: [], &block)
732
+ super(name, loc: loc, comments: comments)
733
+ block&.call(self)
734
+ end
735
+
736
+ sig { override.returns(String) }
737
+ def to_s
738
+ "&#{name}"
739
+ end
740
+
741
+ sig { params(other: T.nilable(Object)).returns(T::Boolean) }
742
+ def ==(other)
743
+ BlockParam === other && name == other.name
744
+ end
745
+ end
746
+
747
+ # Mixins
748
+
749
+ class Mixin < NodeWithComments
750
+ extend T::Sig
751
+ extend T::Helpers
752
+
753
+ abstract!
754
+
755
+ sig { returns(T::Array[String]) }
756
+ attr_accessor :names
757
+
758
+ sig do
759
+ params(
760
+ name: String,
761
+ names: T::Array[String],
762
+ loc: T.nilable(Loc),
763
+ comments: T::Array[Comment]
764
+ ).void
765
+ end
766
+ def initialize(name, names, loc: nil, comments: [])
767
+ super(loc: loc, comments: comments)
768
+ @names = T.let([name, *names], T::Array[String])
769
+ end
770
+ end
771
+
772
+ class Include < Mixin
773
+ extend T::Sig
774
+
775
+ sig do
776
+ params(
777
+ name: String,
778
+ names: String,
779
+ loc: T.nilable(Loc),
780
+ comments: T::Array[Comment],
781
+ block: T.nilable(T.proc.params(node: Include).void)
782
+ ).void
783
+ end
784
+ def initialize(name, *names, loc: nil, comments: [], &block)
785
+ super(name, names, loc: loc, comments: comments)
786
+ block&.call(self)
787
+ end
788
+
789
+ sig { override.returns(String) }
790
+ def to_s
791
+ "#{parent_scope&.fully_qualified_name}.include(#{names.join(", ")})"
792
+ end
793
+ end
794
+
795
+ class Extend < Mixin
796
+ extend T::Sig
797
+
798
+ sig do
799
+ params(
800
+ name: String,
801
+ names: String,
802
+ loc: T.nilable(Loc),
803
+ comments: T::Array[Comment],
804
+ block: T.nilable(T.proc.params(node: Extend).void)
805
+ ).void
806
+ end
807
+ def initialize(name, *names, loc: nil, comments: [], &block)
808
+ super(name, names, loc: loc, comments: comments)
809
+ block&.call(self)
810
+ end
811
+
812
+ sig { override.returns(String) }
813
+ def to_s
814
+ "#{parent_scope&.fully_qualified_name}.extend(#{names.join(", ")})"
815
+ end
816
+ end
817
+
818
+ # Visibility
819
+
820
+ class Visibility < NodeWithComments
821
+ extend T::Sig
822
+ extend T::Helpers
823
+
824
+ abstract!
825
+
826
+ sig { returns(Symbol) }
827
+ attr_reader :visibility
828
+
829
+ sig { params(visibility: Symbol, loc: T.nilable(Loc), comments: T::Array[Comment]).void }
830
+ def initialize(visibility, loc: nil, comments: [])
831
+ super(loc: loc, comments: comments)
832
+ @visibility = visibility
833
+ end
834
+
835
+ sig { params(other: Visibility).returns(T::Boolean) }
836
+ def ==(other)
837
+ visibility == other.visibility
838
+ end
839
+
840
+ sig { returns(T::Boolean) }
841
+ def public?
842
+ visibility == :public
843
+ end
844
+
845
+ sig { returns(T::Boolean) }
846
+ def protected?
847
+ visibility == :protected
848
+ end
849
+
850
+ sig { returns(T::Boolean) }
851
+ def private?
852
+ visibility == :private
853
+ end
854
+ end
855
+
856
+ class Public < Visibility
857
+ extend T::Sig
858
+
859
+ sig do
860
+ params(
861
+ loc: T.nilable(Loc),
862
+ comments: T::Array[Comment],
863
+ block: T.nilable(T.proc.params(node: Public).void)
864
+ ).void
865
+ end
866
+ def initialize(loc: nil, comments: [], &block)
867
+ super(:public, loc: loc, comments: comments)
868
+ block&.call(self)
869
+ end
870
+ end
871
+
872
+ class Protected < Visibility
873
+ extend T::Sig
874
+
875
+ sig do
876
+ params(
877
+ loc: T.nilable(Loc),
878
+ comments: T::Array[Comment],
879
+ block: T.nilable(T.proc.params(node: Protected).void)
880
+ ).void
881
+ end
882
+ def initialize(loc: nil, comments: [], &block)
883
+ super(:protected, loc: loc, comments: comments)
884
+ block&.call(self)
885
+ end
886
+ end
887
+
888
+ class Private < Visibility
889
+ extend T::Sig
890
+
891
+ sig do
892
+ params(
893
+ loc: T.nilable(Loc),
894
+ comments: T::Array[Comment],
895
+ block: T.nilable(T.proc.params(node: Private).void)
896
+ ).void
897
+ end
898
+ def initialize(loc: nil, comments: [], &block)
899
+ super(:private, loc: loc, comments: comments)
900
+ block&.call(self)
901
+ end
902
+ end
903
+
904
+ # Sorbet's sigs
905
+
906
+ class Sig < Node
907
+ extend T::Sig
908
+
909
+ sig { returns(T::Array[SigParam]) }
910
+ attr_reader :params
911
+
912
+ sig { returns(T.nilable(String)) }
913
+ attr_accessor :return_type
914
+
915
+ sig { returns(T::Boolean) }
916
+ attr_accessor :is_abstract, :is_override, :is_overridable
917
+
918
+ sig { returns(T::Array[String]) }
919
+ attr_reader :type_params
920
+
921
+ sig { returns(T.nilable(Symbol)) }
922
+ attr_accessor :checked
923
+
924
+ sig do
925
+ params(
926
+ params: T::Array[SigParam],
927
+ return_type: T.nilable(String),
928
+ is_abstract: T::Boolean,
929
+ is_override: T::Boolean,
930
+ is_overridable: T::Boolean,
931
+ type_params: T::Array[String],
932
+ checked: T.nilable(Symbol),
933
+ loc: T.nilable(Loc),
934
+ block: T.nilable(T.proc.params(node: Sig).void)
935
+ ).void
936
+ end
937
+ def initialize(
938
+ params: [],
939
+ return_type: nil,
940
+ is_abstract: false,
941
+ is_override: false,
942
+ is_overridable: false,
943
+ type_params: [],
944
+ checked: nil,
945
+ loc: nil,
946
+ &block
947
+ )
948
+ super(loc: loc)
949
+ @params = params
950
+ @return_type = return_type
951
+ @is_abstract = is_abstract
952
+ @is_override = is_override
953
+ @is_overridable = is_overridable
954
+ @type_params = type_params
955
+ @checked = checked
956
+ block&.call(self)
957
+ end
958
+
959
+ sig { params(param: SigParam).void }
960
+ def <<(param)
961
+ @params << param
962
+ end
963
+
964
+ sig { params(other: Object).returns(T::Boolean) }
965
+ def ==(other)
966
+ return false unless other.is_a?(Sig)
967
+ params == other.params && return_type == other.return_type && is_abstract == other.is_abstract &&
968
+ is_override == other.is_override && is_overridable == other.is_overridable &&
969
+ type_params == other.type_params && checked == other.checked
970
+ end
971
+ end
972
+
973
+ class SigParam < NodeWithComments
974
+ extend T::Sig
975
+
976
+ sig { returns(String) }
977
+ attr_reader :name, :type
978
+
979
+ sig do
980
+ params(
981
+ name: String,
982
+ type: String,
983
+ loc: T.nilable(Loc),
984
+ comments: T::Array[Comment],
985
+ block: T.nilable(T.proc.params(node: SigParam).void)
986
+ ).void
987
+ end
988
+ def initialize(name, type, loc: nil, comments: [], &block)
989
+ super(loc: loc, comments: comments)
990
+ @name = name
991
+ @type = type
992
+ block&.call(self)
993
+ end
994
+
995
+ sig { params(other: Object).returns(T::Boolean) }
996
+ def ==(other)
997
+ other.is_a?(SigParam) && name == other.name && type == other.type
998
+ end
999
+ end
1000
+
1001
+ # Sorbet's T::Struct
1002
+
1003
+ class TStruct < Class
1004
+ extend T::Sig
1005
+
1006
+ sig do
1007
+ params(
1008
+ name: String,
1009
+ loc: T.nilable(Loc),
1010
+ comments: T::Array[Comment],
1011
+ block: T.nilable(T.proc.params(klass: TStruct).void)
1012
+ ).void
1013
+ end
1014
+ def initialize(name, loc: nil, comments: [], &block)
1015
+ super(name, superclass_name: "::T::Struct", loc: loc, comments: comments) {}
1016
+ block&.call(self)
1017
+ end
1018
+ end
1019
+
1020
+ class TStructField < NodeWithComments
1021
+ extend T::Sig
1022
+ extend T::Helpers
1023
+
1024
+ abstract!
1025
+
1026
+ sig { returns(String) }
1027
+ attr_accessor :name, :type
1028
+
1029
+ sig { returns(T.nilable(String)) }
1030
+ attr_accessor :default
1031
+
1032
+ sig do
1033
+ params(
1034
+ name: String,
1035
+ type: String,
1036
+ default: T.nilable(String),
1037
+ loc: T.nilable(Loc),
1038
+ comments: T::Array[Comment]
1039
+ ).void
1040
+ end
1041
+ def initialize(name, type, default: nil, loc: nil, comments: [])
1042
+ super(loc: loc, comments: comments)
1043
+ @name = name
1044
+ @type = type
1045
+ @default = default
1046
+ end
1047
+
1048
+ sig { abstract.returns(T::Array[String]) }
1049
+ def fully_qualified_names; end
1050
+ end
1051
+
1052
+ class TStructConst < TStructField
1053
+ extend T::Sig
1054
+
1055
+ sig do
1056
+ params(
1057
+ name: String,
1058
+ type: String,
1059
+ default: T.nilable(String),
1060
+ loc: T.nilable(Loc),
1061
+ comments: T::Array[Comment],
1062
+ block: T.nilable(T.proc.params(node: TStructConst).void)
1063
+ ).void
1064
+ end
1065
+ def initialize(name, type, default: nil, loc: nil, comments: [], &block)
1066
+ super(name, type, default: default, loc: loc, comments: comments)
1067
+ block&.call(self)
1068
+ end
1069
+
1070
+ sig { override.returns(T::Array[String]) }
1071
+ def fully_qualified_names
1072
+ parent_name = parent_scope&.fully_qualified_name
1073
+ ["#{parent_name}##{name}"]
1074
+ end
1075
+
1076
+ sig { override.returns(String) }
1077
+ def to_s
1078
+ "#{parent_scope&.fully_qualified_name}.const(:#{name})"
1079
+ end
1080
+ end
1081
+
1082
+ class TStructProp < TStructField
1083
+ extend T::Sig
1084
+
1085
+ sig do
1086
+ params(
1087
+ name: String,
1088
+ type: String,
1089
+ default: T.nilable(String),
1090
+ loc: T.nilable(Loc),
1091
+ comments: T::Array[Comment],
1092
+ block: T.nilable(T.proc.params(node: TStructProp).void)
1093
+ ).void
1094
+ end
1095
+ def initialize(name, type, default: nil, loc: nil, comments: [], &block)
1096
+ super(name, type, default: default, loc: loc, comments: comments)
1097
+ block&.call(self)
1098
+ end
1099
+
1100
+ sig { override.returns(T::Array[String]) }
1101
+ def fully_qualified_names
1102
+ parent_name = parent_scope&.fully_qualified_name
1103
+ ["#{parent_name}##{name}", "#{parent_name}##{name}="]
1104
+ end
1105
+
1106
+ sig { override.returns(String) }
1107
+ def to_s
1108
+ "#{parent_scope&.fully_qualified_name}.prop(:#{name})"
1109
+ end
1110
+ end
1111
+
1112
+ # Sorbet's T::Enum
1113
+
1114
+ class TEnum < Class
1115
+ extend T::Sig
1116
+
1117
+ sig do
1118
+ params(
1119
+ name: String,
1120
+ loc: T.nilable(Loc),
1121
+ comments: T::Array[Comment],
1122
+ block: T.nilable(T.proc.params(klass: TEnum).void)
1123
+ ).void
1124
+ end
1125
+ def initialize(name, loc: nil, comments: [], &block)
1126
+ super(name, superclass_name: "::T::Enum", loc: loc, comments: comments) {}
1127
+ block&.call(self)
1128
+ end
1129
+ end
1130
+
1131
+ class TEnumBlock < NodeWithComments
1132
+ extend T::Sig
1133
+
1134
+ sig { returns(T::Array[String]) }
1135
+ attr_reader :names
1136
+
1137
+ sig do
1138
+ params(
1139
+ names: T::Array[String],
1140
+ loc: T.nilable(Loc),
1141
+ comments: T::Array[Comment],
1142
+ block: T.nilable(T.proc.params(node: TEnumBlock).void)
1143
+ ).void
1144
+ end
1145
+ def initialize(names = [], loc: nil, comments: [], &block)
1146
+ super(loc: loc, comments: comments)
1147
+ @names = names
1148
+ block&.call(self)
1149
+ end
1150
+
1151
+ sig { returns(T::Boolean) }
1152
+ def empty?
1153
+ names.empty?
1154
+ end
1155
+
1156
+ sig { params(name: String).void }
1157
+ def <<(name)
1158
+ @names << name
1159
+ end
1160
+
1161
+ sig { override.returns(String) }
1162
+ def to_s
1163
+ "#{parent_scope&.fully_qualified_name}.enums"
1164
+ end
1165
+ end
1166
+
1167
+ # Sorbet's misc.
1168
+
1169
+ class Helper < NodeWithComments
1170
+ extend T::Helpers
1171
+
1172
+ sig { returns(String) }
1173
+ attr_reader :name
1174
+
1175
+ sig do
1176
+ params(
1177
+ name: String,
1178
+ loc: T.nilable(Loc),
1179
+ comments: T::Array[Comment],
1180
+ block: T.nilable(T.proc.params(node: Helper).void)
1181
+ ).void
1182
+ end
1183
+ def initialize(name, loc: nil, comments: [], &block)
1184
+ super(loc: loc, comments: comments)
1185
+ @name = name
1186
+ block&.call(self)
1187
+ end
1188
+
1189
+ sig { override.returns(String) }
1190
+ def to_s
1191
+ "#{parent_scope&.fully_qualified_name}.#{name}!"
1192
+ end
1193
+ end
1194
+
1195
+ class TypeMember < NodeWithComments
1196
+ extend T::Sig
1197
+
1198
+ sig { returns(String) }
1199
+ attr_reader :name, :value
1200
+
1201
+ sig do
1202
+ params(
1203
+ name: String,
1204
+ value: String,
1205
+ loc: T.nilable(Loc),
1206
+ comments: T::Array[Comment],
1207
+ block: T.nilable(T.proc.params(node: TypeMember).void)
1208
+ ).void
1209
+ end
1210
+ def initialize(name, value, loc: nil, comments: [], &block)
1211
+ super(loc: loc, comments: comments)
1212
+ @name = name
1213
+ @value = value
1214
+ block&.call(self)
1215
+ end
1216
+
1217
+ sig { returns(String) }
1218
+ def fully_qualified_name
1219
+ return name if name.start_with?("::")
1220
+ "#{parent_scope&.fully_qualified_name}::#{name}"
1221
+ end
1222
+
1223
+ sig { override.returns(String) }
1224
+ def to_s
1225
+ fully_qualified_name
1226
+ end
1227
+ end
1228
+
1229
+ class MixesInClassMethods < Mixin
1230
+ extend T::Sig
1231
+
1232
+ sig do
1233
+ params(
1234
+ name: String,
1235
+ names: String,
1236
+ loc: T.nilable(Loc),
1237
+ comments: T::Array[Comment],
1238
+ block: T.nilable(T.proc.params(node: MixesInClassMethods).void)
1239
+ ).void
1240
+ end
1241
+ def initialize(name, *names, loc: nil, comments: [], &block)
1242
+ super(name, names, loc: loc, comments: comments)
1243
+ block&.call(self)
1244
+ end
1245
+
1246
+ sig { override.returns(String) }
1247
+ def to_s
1248
+ "#{parent_scope&.fully_qualified_name}.mixes_in_class_methods(#{names.join(", ")})"
1249
+ end
1250
+ end
1251
+ end