rbi 0.2.2 → 0.3.3

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.
@@ -5,65 +5,70 @@ module RBI
5
5
  class RBSPrinter < Visitor
6
6
  class Error < RBI::Error; end
7
7
 
8
- sig { returns(T::Boolean) }
8
+ #: bool
9
9
  attr_accessor :print_locs, :in_visibility_group
10
10
 
11
- sig { returns(T.nilable(Node)) }
11
+ #: Node?
12
12
  attr_reader :previous_node
13
13
 
14
- sig { returns(Integer) }
14
+ #: Integer
15
15
  attr_reader :current_indent
16
16
 
17
- sig { params(out: T.any(IO, StringIO), indent: Integer, print_locs: T::Boolean).void }
18
- def initialize(out: $stdout, indent: 0, print_locs: false)
17
+ #: bool
18
+ attr_accessor :positional_names
19
+
20
+ #: (?out: (IO | StringIO), ?indent: Integer, ?print_locs: bool, ?positional_names: bool) -> void
21
+ def initialize(out: $stdout, indent: 0, print_locs: false, positional_names: true)
19
22
  super()
20
23
  @out = out
21
24
  @current_indent = indent
22
25
  @print_locs = print_locs
23
- @in_visibility_group = T.let(false, T::Boolean)
24
- @previous_node = T.let(nil, T.nilable(Node))
26
+ @in_visibility_group = false #: bool
27
+ @previous_node = nil #: Node?
28
+ @positional_names = positional_names #: bool
25
29
  end
26
30
 
27
31
  # Printing
28
32
 
29
- sig { void }
33
+ #: -> void
30
34
  def indent
31
35
  @current_indent += 2
32
36
  end
33
37
 
34
- sig { void }
38
+ #: -> void
35
39
  def dedent
36
40
  @current_indent -= 2
37
41
  end
38
42
 
39
43
  # Print a string without indentation nor `\n` at the end.
40
- sig { params(string: String).void }
44
+ #: (String string) -> void
41
45
  def print(string)
42
46
  @out.print(string)
43
47
  end
44
48
 
45
49
  # Print a string without indentation but with a `\n` at the end.
46
- sig { params(string: T.nilable(String)).void }
50
+ #: (?String? string) -> void
47
51
  def printn(string = nil)
48
52
  print(string) if string
49
53
  print("\n")
50
54
  end
51
55
 
52
56
  # Print a string with indentation but without a `\n` at the end.
53
- sig { params(string: T.nilable(String)).void }
57
+ #: (?String? string) -> void
54
58
  def printt(string = nil)
55
59
  print(" " * @current_indent)
56
60
  print(string) if string
57
61
  end
58
62
 
59
63
  # Print a string with indentation and `\n` at the end.
60
- sig { params(string: String).void }
64
+ #: (String string) -> void
61
65
  def printl(string)
62
66
  printt
63
67
  printn(string)
64
68
  end
65
69
 
66
- sig { override.params(nodes: T::Array[Node]).void }
70
+ # @override
71
+ #: (Array[Node] nodes) -> void
67
72
  def visit_all(nodes)
68
73
  previous_node = @previous_node
69
74
  @previous_node = nil
@@ -74,7 +79,8 @@ module RBI
74
79
  @previous_node = previous_node
75
80
  end
76
81
 
77
- sig { override.params(file: File).void }
82
+ # @override
83
+ #: (File file) -> void
78
84
  def visit_file(file)
79
85
  unless file.comments.empty?
80
86
  visit_all(file.comments)
@@ -86,7 +92,8 @@ module RBI
86
92
  end
87
93
  end
88
94
 
89
- sig { override.params(node: Comment).void }
95
+ # @override
96
+ #: (Comment node) -> void
90
97
  def visit_comment(node)
91
98
  lines = node.text.lines
92
99
 
@@ -102,39 +109,45 @@ module RBI
102
109
  end
103
110
  end
104
111
 
105
- sig { override.params(node: BlankLine).void }
112
+ # @override
113
+ #: (BlankLine node) -> void
106
114
  def visit_blank_line(node)
107
115
  printn
108
116
  end
109
117
 
110
- sig { override.params(node: Tree).void }
118
+ # @override
119
+ #: (Tree node) -> void
111
120
  def visit_tree(node)
112
121
  visit_all(node.comments)
113
122
  printn if !node.comments.empty? && !node.empty?
