Class: RBI::RBSPrinter

Inherits:
Visitor show all
Defined in:
lib/rbi/rbs_printer.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit

Constructor Details

#initialize(out: $stdout, indent: 0, print_locs: false, positional_names: true, max_line_length: nil, force_multiline_signatures: false) ⇒ RBSPrinter

Returns a new instance of RBSPrinter.

Signature:

  • (

  • ?out: (IO | StringIO),

  • ?indent: Integer,

  • ?print_locs: bool,

  • ?positional_names: bool,

  • ?max_line_length: Integer?,

  • ?force_multiline_signatures: bool

  • ) -> void



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbi/rbs_printer.rb', line 31

def initialize(
  out: $stdout,
  indent: 0,
  print_locs: false,
  positional_names: true,
  max_line_length: nil,
  force_multiline_signatures: false
)
  super()
  @out = out
  @current_indent = indent
  @print_locs = print_locs
  @in_visibility_group = false #: bool
  @previous_node = nil #: Node?
  @positional_names = positional_names #: bool
  @max_line_length = max_line_length #: Integer?
  @force_multiline_signatures = force_multiline_signatures #: bool
end

Instance Attribute Details

#current_indentObject (readonly)

Signature:

  • Integer



15
16
17
# File 'lib/rbi/rbs_printer.rb', line 15

def current_indent
  @current_indent
end

#in_visibility_groupObject

Signature:

  • bool



9
10
11
# File 'lib/rbi/rbs_printer.rb', line 9

def in_visibility_group
  @in_visibility_group
end

#max_line_lengthObject (readonly)

Signature:

  • Integer?



21
22
23
# File 'lib/rbi/rbs_printer.rb', line 21

def max_line_length
  @max_line_length
end

#positional_namesObject

Signature:

  • bool



18
19
20
# File 'lib/rbi/rbs_printer.rb', line 18

def positional_names
  @positional_names
end

#previous_nodeObject (readonly)

Signature:

  • Node?



12
13
14
# File 'lib/rbi/rbs_printer.rb', line 12

def previous_node
  @previous_node
end

Signature:

  • bool



9
10
11
# File 'lib/rbi/rbs_printer.rb', line 9

def print_locs
  @print_locs
end

Instance Method Details

#dedentObject

Signature:

  • -> void



58
59
60
# File 'lib/rbi/rbs_printer.rb', line 58

def dedent
  @current_indent -= 2
end

#indentObject

Signature:

  • -> void



53
54
55
# File 'lib/rbi/rbs_printer.rb', line 53

def indent
  @current_indent += 2
end

Print a string without indentation nor \n at the end.

Signature:

  • (String string) -> void



64
65
66
# File 'lib/rbi/rbs_printer.rb', line 64

def print(string)
  @out.print(string)
end

Signature:

  • (RBI::Attr node, Sig sig) -> void



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rbi/rbs_printer.rb', line 311

def print_attr_sig(node, sig)
  ret_type = sig.return_type

  type = case node
  when AttrReader, AttrAccessor
    parse_type(ret_type).rbs_string
  else
    # For attr_writer, Sorbet will prioritize the return type over the argument type in case of mismatch
    arg_type = sig.params.first
    if arg_type && (ret_type.is_a?(Type::Void) || ret_type == "void")
      # If we have an argument type and the return type is void, we prioritize the argument type
      parse_type(arg_type.type).rbs_string
    else
      # Otherwise, we prioritize the return type
      parse_type(ret_type).rbs_string
    end
  end

  print(type)
end

Signature:

  • (RBI::Method node, Sig sig) -> void



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/rbi/rbs_printer.rb', line 409

def print_method_sig(node, sig)
  old_out = @out
  new_out = StringIO.new

  @out = new_out
  print_method_sig_inline(node, sig)
  @out = old_out

  max_line_length = @max_line_length
  if @force_multiline_signatures || (max_line_length && new_out.string.size > max_line_length)
    print_method_sig_multiline(node, sig)
  else
    print(new_out.string)
  end
