rbi 0.3.2 → 0.3.5

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.
@@ -17,8 +17,17 @@ module RBI
17
17
  #: bool
18
18
  attr_accessor :positional_names
19
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)
20
+ #: Integer?
21
+ attr_reader :max_line_length
22
+
23
+ #: (
24
+ #| ?out: (IO | StringIO),
25
+ #| ?indent: Integer,
26
+ #| ?print_locs: bool,
27
+ #| ?positional_names: bool,
28
+ #| ?max_line_length: Integer?
29
+ #| ) -> void
30
+ def initialize(out: $stdout, indent: 0, print_locs: false, positional_names: true, max_line_length: nil)
22
31
  super()
23
32
  @out = out
24
33
  @current_indent = indent
@@ -26,6 +35,7 @@ module RBI
26
35
  @in_visibility_group = false #: bool
27
36
  @previous_node = nil #: Node?
28
37
  @positional_names = positional_names #: bool
38
+ @max_line_length = max_line_length #: Integer?
29
39
  end
30
40
 
31
41
  # Printing
@@ -316,6 +326,10 @@ module RBI
316
326
  print_blank_line_before(node)
317
327
  visit_all(node.comments)
318
328
 
329
+ if node.sigs.any?(&:without_runtime)
330
+ printl("# @without_runtime")
331
+ end
332
+
319
333
  if node.sigs.any?(&:is_abstract)
320
334
  printl("# @abstract")
321
335
  end
@@ -382,6 +396,23 @@ module RBI
382
396
 
383
397
  #: (RBI::Method node, Sig sig) -> void
384
398
  def print_method_sig(node, sig)
399
+ old_out = @out
400
+ new_out = StringIO.new
401
+
402
+ @out = new_out
403
+ print_method_sig_inline(node, sig)
404
+ @out = old_out
405
+
406
+ max_line_length = @max_line_length
407
+ if max_line_length.nil? || new_out.string.size <= max_line_length
408
+ print(new_out.string)
409
+ else
410
+ print_method_sig_multiline(node, sig)
411
+ end
412
+ end
413
+
414
+ #: (RBI::Method node, Sig sig) -> void
415
+ def print_method_sig_inline(node, sig)
385
416
  unless sig.type_params.empty?
386
417
  print("[#{sig.type_params.join(", ")}] ")
387
418
  end
@@ -442,6 +473,72 @@ module RBI
442
473
  print(" # #{loc}") if loc && print_locs
443
474
  end
444
475
 
476
+ #: (RBI::Method node, Sig sig) -> void
477
+ def print_method_sig_multiline(node, sig)
478
+ unless sig.type_params.empty?
479
+ print("[#{sig.type_params.join(", ")}] ")
480
+ end
481
+
482
+ block_param = node.params.find { |param| param.is_a?(BlockParam) }
483
+ sig_block_param = sig.params.find { |param| param.name == block_param&.name }
484
+
485
+ sig_params = sig.params.dup
486
+ if block_param
487
+ sig_params.reject! do |param|
488
+ param.name == block_param.name
489
+ end
490
+ end
491
+
492
+ unless sig_params.empty?
493
+ printl("(")
494
+ indent
495
+ sig_params.each_with_index do |param, index|
496
+ printt
497
+ print_sig_param(node, param)
498
+ print(",") if index < sig_params.size - 1
499
+ printn
500
+ end
501
+ dedent
502
+ printt(") ")
503
+ end
504
+ if sig_block_param
505
+ block_type = sig_block_param.type
506
+ block_type = Type.parse_string(block_type) if block_type.is_a?(String)
507
+
508
+ block_is_nilable = false
509
+ if block_type.is_a?(Type::Nilable)
510
+ block_is_nilable = true
511
+ block_type = block_type.type
512
+ end
513
+
514
+ type_string = parse_type(block_type).rbs_string.delete_prefix("^")
515
+
516
+ skip = false
517
+ case block_type
518
+ when Type::Untyped
519
+ type_string = "(?) -> untyped"
520
+ block_is_nilable = true
521
+ when Type::Simple
522
+ type_string = "(?) -> untyped"
523
+ skip = true if block_type.name == "NilClass"
524
+ end
525
+
526
+ if skip
527
+ # no-op, we skip the block definition
528
+ elsif block_is_nilable
529
+ print("?{ #{type_string} } ")
530
+ else
531
+ print("{ #{type_string} } ")
532
+ end
533
+ end
534
+
535
+ type = parse_type(sig.return_type)
536
+ print("-> #{type.rbs_string}")
537
+
538
+ loc = sig.loc
539
+ print(" # #{loc}") if loc && print_locs
540
+ end
541
+
445
542
  #: (Sig node) -> void
