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.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/lib/rbi/index.rb +3 -8
- data/lib/rbi/loc.rb +11 -0
- data/lib/rbi/model.rb +19 -41
- data/lib/rbi/parser.rb +119 -71
- data/lib/rbi/printer.rb +6 -2
- data/lib/rbi/rbs/type_translator.rb +26 -30
- data/lib/rbi/rbs_printer.rb +103 -5
- data/lib/rbi/rewriters/attr_to_methods.rb +2 -1
- data/lib/rbi/rewriters/group_nodes.rb +16 -16
- data/lib/rbi/rewriters/merge_trees.rb +20 -11
- data/lib/rbi/rewriters/remove_known_definitions.rb +9 -3
- data/lib/rbi/rewriters/sort_nodes.rb +1 -1
- data/lib/rbi/type.rb +336 -100
- data/lib/rbi/type_parser.rb +7 -5
- data/lib/rbi/version.rb +1 -1
- data/lib/rbi/visitor.rb +1 -4
- data/lib/rbi.rb +0 -1
- data/rbi/rbi.rbi +258 -88
- metadata +3 -17
data/lib/rbi/rbs_printer.rb
CHANGED
@@ -17,8 +17,17 @@ module RBI
|
|
17
17
|
#: bool
|
18
18
|
attr_accessor :positional_names
|
19
19
|
|
20
|
-
#:
|
21
|
-
|
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
|
@@ -91,22 +91,22 @@ module RBI
|
|
91
91
|
@kind = kind
|
92
92
|
end
|
93
93
|
|
94
|
-
class Kind
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
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
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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
|
127
|
-
|
128
|
-
|
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
|