end

Signature:

  • (RBI::Method node, Sig sig) -> void

Raises:



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
# File 'lib/rbi/rbs_printer.rb', line 426

def print_method_sig_inline(node, sig)
  unless sig.type_params.empty?
    print("[#{sig.type_params.join(", ")}] ")
  end

  raise Error, "Arity mismatch between method and signature" if sig.params.size != node.params.size

  block_params, params = sig.params.zip(node.params).partition do |_sig_param, method_param|
    method_param.is_a?(BlockParam)
  end

  raise Error, "Multiple block parameters found" if block_params.size > 1

  block_param = block_params.first if block_params.any?

  unless params.empty?
    print("(")
    params.each_with_index do |(sig_param, method_param), index|
      print(", ") if index > 0
      print_sig_param(
        sig_param,
        method_param, #: as !nil
      )
    end
    print(") ")
  end
  if (sig_param = block_param&.first)
    block_type = sig_param.type
    block_type = Type.parse_string(block_type) if block_type.is_a?(String)

    block_is_nilable = false
    if block_type.is_a?(Type::Nilable)
      block_is_nilable = true
      block_type = block_type.type
    end

    type_string = parse_type(block_type).rbs_string.delete_prefix("^")

    skip = false
    case block_type
    when Type::Untyped
      type_string = "(?) -> untyped"
      block_is_nilable = true
    when Type::Simple
      type_string = "(?) -> untyped"
      skip = true if block_type.name == "NilClass"
    end

    if skip
      # no-op, we skip the block definition
    elsif block_is_nilable
      print("?{ #{type_string} } ")
    else
      print("{ #{type_string} } ")
    end
  end

  type = parse_type(sig.return_type)
  print("-> #{type.rbs_string}")

  loc = sig.loc
  print(" # #{loc}") if loc && print_locs
end

Signature:

  • (RBI::Method node, Sig sig) -> void

Raises:



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
# File 'lib/rbi/rbs_printer.rb', line 491

def print_method_sig_multiline(node, sig)
  unless sig.type_params.empty?
    print("[#{sig.type_params.join(", ")}] ")
  end

  raise Error, "Arity mismatch between method and signature" if sig.params.size != node.params.size

  block_params, params = sig.params.zip(node.params).partition do |_sig_param, method_param|
    method_param.is_a?(BlockParam)
  end

  raise Error, "Multiple block parameters found" if block_params.size > 1

  block_param = block_params.first if block_params.any?

  unless params.empty?
    printl("(")
    indent
    params.each_with_index do |(sig_param, method_param), index|
      printt
      print_sig_param(
        sig_param,
        method_param, #: as !nil
      )
      print(",") if index < params.size - 1
      printn
    end
    dedent
    printt(") ")
  end
  if (block_param = block_param&.first)
    block_type = block_param.type
    block_type = Type.parse_string(block_type) if block_type.is_a?(String)

    block_is_nilable = false
    if block_type.is_a?(Type::Nilable)
      block_is_nilable = true
      block_type = block_type.type
    end

    type_string = parse_type(block_type).rbs_string.delete_prefix("^")

    skip = false
    case block_type
    when Type::Untyped
      type_string = "(?) -> untyped"
      block_is_nilable = true
    when Type::Simple
      type_string = "(?) -> untyped"
      skip = true if block_type.name == "NilClass"
    end

    if skip
      # no-op, we skip the block definition
    elsif block_is_nilable
      print("?{ #{type_string} } ")
    else
      print("{ #{type_string} } ")
    end
  end

  type = parse_type(sig.return_type)
  print("-> #{type.rbs_string}")

  loc = sig.loc
  print(" # #{loc}") if loc && print_locs
end

#printl(string) ⇒ Object

Print a string with indentation and \n at the end.

Signature:

  • (String string) -> void



84
85
86
87
# File 'lib/rbi/rbs_printer.rb', line 84