114
123
  visit_all(node.nodes)
115
124
  end
116
125
 
117
- sig { override.params(node: Module).void }
126
+ # @override
127
+ #: (Module node) -> void
118
128
  def visit_module(node)
119
129
  visit_scope(node)
120
130
  end
121
131
 
122
- sig { override.params(node: Class).void }
132
+ # @override
133
+ #: (Class node) -> void
123
134
  def visit_class(node)
124
135
  visit_scope(node)
125
136
  end
126
137
 
127
- sig { override.params(node: Struct).void }
138
+ # @override
139
+ #: (Struct node) -> void
128
140
  def visit_struct(node)
129
141
  visit_scope(node)
130
142
  end
131
143
 
132
- sig { override.params(node: SingletonClass).void }
144
+ # @override
145
+ #: (SingletonClass node) -> void
133
146
  def visit_singleton_class(node)
134
147
  visit_scope(node)
135
148
  end
136
149
 
137
- sig { params(node: Scope).void }
150
+ #: (Scope node) -> void
138
151
  def visit_scope(node)
139
152
  print_blank_line_before(node)
140
153
  print_loc(node)
@@ -144,7 +157,7 @@ module RBI
144
157
  visit_scope_body(node)
145
158
  end
146
159
 
147
- sig { params(node: Scope).void }
160
+ #: (Scope node) -> void
148
161
  def visit_scope_header(node)
149
162
  node.nodes.grep(Helper).each do |helper|
150
163
  visit(Comment.new("@#{helper.name}"))
@@ -197,7 +210,7 @@ module RBI
197
210
  printn
198
211
  end
199
212
 
200
- sig { params(node: Scope).void }
213
+ #: (Scope node) -> void
201
214
  def visit_scope_body(node)
202
215
  unless node.empty?
203
216
  indent
@@ -209,7 +222,8 @@ module RBI
209
222
  end
210
223
  end
211
224
 
212
- sig { override.params(node: Const).void }
225
+ # @override
226
+ #: (Const node) -> void
213
227
  def visit_const(node)
214
228
  print_blank_line_before(node)
215
229
  print_loc(node)
@@ -224,22 +238,25 @@ module RBI
224
238
  end
225
239
  end
226
240
 
227
- sig { override.params(node: AttrAccessor).void }
241
+ # @override
242
+ #: (AttrAccessor node) -> void
228
243
  def visit_attr_accessor(node)
229
244
  visit_attr(node)
230
245
  end
231
246
 
232
- sig { override.params(node: AttrReader).void }
247
+ # @override
248
+ #: (AttrReader node) -> void
233
249
  def visit_attr_reader(node)
234
250
  visit_attr(node)
235
251
  end
236
252
 
237
- sig { override.params(node: AttrWriter).void }
253
+ # @override
254
+ #: (AttrWriter node) -> void
238
255
  def visit_attr_writer(node)
239
256
  visit_attr(node)
240
257
  end
241
258
 
242
- sig { params(node: Attr).void }
259
+ #: (Attr node) -> void
243
260
  def visit_attr(node)
244
261
  print_blank_line_before(node)
245
262
 
@@ -271,40 +288,58 @@ module RBI
271
288
  end
272
289
  end
273
290
 
274
- sig { params(node: RBI::Attr, sig: Sig).void }
291
+ #: (RBI::Attr node, Sig sig) -> void
275
292
  def print_attr_sig(node, sig)
293
+ ret_type = sig.return_type
294
+
276
295
  type = case node
277
- when AttrAccessor, AttrReader
278
- parse_type(sig.return_type)
296
+ when AttrReader, AttrAccessor
297
+ parse_type(ret_type).rbs_string
279
298
  else
280
- first_arg = sig.params.first
281
- if first_arg
282
- parse_type(first_arg.type)
299
+ # For attr_writer, Sorbet will prioritize the return type over the argument type in case of mismatch
300
+ arg_type = sig.params.first
301
+ if arg_type && (ret_type.is_a?(Type::Void) || ret_type == "void")
302
+ # If we have an argument type and the return type is void, we prioritize the argument type
303
+ parse_type(arg_type.type).rbs_string
283
304
  else
284
- Type.untyped
305
+ # Otherwise, we prioritize the return type
306
+ parse_type(ret_type).rbs_string
285
307
  end
286
308
  end
287
309
 