446
543
  def visit_sig(node)
447
544
  if node.params
@@ -852,7 +949,7 @@ module RBI
852
949
  def parse_type(type)
853
950
  return type if type.is_a?(Type)
854
951
 
855
- Type.parse_string(type)
952
+ Type.parse_string(type).simplify
856
953
  rescue Type::Error => e
857
954
  raise Error, "Failed to parse type `#{type}` (#{e.message})"
858
955
  end
@@ -886,9 +983,10 @@ module RBI
886
983
  #: String
887
984
  attr_reader :string
888
985
 
889
- #: -> void
890
- def initialize
986
+ #: (?max_line_length: Integer?) -> void
987
+ def initialize(max_line_length: nil)
891
988
  @string = String.new #: String
989
+ @max_line_length = max_line_length
892
990
  end
893
991
 
894
992
  #: (Type node) -> void
@@ -54,7 +54,8 @@ module RBI
54
54
  end
55
55
 
56
56
  class Attr
57
- sig { abstract.returns(T::Array[Method]) }
57
+ # @abstract
58
+ #: -> Array[Method]
58
59
  def convert_to_methods; end
59
60
 
60
61
  private
@@ -91,22 +91,22 @@ module RBI
91
91
  @kind = kind
92
92
  end
93
93
 
94
- class Kind < T::Enum
95
- enums do
96
- Mixins = new
97
- RequiredAncestors = new
98
- Helpers = new
99
- TypeMembers = new
100
- MixesInClassMethods = new
101
- Sends = new
102
- Attrs = new
103
- TStructFields = new
104
- TEnums = new
105
- Inits = new
106
- Methods = new
107
- SingletonClasses = new
108
- Consts = new
109
- end
94
+ class Kind
95
+ Mixins = new #: Kind
96
+ RequiredAncestors = new #: Kind
97
+ Helpers = new #: Kind
98
+ TypeMembers = new #: Kind
99
+ MixesInClassMethods = new #: Kind
100
+ Sends = new #: Kind
101
+ Attrs = new #: Kind
102
+ TStructFields = new #: Kind
103
+ TEnums = new #: Kind
104
+ Inits = new #: Kind
105
+ Methods = new #: Kind
106
+ SingletonClasses = new #: Kind
107
+ Consts = new #: Kind
108
+
109
+ private_class_method(:new)
110
110
  end
111
111
  end
112
112
  end
@@ -37,12 +37,12 @@ module RBI
37
37
  # end
38
38
  # ~~~
39
39
  class Merge
40
- class Keep < ::T::Enum
41
- enums do
42
- NONE = new
43
- LEFT = new
44
- RIGHT = new
45
- end
40
+ class Keep
41
+ NONE = new #: Keep
42
+ LEFT = new #: Keep
43
+ RIGHT = new #: Keep
44
+
45
+ private_class_method(:new)
46
46
  end
47
47
 
48
48
  class << self
@@ -79,11 +79,20 @@ module RBI
79
79
  end
80
80
 
81
81
  # Used for logging / error displaying purpose
82
- class Conflict < T::Struct
83
- const :left, Node
84
- const :right, Node
85
- const :left_name, String
86
- const :right_name, String
82
+ class Conflict
83
+ #: Node
84
+ attr_reader :left, :right
85
+
86
+ #: String
87
+ attr_reader :left_name, :right_name
88
+
89
+ #: (left: Node, right: Node, left_name: String, right_name: String) -> void
90
+ def initialize(left:, right:, left_name:, right_name:)
91
+ @left = left
92
+ @right = right
93
+ @left_name = left_name
94
+ @right_name = right_name
95
+ end
87
96
 
88
97
  #: -> String
89
98
  def to_s
@@ -123,9 +123,15 @@ module RBI
123
123
  @operations << Operation.new(deleted_node: node, duplicate_of: previous)
124
124
  end
125
125
 
126
- class Operation < T::Struct
127
- const :deleted_node, Node
128
- const :duplicate_of, Node
126
+ class Operation
127
+ #: Node
128
+ attr_reader :deleted_node, :duplicate_of
129
+
130
+ #: (deleted_node: Node, duplicate_of: Node) -> void
131
+ def initialize(deleted_node:, duplicate_of:)
132
+ @deleted_node = deleted_node
133
+ @duplicate_of = duplicate_of
134
+ end
129
135
 
130
136
  #: -> String
131
137
  def to_s
@@ -87,7 +87,7 @@ module RBI
87
87
  when Group::Kind::SingletonClasses then 11
88
88
  when Group::Kind::Consts then 12
89
89
  else
90
- T.absurd(kind)
90
+ raise Error, "Unknown group kind: #{kind}"
91
91
  end
92
92
  end
93
93