def printl(string)
  printt
  printn(string)
end

#printn(string = nil) ⇒ Object

Print a string without indentation but with a \n at the end.

Signature:

  • (?String? string) -> void



70
71
72
73
# File 'lib/rbi/rbs_printer.rb', line 70

def printn(string = nil)
  print(string) if string
  print("\n")
end

#printt(string = nil) ⇒ Object

Print a string with indentation but without a \n at the end.

Signature:

  • (?String? string) -> void



77
78
79
80
# File 'lib/rbi/rbs_printer.rb', line 77

def printt(string = nil)
  print(" " * @current_indent)
  print(string) if string
end

#visit_all(nodes) ⇒ Object

Signature:

  • (Array[Node] nodes) -> void



91
92
93
94
95
96
97
98
99
# File 'lib/rbi/rbs_printer.rb', line 91

def visit_all(nodes)
  previous_node = @previous_node
  @previous_node = nil
  nodes.each do |node|
    visit(node)
    @previous_node = node
  end
  @previous_node = previous_node
end

#visit_arg(node) ⇒ Object

Signature:

  • (Arg node) -> void



693
694
695
# File 'lib/rbi/rbs_printer.rb', line 693

def visit_arg(node)
  # no-op
end

#visit_attr(node) ⇒ Object

Signature:

  • (Attr node) -> void



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
# File 'lib/rbi/rbs_printer.rb', line 279

def visit_attr(node)
  print_blank_line_before(node)

  node.names.each do |name|
    visit_all(node.comments)
    print_loc(node)
    printt
    unless in_visibility_group || node.visibility.public? || node.visibility.protected?
      print(node.visibility.visibility.name)
      print(" ")
    end
    case node
    when AttrAccessor
      print("attr_accessor")
    when AttrReader
      print("attr_reader")
    when AttrWriter
      print("attr_writer")
    end
    print(" #{name}")
    first_sig, *_rest = node.sigs # discard remaining signatures
    if first_sig
      print(": ")
      print_attr_sig(node, first_sig)
    else
      print(": untyped")
    end
    printn
  end
end

#visit_attr_accessor(node) ⇒ Object

Signature:

  • (AttrAccessor node) -> void



262
263
264
# File 'lib/rbi/rbs_printer.rb', line 262

def visit_attr_accessor(node)
  visit_attr(node)
end

#visit_attr_reader(node) ⇒ Object

Signature:

  • (AttrReader node) -> void



268
269
270
# File 'lib/rbi/rbs_printer.rb', line 268

def visit_attr_reader(node)
  visit_attr(node)
end

#visit_attr_writer(node) ⇒ Object

Signature:

  • (AttrWriter node) -> void



274
275
276
# File 'lib/rbi/rbs_printer.rb', line 274

def visit_attr_writer(node)
  visit_attr(node)
end

#visit_blank_line(node) ⇒ Object

Signature:

  • (BlankLine node) -> void



133
134
135
# File 'lib/rbi/rbs_printer.rb', line 133

def visit_blank_line(node)
  printn
end

#visit_block_param(node) ⇒ Object

Signature:

  • (BlockParam node) -> void



625
626
627
# File 'lib/rbi/rbs_printer.rb', line 625

def visit_block_param(node)
  print("{ (*untyped) -> untyped } ")
end

#visit_class(node) ⇒ Object

Signature:

  • (Class node) -> void



153
154
155
# File 'lib/rbi/rbs_printer.rb', line 153

def visit_class(node)
  visit_scope(node)
end

#visit_comment(node) ⇒ Object

Signature:

  • (Comment node) -> void



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rbi/rbs_printer.rb', line 116

def visit_comment(node)
  lines = node.text.lines

  if lines.empty?
    printl("#")
  end

  lines.each do |line|
    text = line.rstrip
    printt("#")
    print(" #{text}") unless text.empty?
    printn
  end
end

#visit_conflict_tree(node) ⇒ Object

Signature:

  • (ConflictTree node) -> void