288
- print(type.rbs_string)
310
+ print(type)
289
311
  end
290
312
 
291
- sig { override.params(node: Method).void }
313
+ # @override
314
+ #: (Method node) -> void
292
315
  def visit_method(node)
293
316
  print_blank_line_before(node)
294
317
  visit_all(node.comments)
295
318
 
319
+ if node.sigs.any?(&:without_runtime)
320
+ printl("# @without_runtime")
321
+ end
322
+
296
323
  if node.sigs.any?(&:is_abstract)
297
324
  printl("# @abstract")
298
325
  end
299
326
 
300
327
  if node.sigs.any?(&:is_override)
301
- printl("# @override")
328
+ if node.sigs.any?(&:allow_incompatible_override)
329
+ printl("# @override(allow_incompatible: true)")
330
+ else
331
+ printl("# @override")
332
+ end
302
333
  end
303
334
 
304
335
  if node.sigs.any?(&:is_overridable)
305
336
  printl("# @overridable")
306
337
  end
307
338
 
339
+ if node.sigs.any?(&:is_final)
340
+ printl("# @final")
341
+ end
342
+
308
343
  print_loc(node)
309
344
  printt
310
345
  unless in_visibility_group || node.visibility.public?
@@ -318,7 +353,10 @@ module RBI
318
353
  print(": ")
319
354
  if sigs.any?
320
355
  first, *rest = sigs
321
- print_method_sig(node, T.must(first))
356
+ print_method_sig(
357
+ node,
358
+ first, #: as !nil
359
+ )
322
360
  if rest.any?
323
361
  spaces = node.name.size + 4
324
362
  rest.each do |sig|
@@ -346,10 +384,10 @@ module RBI
346
384
  printn
347
385
  end
348
386
 
349
- sig { params(node: RBI::Method, sig: Sig).void }
387
+ #: (RBI::Method node, Sig sig) -> void
350
388
  def print_method_sig(node, sig)
351
389
  unless sig.type_params.empty?