829
830
831
832
833
834
835
# File 'lib/rbi/rbs_printer.rb', line 829

def visit_conflict_tree(node)
  printl("<<<<<<< #{node.left_name}")
  visit(node.left)
  printl("=======")
  visit(node.right)
  printl(">>>>>>> #{node.right_name}")
end

#visit_const(node) ⇒ Object

Signature:

  • (Const node) -> void



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rbi/rbs_printer.rb', line 246

def visit_const(node)
  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  type = parse_t_let(node.value)
  if type
    type = parse_type(type)
    printl("#{node.name}: #{type.rbs_string}")
  else
    printl("#{node.name}: untyped")
  end
end

#visit_extend(node) ⇒ Object

Signature:

  • (Extend node) -> void



637
638
639
# File 'lib/rbi/rbs_printer.rb', line 637

def visit_extend(node)
  visit_mixin(node)
end

#visit_file(file) ⇒ Object

Signature:

  • (File file) -> void



103
104
105
106
107
108
109
110
111
112
# File 'lib/rbi/rbs_printer.rb', line 103

def visit_file(file)
  unless file.comments.empty?
    visit_all(file.comments)
  end

  unless file.root.empty? && file.root.comments.empty?
    printn unless file.comments.empty?
    visit(file.root)
  end
end

#visit_group(node) ⇒ Object

Signature:

  • (Group node) -> void



802
803
804
805
# File 'lib/rbi/rbs_printer.rb', line 802

def visit_group(node)
  printn unless previous_node.nil?
  visit_all(node.nodes)
end

#visit_helper(node) ⇒ Object

Signature:

  • (Helper node) -> void



790
791
792
# File 'lib/rbi/rbs_printer.rb', line 790

def visit_helper(node)
  # no-op, we already show them in the scope header
end

#visit_include(node) ⇒ Object

Signature:

  • (Include node) -> void



631
632
633
# File 'lib/rbi/rbs_printer.rb', line 631

def visit_include(node)
  visit_mixin(node)
end

#visit_kw_arg(node) ⇒ Object

Signature:

  • (KwArg node) -> void



699
700
701
# File 'lib/rbi/rbs_printer.rb', line 699

def visit_kw_arg(node)
  # no-op
end

#visit_kw_opt_param(node) ⇒ Object

Signature:

  • (KwOptParam node) -> void



612
613
614
# File 'lib/rbi/rbs_printer.rb', line 612

def visit_kw_opt_param(node)
  print("?#{node.name}: untyped")
end

#visit_kw_param(node) ⇒ Object

Signature:

  • (KwParam node) -> void



606
607
608
# File 'lib/rbi/rbs_printer.rb', line 606

def visit_kw_param(node)
  print("#{node.name}: untyped")
end

#visit_kw_rest_param(node) ⇒ Object

Signature:

  • (KwRestParam node) -> void



618
619
620
621
# File 'lib/rbi/rbs_printer.rb', line 618

def visit_kw_rest_param(node)
  print("**untyped")
  print(" #{node.name}") unless node.anonymous?
end

#visit_method(node) ⇒ Object

Signature:

  • (Method node) -> void



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
# File 'lib/rbi/rbs_printer.rb', line 334

def visit_method(node)
  print_blank_line_before(node)
  visit_all(node.comments)

  if node.sigs.any?(&:without_runtime)
    printl("# @without_runtime")
  end

  if node.sigs.any?(&:is_abstract)
    printl("# @abstract")
  end

  if node.sigs.any?(&:is_override)
    if node.sigs.any?(&:allow_incompatible_override)
      printl("# @override(allow_incompatible: true)")
    elsif node.sigs.any?(&:allow_incompatible_override_visibility)
      printl("# @override(allow_incompatible: :visibility)")
    else
      printl("# @override")
    end
  end

  if node.sigs.any?(&:is_overridable)
    printl("# @overridable")
  end

  if node.sigs.any?(&:is_final)
    printl("# @final")
  end

  print_loc(node)
  printt
  unless in_visibility_group || node.visibility.public?
    print(node.visibility.visibility.name)
    print(" ")
  end
  print("def ")
  print("self.") if node.is_singleton
  print(node.name)
  sigs = node.sigs
  print(": ")
  if sigs.any?
    first, *rest = sigs
    print_method_sig(
      node,
      first, #: as !nil
    )
    if rest.any?
      spaces = node.name.size + 4
      rest.each do |sig|
        printn
        printt
        print("#{" " * spaces}| ")
        print_method_sig(node, sig)
      end
    end
  else
    if node.params.any?
      params = node.params.grep_v(BlockParam)
      block = node.params.find { |param| param.is_a?(BlockParam) }

      print("(")
      params.each_with_index do |param, index|
        print(", ") if index > 0
        visit(param)
      end
      print(") ")
      visit(block)
    end
    print("-> untyped")
  end
  printn
end

#visit_mixes_in_class_methods(node) ⇒ Object

Signature:

  • (MixesInClassMethods node) -> void



796
797
798
# File 'lib/rbi/rbs_printer.rb', line 796

def visit_mixes_in_class_methods(node)
  visit_mixin(node)
end

#visit_mixin(node) ⇒ Object

Signature:

  • (Mixin node) -> void



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/rbi/rbs_printer.rb', line 642

def visit_mixin(node)
  return if node.is_a?(MixesInClassMethods) # no-op, `mixes_in_class_methods` is not supported in RBS

  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  case node
  when Include
    printt("include")
  when Extend
    printt("extend")
  end
  printn(" #{node.names.join(", ")}")
end

#visit_module(node) ⇒ Object

Signature:

  • (Module node) -> void



147
148
149
# File 'lib/rbi/rbs_printer.rb', line 147

def visit_module(node)
  visit_scope(node)
end

#visit_opt_param(node) ⇒ Object

Signature:

  • (OptParam node) -> void



589
590
591
592
593
594
595
# File 'lib/rbi/rbs_printer.rb', line 589

def visit_opt_param(node)
  if @positional_names
    print("?untyped #{node.name}")
  else
    print("?untyped")
  end
end

#visit_private(node) ⇒ Object

Signature:

  • (Private node) -> void



672
673
674
# File 'lib/rbi/rbs_printer.rb', line 672

def visit_private(node)
  visit_visibility(node)
end

#visit_protected(node) ⇒ Object

Signature:

  • (Protected node) -> void



666
667
668
# File 'lib/rbi/rbs_printer.rb', line 666

def visit_protected(node)
  # no-op, `protected` is not supported in RBS
end

#visit_public(node) ⇒ Object

Signature:

  • (Public node) -> void



660
661
662
# File 'lib/rbi/rbs_printer.rb', line 660

def visit_public(node)
  visit_visibility(node)
end

#visit_req_param(node) ⇒ Object

Signature:

  • (ReqParam node) -> void



579
580
581
582
583
584
585
# File 'lib/rbi/rbs_printer.rb', line 579

def visit_req_param(node)
  if @positional_names
    print("untyped #{node.name}")
  else
    print("untyped")
  end
end

#visit_requires_ancestor(node) ⇒ Object

Signature:

  • (RequiresAncestor node) -> void



823
824
825
# File 'lib/rbi/rbs_printer.rb', line 823

def visit_requires_ancestor(node)
  # no-op, we already show them in the scope header
end

#visit_rest_param(node) ⇒ Object

Signature:

  • (RestParam node) -> void



599
600
601
602
# File 'lib/rbi/rbs_printer.rb', line 599

def visit_rest_param(node)
  print("*untyped")
  print(" #{node.name}") if @positional_names && !node.anonymous?
end

#visit_scope(node) ⇒ Object

Signature:

  • (Scope node) -> void



170
171
172
173
174
175
176
177
# File 'lib/rbi/rbs_printer.rb', line 170