352
- print("[#{sig.type_params.map { |t| "TYPE_#{t}" }.join(", ")}] ")
390
+ print("[#{sig.type_params.join(", ")}] ")
353
391
  end
354
392
 
355
393
  block_param = node.params.find { |param| param.is_a?(BlockParam) }
@@ -388,9 +426,7 @@ module RBI
388
426
  type_string = "(?) -> untyped"
389
427
  block_is_nilable = true
390
428
  when Type::Simple
391
- if block_type.name == "Proc"
392
- type_string = "(?) -> untyped"
393
- end
429
+ type_string = "(?) -> untyped"
394
430
  skip = true if block_type.name == "NilClass"
395
431
  end
396
432
 
@@ -410,52 +446,91 @@ module RBI
410
446
  print(" # #{loc}") if loc && print_locs
411
447
  end
412
448
 
413
- sig { override.params(node: ReqParam).void }
449
+ #: (Sig node) -> void
450
+ def visit_sig(node)
451
+ if node.params
452
+ print("(")
453
+ node.params.each do |param|
454
+ visit(param)
455
+ end
456
+ print(") ")
457
+ end
458
+
459
+ print("-> #{parse_type(node.return_type).rbs_string}")
460
+ end
461
+
462
+ #: (SigParam node) -> void
463
+ def visit_sig_param(node)
464
+ print(parse_type(node.type).rbs_string)
465
+ end
466
+
467
+ # @override
468
+ #: (ReqParam node) -> void
414
469
  def visit_req_param(node)
415
- print("untyped #{node.name}")
470
+ if @positional_names
471
+ print("untyped #{node.name}")
472
+ else
473
+ print("untyped")
474
+ end
416
475
  end
417
476
 
418
- sig { override.params(node: OptParam).void }
477
+ # @override
478
+ #: (OptParam node) -> void
419
479
  def visit_opt_param(node)
420
- print("?untyped #{node.name}")
480
+ if @positional_names
481
+ print("?untyped #{node.name}")
482
+ else
483
+ print("?untyped")
484
+ end
421
485
  end
422
486
 
423
- sig { override.params(node: RestParam).void }
487
+ # @override
488
+ #: (RestParam node) -> void
424
489
  def visit_rest_param(node)
425
- print("*untyped #{node.name}")
490
+ if @positional_names
491
+ print("*untyped #{node.name}")
492
+ else
493
+ print("*untyped")
494
+ end
426
495
  end
427
496
 
428
- sig { override.params(node: KwParam).void }
497
+ # @override
498
+ #: (KwParam node) -> void
429
499
  def visit_kw_param(node)
430
500
  print("#{node.name}: untyped")
431
501
  end
432
502
 
433
- sig { override.params(node: KwOptParam).void }
503
+ # @override
504
+ #: (KwOptParam node) -> void
434
505
  def visit_kw_opt_param(node)
435
506
  print("?#{node.name}: untyped")
436
507
  end
437
508
 
438
- sig { override.params(node: KwRestParam).void }
509
+ # @override
510
+ #: (KwRestParam node) -> void
439
511
  def visit_kw_rest_param(node)
440
512
  print("**#{node.name}: untyped")
441
513
  end
442
514
 
443
- sig { override.params(node: BlockParam).void }
515
+ # @override
516
+ #: (BlockParam node) -> void
444
517
  def visit_block_param(node)
445
518
  print("{ (*untyped) -> untyped } ")
446
519
  end
447
520
 
448
- sig { override.params(node: Include).void }
521
+ # @override
522
+ #: (Include node) -> void
449
523
  def visit_include(node)
450
524
  visit_mixin(node)
451
525
  end
452
526
 
453
- sig { override.params(node: Extend).void }
527
+ # @override
528
+ #: (Extend node) -> void
454
529
  def visit_extend(node)
455
530
  visit_mixin(node)
456
531
  end
457
532
 
458
- sig { params(node: Mixin).void }
533
+ #: (Mixin node) -> void
459
534
  def visit_mixin(node)
460
535
  return if node.is_a?(MixesInClassMethods) # no-op, `mixes_in_class_methods` is not supported in RBS
461
536
 
@@ -472,22 +547,25 @@ module RBI
472
547
  printn(" #{node.names.join(", ")}")
473
548
  end
474
549
 
475
- sig { override.params(node: Public).void }
550
+ # @override
551
+ #: (Public node) -> void
476
552
  def visit_public(node)
477
553
  visit_visibility(node)
478
554
  end
479
555
 
480
- sig { override.params(node: Protected).void }
556
+ # @override
557
+ #: (Protected node) -> void
481
558
  def visit_protected(node)
482
559
  # no-op, `protected` is not supported in RBS
483
560
  end
484
561
 
485
- sig { override.params(node: Private).void }
562
+ # @override
563
+ #: (Private node) -> void
486
564
  def visit_private(node)
487
565
  visit_visibility(node)
488
566
  end
489
567
 
490
- sig { params(node: Visibility).void }
568
+ #: (Visibility node) -> void
491
569
  def visit_visibility(node)
492
570
  print_blank_line_before(node)
493
571
  print_loc(node)
@@ -496,22 +574,26 @@ module RBI
496
574
  printl(node.visibility.to_s)
497
575
  end
498
576
 
499
- sig { override.params(node: Send).void }
577
+ # @override
578
+ #: (Send node) -> void
500
579
  def visit_send(node)
501
580
  # no-op, arbitrary sends are not supported in RBS
502
581
  end
503
582
 
504
- sig { override.params(node: Arg).void }
583
+ # @override
584
+ #: (Arg node) -> void
505
585
  def visit_arg(node)
506
586
  # no-op
507
587
  end
508
588
 
509
- sig { override.params(node: KwArg).void }
589
+ # @override
590
+ #: (KwArg node) -> void
510
591
  def visit_kw_arg(node)
511
592
  # no-op
512
593
  end
513
594
 
514
- sig { override.params(node: TStruct).void }
595
+ # @override
596
+ #: (TStruct node) -> void
515
597
  def visit_tstruct(node)
516
598
  print_blank_line_before(node)
517
599
  print_loc(node)
@@ -545,65 +627,77 @@ module RBI
545
627
  printl("end")
546
628
  end
547
629
 
548
- sig { override.params(node: TStructConst).void }
630
+ # @override
631
+ #: (TStructConst node) -> void
549
632
  def visit_tstruct_const(node)
550
633
  # `T::Struct.const` is not supported in RBS instead we generate an attribute reader
551
634
  accessor = AttrReader.new(node.name.to_sym, comments: node.comments, sigs: [Sig.new(return_type: node.type)])
552
635
  visit_attr_reader(accessor)
553
636
  end
554
637
 
555
- sig { override.params(node: TStructProp).void }
638
+ # @override
639
+ #: (TStructProp node) -> void
556
640
  def visit_tstruct_prop(node)
557
641
  # `T::Struct.prop` is not supported in RBS instead we generate an attribute accessor
558
642
  accessor = AttrAccessor.new(node.name.to_sym, comments: node.comments, sigs: [Sig.new(return_type: node.type)])
559
643
  visit_attr_accessor(accessor)
560
644
  end
561
645
 
562
- sig { override.params(node: TEnum).void }
646
+ # @override
647
+ #: (TEnum node) -> void
563
648
  def visit_tenum(node)
564
649
  visit_scope(node)
565
650
  end
566
651
 
567
- sig { override.params(node: TEnumBlock).void }
652
+ # @override
653
+ #: (TEnumBlock node) -> void
568
654
  def visit_tenum_block(node)
569
- node.nodes.each do |child|
570
- child = if child.is_a?(Const) && child.value == "new"
571
- parent = node.parent_scope
572
- Const.new(
573
- child.name,
574
- "T.let(nil, #{parent.is_a?(TEnum) ? parent.name : "T.untyped"})",
575
- comments: child.comments,
576
- )
577
- else
578
- child
579
- end
580
- visit(child)
581
- @previous_node = child
655
+ visit_all(node.nodes)
656
+ end
657
+
658
+ # @override
659
+ #: (TEnumValue node) -> void
660
+ def visit_tenum_value(node)
661
+ print_blank_line_before(node)
662
+ print_loc(node)
663
+ visit_all(node.comments)
664
+
665
+ t_enum = node.parent_scope&.parent_scope
666
+
667
+ if t_enum.is_a?(TEnum)
668
+ printl("#{node.name}: #{t_enum.name}")
669
+ else
670
+ printl("#{node.name}: untyped")
582
671
  end
583
672
  end
584
673
 
585
- sig { override.params(node: TypeMember).void }
674
+ # @override
675
+ #: (TypeMember node) -> void
586
676
  def visit_type_member(node)
587
677
  # no-op, we already show them in the scope header
588
678
  end
589
679
 
590
- sig { override.params(node: Helper).void }
680
+ # @override
681
+ #: (Helper node) -> void
591
682
  def visit_helper(node)
592
683
  # no-op, we already show them in the scope header
593
684
  end
594
685
 
595
- sig { override.params(node: MixesInClassMethods).void }
686
+ # @override
687
+ #: (MixesInClassMethods node) -> void
596
688
  def visit_mixes_in_class_methods(node)
597
689
  visit_mixin(node)
598
690
  end
599
691
 
600
- sig { override.params(node: Group).void }
692
+ # @override
693
+ #: (Group node) -> void
601
694
  def visit_group(node)
602
695
  printn unless previous_node.nil?
603
696
  visit_all(node.nodes)
604
697
  end
605
698
 
606
- sig { override.params(node: VisibilityGroup).void }
699
+ # @override
700
+ #: (VisibilityGroup node) -> void
607
701
  def visit_visibility_group(node)
608
702
  self.in_visibility_group = true
609
703
  if node.visibility.public?
@@ -616,12 +710,14 @@ module RBI
616
710
  self.in_visibility_group = false
617
711
  end
618
712
 
619
- sig { override.params(node: RequiresAncestor).void }
713
+ # @override
714
+ #: (RequiresAncestor node) -> void
620
715
  def visit_requires_ancestor(node)
621
716
  # no-op, we already show them in the scope header
622
717
  end
623
718
 
624
- sig { override.params(node: ConflictTree).void }
719
+ # @override
720
+ #: (ConflictTree node) -> void
625
721
  def visit_conflict_tree(node)
626
722
  printl("<<<<<<< #{node.left_name}")
627
723
  visit(node.left)
@@ -630,7 +726,8 @@ module RBI
630
726
  printl(">>>>>>> #{node.right_name}")
631
727
  end
632
728
 
633
- sig { override.params(node: ScopeConflict).void }
729
+ # @override
730
+ #: (ScopeConflict node) -> void
634
731
  def visit_scope_conflict(node)
635
732
  print_blank_line_before(node)
636
733
  print_loc(node)
@@ -646,7 +743,7 @@ module RBI
646
743
 
647
744
  private
648
745
 
649
- sig { params(node: Node).void }
746
+ #: (Node node) -> void
650
747
  def print_blank_line_before(node)
651
748
  previous_node = self.previous_node
652
749
  return unless previous_node
@@ -665,13 +762,13 @@ module RBI
665
762
  printn
666
763
  end
667
764
 
668
- sig { params(node: Node).void }
765
+ #: (Node node) -> void
669
766
  def print_loc(node)
670
767
  loc = node.loc
671
768
  printl("# #{loc}") if loc && print_locs
672
769
  end
673
770
 
674
- sig { params(node: Method, param: SigParam).void }
771
+ #: (Method node, SigParam param) -> void
675
772
  def print_sig_param(node, param)
676
773
  type = parse_type(param.type).rbs_string
677
774
 
@@ -679,11 +776,23 @@ module RBI
679
776
 
680
777
  case orig_param
681
778
  when ReqParam
682
- print("#{type} #{param.name}")
779
+ if @positional_names
780
+ print("#{type} #{param.name}")
781
+ else
782
+ print(type)
783
+ end
683
784
  when OptParam
684
- print("?#{type} #{param.name}")
785
+ if @positional_names
786
+ print("?#{type} #{param.name}")
787
+ else
788
+ print("?#{type}")
789
+ end
685
790
  when RestParam
686
- print("*#{type} #{param.name}")
791
+ if @positional_names
792
+ print("*#{type} #{param.name}")
793
+ else
794
+ print("*#{type}")
795
+ end
687
796
  when KwParam
688
797
  print("#{param.name}: #{type}")
689
798
  when KwOptParam
@@ -695,7 +804,7 @@ module RBI
695
804
  end
696
805
  end
697
806
 
698
- sig { params(node: Param, last: T::Boolean).void }
807
+ #: (Param node, last: bool) -> void
699
808
  def print_param_comment_leading_space(node, last:)
700
809
  printn
701
810
  printt
@@ -713,7 +822,7 @@ module RBI
713
822
  end
714
823
  end
715
824
 
716
- sig { params(node: SigParam, last: T::Boolean).void }
825
+ #: (SigParam node, last: bool) -> void
717
826
  def print_sig_param_comment_leading_space(node, last:)
718
827
  printn
719
828
  printt
@@ -721,7 +830,7 @@ module RBI
721
830
  print(" ") unless last
722
831
  end
723
832
 
724
- sig { params(node: Node).returns(T::Boolean) }
833
+ #: (Node node) -> bool
725
834
  def oneline?(node)
726
835
  case node
727
836
  when ScopeConflict
@@ -743,7 +852,7 @@ module RBI
743
852
  end
744
853
  end
745
854
 
746
- sig { params(type: T.any(Type, String)).returns(Type) }
855
+ #: ((Type | String) type) -> Type
747
856
  def parse_type(type)
748
857
  return type if type.is_a?(Type)
749
858
 
@@ -755,7 +864,7 @@ module RBI
755
864
  # Parse a string containing a `T.let(x, X)` and extract the type
756
865
  #
757
866
  # Returns `nil` is the string is not a `T.let`.
758
- sig { params(code: T.nilable(String)).returns(T.nilable(String)) }
867
+ #: (String? code) -> String?
759
868
  def parse_t_let(code)
760
869
  return unless code
761
870
 
@@ -778,17 +887,15 @@ module RBI
778
887
  end
779
888
 
780
889
  class TypePrinter
781
- extend T::Sig
782
-
783
- sig { returns(String) }
890
+ #: String
784
891
  attr_reader :string
785
892
 
786
- sig { void }
893
+ #: -> void
787
894
  def initialize
788
- @string = T.let(String.new, String)
895
+ @string = String.new #: String
789
896
  end
790
897
 
791
- sig { params(node: Type).void }
898
+ #: (Type node) -> void
792
899
  def visit(node)
793
900
  case node
794
901
  when Type::Simple
@@ -832,19 +939,19 @@ module RBI
832
939
  end
833
940
  end
834
941
 
835
- sig { params(type: Type::Simple).void }
942
+ #: (Type::Simple type) -> void
836
943
  def visit_simple(type)
837
- @string << translate_t_type(type.name)
944
+ @string << translate_t_type(type.name.gsub(/\s/, ""))
838
945
  end
839
946
 
840
- sig { params(type: Type::Boolean).void }
947
+ #: (Type::Boolean type) -> void
841
948
  def visit_boolean(type)
842
949
  @string << "bool"
843
950
  end
844
951
 
845
- sig { params(type: Type::Generic).void }
952
+ #: (Type::Generic type) -> void
846
953
  def visit_generic(type)
847
- @string << translate_t_type(type.name)
954
+ @string << translate_t_type(type.name.gsub(/\s/, ""))
848
955
  @string << "["
849
956
  type.params.each_with_index do |arg, index|
850
957
  visit(arg)
@@ -853,50 +960,57 @@ module RBI
853
960
  @string << "]"
854
961
  end
855
962
 
856
- sig { params(type: Type::Anything).void }
963
+ #: (Type::Anything type) -> void
857
964
  def visit_anything(type)
858
965
  @string << "top"
859
966
  end
860
967
 
861
- sig { params(type: Type::Void).void }
968
+ #: (Type::Void type) -> void
862
969
  def visit_void(type)
863
970
  @string << "void"
864
971
  end
865
972
 
866
- sig { params(type: Type::NoReturn).void }
973
+ #: (Type::NoReturn type) -> void
867
974
  def visit_no_return(type)
868
975
  @string << "bot"
869
976
  end
870
977
 
871
- sig { params(type: Type::Untyped).void }
978
+ #: (Type::Untyped type) -> void
872
979
  def visit_untyped(type)
873
980
  @string << "untyped"
874
981
  end
875
982
 
876
- sig { params(type: Type::SelfType).void }
983
+ #: (Type::SelfType type) -> void
877
984
  def visit_self_type(type)
878
985
  @string << "self"
879
986
  end
880
987
 
881
- sig { params(type: Type::AttachedClass).void }
988
+ #: (Type::AttachedClass type) -> void
882
989
  def visit_attached_class(type)
883
990
  @string << "instance"
884
991
  end
885
992
 
886
- sig { params(type: Type::Nilable).void }
993
+ #: (Type::Nilable type) -> void
887
994
  def visit_nilable(type)
888
- visit(type.type)
995
+ inner = type.type
996
+ if inner.is_a?(Type::Proc)
997
+ @string << "("
998
+ end
999
+ visit(inner)
1000
+ if inner.is_a?(Type::Proc)
1001
+ @string << ")"
1002
+ end
889
1003
  @string << "?"
890
1004
  end
891
1005
 
892
- sig { params(type: Type::ClassOf).void }
1006
+ #: (Type::ClassOf type) -> void
893
1007
  def visit_class_of(type)
894
1008
  @string << "singleton("
895
1009
  visit(type.type)
896
1010
  @string << ")"
897
1011
  end
898
1012
 
899
- sig { params(type: Type::All).void }
1013
+ #: (Type::All type) -> void
900
1014
  def visit_all(type)
901
1015
  @string << "("
902
1016
  type.types.each_with_index do |arg, index|
@@ -906,7 +1020,7 @@ module RBI
906
1020
  @string << ")"
907
1021
  end
908
1022
 
909
- sig { params(type: Type::Any).void }
1023
+ #: (Type::Any type) -> void
910
1024
  def visit_any(type)
911
1025
  @string << "("
912
1026
  type.types.each_with_index do |arg, index|
@@ -916,7 +1030,7 @@ module RBI
916
1030
  @string << ")"
917
1031
  end
918
1032
 
919
- sig { params(type: Type::Tuple).void }
1033
+ #: (Type::Tuple type) -> void
920
1034
  def visit_tuple(type)
921
1035
  @string << "["
922
1036
  type.types.each_with_index do |arg, index|
@@ -926,18 +1040,27 @@ module RBI
926
1040
  @string << "]"
927
1041
  end
928
1042
 
929
- sig { params(type: Type::Shape).void }
1043
+ #: (Type::Shape type) -> void
930
1044
  def visit_shape(type)
931
1045
  @string << "{"
932
1046
  type.types.each_with_index do |(key, value), index|
933
- @string << "#{key}: "
1047
+ @string << case key
1048
+ when String
1049
+ "\"#{key}\" => "
1050
+ when Symbol
1051
+ if key.match?(/\A[a-zA-Z_]+[a-zA-Z0-9_]*\z/)
1052
+ "#{key}: "
1053
+ else
1054
+ "\"#{key}\": "
1055
+ end
1056
+ end
934
1057
  visit(value)
935
1058
  @string << ", " if index < type.types.size - 1
936
1059
  end
937
1060
  @string << "}"
938
1061
  end
939
1062
 
940
- sig { params(type: Type::Proc).void }
1063
+ #: (Type::Proc type) -> void
941
1064
  def visit_proc(type)
942
1065
  @string << "^"
943
1066
  if type.proc_params.any?
@@ -949,25 +1072,31 @@ module RBI
949
1072
  end
950
1073
  @string << ") "
951
1074
  end
1075
+ proc_bind = type.proc_bind
1076
+ if proc_bind
1077
+ @string << "[self: "
1078
+ visit(proc_bind)
1079
+ @string << "] "
1080
+ end
952
1081
  @string << "-> "
953
1082
  visit(type.proc_returns)
954
1083
  end
955
1084
 
956
- sig { params(type: Type::TypeParameter).void }
1085
+ #: (Type::TypeParameter type) -> void
957
1086
  def visit_type_parameter(type)
958
- @string << "TYPE_#{type.name}"
1087
+ @string << type.name.to_s
959
1088
  end
960
1089
 
961
- sig { params(type: Type::Class).void }
1090
+ #: (Type::Class type) -> void
962
1091
  def visit_class(type)
963
- @string << "singleton("
1092
+ @string << "Class["
964
1093
  visit(type.type)
965
- @string << ")"
1094
+ @string << "]"
966
1095
  end
967
1096
 
968
1097
  private
969
1098
 
970
- sig { params(type_name: String).returns(String) }
1099
+ #: (String type_name) -> String
971
1100
  def translate_t_type(type_name)
972
1101
  case type_name
973
1102
  when "T::Array"
@@ -983,15 +1112,13 @@ module RBI
983
1112
  end
984
1113
 
985
1114
  class File
986
- extend T::Sig
987
-
988
- sig { params(out: T.any(IO, StringIO), indent: Integer, print_locs: T::Boolean).void }
1115
+ #: (?out: (IO | StringIO), ?indent: Integer, ?print_locs: bool) -> void
989
1116
  def rbs_print(out: $stdout, indent: 0, print_locs: false)
990
1117
  p = RBSPrinter.new(out: out, indent: indent, print_locs: print_locs)
991
1118
  p.visit_file(self)
992
1119
  end
993
1120
 
994
- sig { params(indent: Integer, print_locs: T::Boolean).returns(String) }
1121
+ #: (?indent: Integer, ?print_locs: bool) -> String
995
1122
  def rbs_string(indent: 0, print_locs: false)
996
1123
  out = StringIO.new
997
1124
  rbs_print(out: out, indent: indent, print_locs: print_locs)
@@ -1000,26 +1127,22 @@ module RBI
1000
1127
  end
1001
1128
 
1002
1129
  class Node
1003
- extend T::Sig
1004
-
1005
- sig { params(out: T.any(IO, StringIO), indent: Integer, print_locs: T::Boolean).void }
1006
- def rbs_print(out: $stdout, indent: 0, print_locs: false)
1007
- p = RBSPrinter.new(out: out, indent: indent, print_locs: print_locs)
1130
+ #: (?out: (IO | StringIO), ?indent: Integer, ?print_locs: bool, ?positional_names: bool) -> void
1131
+ def rbs_print(out: $stdout, indent: 0, print_locs: false, positional_names: true)
1132
+ p = RBSPrinter.new(out: out, indent: indent, print_locs: print_locs, positional_names: positional_names)
1008
1133
  p.visit(self)
1009
1134
  end
1010
1135
 
1011
- sig { params(indent: Integer, print_locs: T::Boolean).returns(String) }
1012
- def rbs_string(indent: 0, print_locs: false)
1136
+ #: (?indent: Integer, ?print_locs: bool, ?positional_names: bool) -> String
1137
+ def rbs_string(indent: 0, print_locs: false, positional_names: true)
1013
1138
  out = StringIO.new
1014
- rbs_print(out: out, indent: indent, print_locs: print_locs)
1139
+ rbs_print(out: out, indent: indent, print_locs: print_locs, positional_names: positional_names)
1015
1140
  out.string
1016
1141
  end
1017
1142
  end
1018
1143
 
1019
1144
  class Type
1020
- extend T::Sig
1021
-
1022
- sig { returns(String) }
1145
+ #: -> String
1023
1146
  def rbs_string
1024
1147
  p = TypePrinter.new
1025
1148
  p.visit(self)