def visit_scope(node)
  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  visit_scope_header(node)
  visit_scope_body(node)
end

#visit_scope_body(node) ⇒ Object

Signature:

  • (Scope node) -> void



233
234
235
236
237
238
239
240
241
242
# File 'lib/rbi/rbs_printer.rb', line 233

def visit_scope_body(node)
  unless node.empty?
    indent
    visit_all(node.nodes)
    dedent
  end
  if !node.is_a?(Struct) || !node.empty?
    printl("end")
  end
end

#visit_scope_conflict(node) ⇒ Object

Signature:

  • (ScopeConflict node) -> void



839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/rbi/rbs_printer.rb', line 839

def visit_scope_conflict(node)
  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  printl("<<<<<<< #{node.left_name}")
  visit_scope_header(node.left)
  printl("=======")
  visit_scope_header(node.right)
  printl(">>>>>>> #{node.right_name}")
  visit_scope_body(node.left)
end

#visit_scope_header(node) ⇒ Object

Signature:

  • (Scope node) -> void



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
# File 'lib/rbi/rbs_printer.rb', line 180

def visit_scope_header(node)
  node.nodes.grep(Helper).each do |helper|
    visit(Comment.new("@#{helper.name}"))
  end

  mixins = node.nodes.grep(MixesInClassMethods)
  if mixins.any?
    visit(Comment.new("@mixes_in_class_methods #{mixins.map(&:names).join(", ")}"))
  end

  ancestors = node.nodes.grep(RequiresAncestor)
  if ancestors.any?
    visit(Comment.new("@requires_ancestor #{ancestors.map(&:name).join(", ")}"))
  end

  case node
  when TStruct
    printt("class #{node.name}")
  when TEnum
    printt("class #{node.name}")
  when Module
    printt("module #{node.name}")
  when Class
    printt("class #{node.name}")
    superclass = node.superclass_name
    print(" < #{superclass}") if superclass
  when Struct
    printt("#{node.name} = ::Struct.new")
    if !node.members.empty? || node.keyword_init
      print("(")
      args = node.members.map { |member| ":#{member}" }
      args << "keyword_init: true" if node.keyword_init
      print(args.join(", "))
      print(")")
    end
  when SingletonClass
    printt("class << self")
  else
    raise Error, "Unhandled node: #{node.class}"
  end

  type_params = node.nodes.grep(TypeMember)
  if type_params.any?
    print("[#{type_params.map(&:name).join(", ")}]")
  end

  if !node.empty? && node.is_a?(Struct)
    print(" do")
  end
  printn
end

#visit_send(node) ⇒ Object

Signature:

  • (Send node) -> void



687
688
689
# File 'lib/rbi/rbs_printer.rb', line 687

def visit_send(node)
  # no-op, arbitrary sends are not supported in RBS
end

#visit_sig(node) ⇒ Object

Signature:

  • (Sig node) -> void



560
561
562
563
564
565
566
567
568
569
570
# File 'lib/rbi/rbs_printer.rb', line 560

def visit_sig(node)
  if node.params
    print("(")
    node.params.each do |param|
      visit(param)
    end
    print(") ")
  end

  print("-> #{parse_type(node.return_type).rbs_string}")
end

#visit_sig_param(node) ⇒ Object

Signature:

  • (SigParam node) -> void



573
574
575
# File 'lib/rbi/rbs_printer.rb', line 573

def visit_sig_param(node)
  print(parse_type(node.type).rbs_string)
end

#visit_singleton_class(node) ⇒ Object

Signature:

  • (SingletonClass node) -> void



165
166
167
# File 'lib/rbi/rbs_printer.rb', line 165

def visit_singleton_class(node)
  visit_scope(node)
end

#visit_struct(node) ⇒ Object

Signature:

  • (Struct node) -> void



159
160
161
# File 'lib/rbi/rbs_printer.rb', line 159

def visit_struct(node)
  visit_scope(node)
end

#visit_tenum(node) ⇒ Object

Signature:

  • (TEnum node) -> void



756
757
758
# File 'lib/rbi/rbs_printer.rb', line 756

def visit_tenum(node)
  visit_scope(node)
end

#visit_tenum_block(node) ⇒ Object

Signature:

  • (TEnumBlock node) -> void



762
763
764
# File 'lib/rbi/rbs_printer.rb', line 762

def visit_tenum_block(node)
  visit_all(node.nodes)
end

#visit_tenum_value(node) ⇒ Object

Signature:

  • (TEnumValue node) -> void



768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/rbi/rbs_printer.rb', line 768

def visit_tenum_value(node)
  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  t_enum = node.parent_scope&.parent_scope

  if t_enum.is_a?(TEnum)
    printl("#{node.name}: #{t_enum.name}")
  else
    printl("#{node.name}: untyped")
  end
end

#visit_tree(node) ⇒ Object

Signature:

  • (Tree node) -> void



139
140
141
142
143
# File 'lib/rbi/rbs_printer.rb', line 139

def visit_tree(node)
  visit_all(node.comments)
  printn if !node.comments.empty? && !node.empty?
  visit_all(node.nodes)
end

#visit_tstruct(node) ⇒ Object

Signature:

  • (TStruct node) -> void



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/rbi/rbs_printer.rb', line 705

def visit_tstruct(node)
  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  visit_scope_header(node)
  nodes = node.nodes.dup

  sig = Sig.new
  init = Method.new("initialize", sigs: [sig])
  last_field = -1
  nodes.each_with_index do |child, i|
    case child
    when TStructField
      default = child.default
      init << if default
        KwOptParam.new(child.name, default)
      else
        KwParam.new(child.name)
      end
      sig << SigParam.new(child.name, child.type)
      last_field = i
    end
  end
  nodes.insert(last_field + 1, init)

  indent
  visit_all(nodes)
  dedent

  printl("end")
end

#visit_tstruct_const(node) ⇒ Object

Signature:

  • (TStructConst node) -> void



740
741
742
743
744
# File 'lib/rbi/rbs_printer.rb', line 740

def visit_tstruct_const(node)
  # `T::Struct.const` is not supported in RBS instead we generate an attribute reader
  accessor = AttrReader.new(node.name.to_sym, comments: node.comments, sigs: [Sig.new(return_type: node.type)])
  visit_attr_reader(accessor)
end

#visit_tstruct_prop(node) ⇒ Object

Signature:

  • (TStructProp node) -> void



748
749
750
751
752
# File 'lib/rbi/rbs_printer.rb', line 748

def visit_tstruct_prop(node)
  # `T::Struct.prop` is not supported in RBS instead we generate an attribute accessor
  accessor = AttrAccessor.new(node.name.to_sym, comments: node.comments, sigs: [Sig.new(return_type: node.type)])
  visit_attr_accessor(accessor)
end

#visit_type_member(node) ⇒ Object

Signature:

  • (TypeMember node) -> void



784
785
786
# File 'lib/rbi/rbs_printer.rb', line 784

def visit_type_member(node)
  # no-op, we already show them in the scope header
end

#visit_visibility(node) ⇒ Object

Signature:

  • (Visibility node) -> void



677
678
679
680
681
682
683
# File 'lib/rbi/rbs_printer.rb', line 677

def visit_visibility(node)
  print_blank_line_before(node)
  print_loc(node)
  visit_all(node.comments)

  printl(node.visibility.name)
end

#visit_visibility_group(node) ⇒ Object

Signature:

  • (VisibilityGroup node) -> void



809
810
811
812
813
814
815
816
817
818
819
# File 'lib/rbi/rbs_printer.rb', line 809

def visit_visibility_group(node)
  self.in_visibility_group = true
  if node.visibility.public?
    printn unless previous_node.nil?
  else
    visit(node.visibility)
    printn
  end
  visit_all(node.nodes)
  self.in_visibility_group = false
end