rbi 0.3.3 → 0.3.6
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 +14 -38
- data/lib/rbi/parser.rb +101 -71
- data/lib/rbi/rbs/type_translator.rb +26 -30
- data/lib/rbi/rbs_printer.rb +99 -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 +179 -494
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80df141e229452edf3fb51dc8933c130ce2a2f972ee6906be79680d79f33a720
|
4
|
+
data.tar.gz: '099a5b5fb43ccd50cf262bcdbc0d8fde6cd0aa14983851c9de14f488def2a56b'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 791d993759d3e237a52d34be622c23eec92d1adb26b267aa47b3aa9e7ce1767b96fff75e02d895493fb63b84fe3f15cb1c37f880db1698e5a8b9e17cba77503e
|
7
|
+
data.tar.gz: cdf11ef801d7ac8a1329b39e095c4895d006ae46ad800d50d1d2cb52aa9c41fdb704557a8a660376b53e4bdb86a076bacde0a5a2c33d0031478dda36850297b1
|
data/Gemfile
CHANGED
@@ -9,8 +9,8 @@ group(:development, :test) do
|
|
9
9
|
gem("byebug")
|
10
10
|
gem("minitest")
|
11
11
|
gem("minitest-reporters")
|
12
|
-
gem("rake", "~> 13.
|
13
|
-
gem("rubocop", "~> 1.
|
12
|
+
gem("rake", "~> 13.3")
|
13
|
+
gem("rubocop", "~> 1.76", require: false)
|
14
14
|
gem("rubocop-shopify", require: false)
|
15
15
|
gem("rubocop-sorbet", require: false)
|
16
16
|
gem("sorbet", ">= 0.5.9204", require: false)
|
data/lib/rbi/index.rb
CHANGED
@@ -3,8 +3,6 @@
|
|
3
3
|
|
4
4
|
module RBI
|
5
5
|
class Index < Visitor
|
6
|
-
include T::Enumerable
|
7
|
-
|
8
6
|
class << self
|
9
7
|
#: (*Node node) -> Index
|
10
8
|
def index(*node)
|
@@ -67,17 +65,14 @@ module RBI
|
|
67
65
|
end
|
68
66
|
|
69
67
|
# A Node that can be referred to by a unique ID inside an index
|
68
|
+
# @interface
|
70
69
|
module Indexable
|
71
|
-
extend T::Sig
|
72
|
-
extend T::Helpers
|
73
|
-
|
74
|
-
interface!
|
75
|
-
|
76
70
|
# Unique IDs that refer to this node.
|
77
71
|
#
|
78
72
|
# Some nodes can have multiple ids, for example an attribute accessor matches the ID of the
|
79
73
|
# getter and the setter.
|
80
|
-
|
74
|
+
# @abstract
|
75
|
+
#: -> Array[String]
|
81
76
|
def index_ids; end
|
82
77
|
end
|
83
78
|
|
data/lib/rbi/loc.rb
CHANGED
@@ -31,6 +31,17 @@ module RBI
|
|
31
31
|
@end_column = end_column
|
32
32
|
end
|
33
33
|
|
34
|
+
#: (Loc) -> Loc
|
35
|
+
def join(other)
|
36
|
+
Loc.new(
|
37
|
+
file: file,
|
38
|
+
begin_line: begin_line,
|
39
|
+
begin_column: begin_column,
|
40
|
+
end_line: other.end_line,
|
41
|
+
end_column: other.end_column,
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
34
45
|
#: -> String
|
35
46
|
def to_s
|
36
47
|
if end_line && end_column
|
data/lib/rbi/model.rb
CHANGED
@@ -4,11 +4,8 @@
|
|
4
4
|
module RBI
|
5
5
|
class ReplaceNodeError < Error; end
|
6
6
|
|
7
|
+
# @abstract
|
7
8
|
class Node
|
8
|
-
extend T::Helpers
|
9
|
-
|
10
|
-
abstract!
|
11
|
-
|
12
9
|
#: Tree?
|
13
10
|
attr_accessor :parent_tree
|
14
11
|
|
@@ -87,11 +84,8 @@ module RBI
|
|
87
84
|
end
|
88
85
|
end
|
89
86
|
|
87
|
+
# @abstract
|
90
88
|
class NodeWithComments < Node
|
91
|
-
extend T::Helpers
|
92
|
-
|
93
|
-
abstract!
|
94
|
-
|
95
89
|
#: Array[Comment]
|
96
90
|
attr_accessor :comments
|
97
91
|
|
@@ -165,13 +159,10 @@ module RBI
|
|
165
159
|
|
166
160
|
# Scopes
|
167
161
|
|
162
|
+
# @abstract
|
168
163
|
class Scope < Tree
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
abstract!
|
173
|
-
|
174
|
-
sig { abstract.returns(String) }
|
164
|
+
# @abstract
|
165
|
+
#: -> String
|
175
166
|
def fully_qualified_name; end
|
176
167
|
|
177
168
|
# @override
|
@@ -297,12 +288,8 @@ module RBI
|
|
297
288
|
|
298
289
|
# Attributes
|
299
290
|
|
291
|
+
# @abstract
|
300
292
|
class Attr < NodeWithComments
|
301
|
-
extend T::Sig
|
302
|
-
extend T::Helpers
|
303
|
-
|
304
|
-
abstract!
|
305
|
-
|
306
293
|
#: Array[Symbol]
|
307
294
|
attr_reader :names
|
308
295
|
|
@@ -320,7 +307,8 @@ module RBI
|
|
320
307
|
@sigs = sigs
|
321
308
|
end
|
322
309
|
|
323
|
-
|
310
|
+
# @abstract
|
311
|
+
#: -> Array[String]
|
324
312
|
def fully_qualified_names; end
|
325
313
|
end
|
326
314
|
|
@@ -510,11 +498,8 @@ module RBI
|
|
510
498
|
end
|
511
499
|
end
|
512
500
|
|
501
|
+
# @abstract
|
513
502
|
class Param < NodeWithComments
|
514
|
-
extend T::Helpers
|
515
|
-
|
516
|
-
abstract!
|
517
|
-
|
518
503
|
#: String
|
519
504
|
attr_reader :name
|
520
505
|
|
@@ -662,11 +647,8 @@ module RBI
|
|
662
647
|
|
663
648
|
# Mixins
|
664
649
|
|
650
|
+
# @abstract
|
665
651
|
class Mixin < NodeWithComments
|
666
|
-
extend T::Helpers
|
667
|
-
|
668
|
-
abstract!
|
669
|
-
|
670
652
|
#: Array[String]
|
671
653
|
attr_reader :names
|
672
654
|
|
@@ -707,11 +689,8 @@ module RBI
|
|
707
689
|
|
708
690
|
# Visibility
|
709
691
|
|
692
|
+
# @abstract
|
710
693
|
class Visibility < NodeWithComments
|
711
|
-
extend T::Helpers
|
712
|
-
|
713
|
-
abstract!
|
714
|
-
|
715
694
|
#: Symbol
|
716
695
|
attr_reader :visibility
|
717
696
|
|
@@ -942,12 +921,8 @@ module RBI
|
|
942
921
|
end
|
943
922
|
end
|
944
923
|
|
924
|
+
# @abstract
|
945
925
|
class TStructField < NodeWithComments
|
946
|
-
extend T::Sig
|
947
|
-
extend T::Helpers
|
948
|
-
|
949
|
-
abstract!
|
950
|
-
|
951
926
|
#: String
|
952
927
|
attr_accessor :name
|
953
928
|
|
@@ -965,7 +940,8 @@ module RBI
|
|
965
940
|
@default = default
|
966
941
|
end
|
967
942
|
|
968
|
-
|
943
|
+
# @abstract
|
944
|
+
#: -> Array[String]
|
969
945
|
def fully_qualified_names; end
|
970
946
|
end
|
971
947
|
|
data/lib/rbi/parser.rb
CHANGED
@@ -380,7 +380,7 @@ module RBI
|
|
380
380
|
comments = detach_comments_from_sigs(sigs) + node_comments(node)
|
381
381
|
|
382
382
|
current_scope << AttrReader.new(
|
383
|
-
*
|
383
|
+
*args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym },
|
384
384
|
sigs: sigs,
|
385
385
|
loc: node_loc(node),
|
386
386
|
comments: comments,
|
@@ -397,7 +397,7 @@ module RBI
|
|
397
397
|
comments = detach_comments_from_sigs(sigs) + node_comments(node)
|
398
398
|
|
399
399
|
current_scope << AttrWriter.new(
|
400
|
-
*
|
400
|
+
*args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym },
|
401
401
|
sigs: sigs,
|
402
402
|
loc: node_loc(node),
|
403
403
|
comments: comments,
|
@@ -414,7 +414,7 @@ module RBI
|
|
414
414
|
comments = detach_comments_from_sigs(sigs) + node_comments(node)
|
415
415
|
|
416
416
|
current_scope << AttrAccessor.new(
|
417
|
-
*
|
417
|
+
*args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym },
|
418
418
|
sigs: sigs,
|
419
419
|
loc: node_loc(node),
|
420
420
|
comments: comments,
|
@@ -443,7 +443,7 @@ module RBI
|
|
443
443
|
end
|
444
444
|
|
445
445
|
current_scope << Extend.new(
|
446
|
-
*
|
446
|
+
*args.arguments.map { |arg| node_string!(arg) },
|
447
447
|
loc: node_loc(node),
|
448
448
|
comments: node_comments(node),
|
449
449
|
)
|
@@ -456,7 +456,7 @@ module RBI
|
|
456
456
|
end
|
457
457
|
|
458
458
|
current_scope << Include.new(
|
459
|
-
*
|
459
|
+
*args.arguments.map { |arg| node_string!(arg) },
|
460
460
|
loc: node_loc(node),
|
461
461
|
comments: node_comments(node),
|
462
462
|
)
|
@@ -469,7 +469,7 @@ module RBI
|
|
469
469
|
end
|
470
470
|
|
471
471
|
current_scope << MixesInClassMethods.new(
|
472
|
-
*
|
472
|
+
*args.arguments.map { |arg| node_string!(arg) },
|
473
473
|
loc: node_loc(node),
|
474
474
|
comments: node_comments(node),
|
475
475
|
)
|
@@ -607,28 +607,66 @@ module RBI
|
|
607
607
|
start_line = node.location.start_line
|
608
608
|
start_line -= 1 unless @comments_by_line.key?(start_line)
|
609
609
|
|
610
|
+
rbs_continuation = [] #: Array[Prism::Comment]
|
611
|
+
|
610
612
|
start_line.downto(1) do |line|
|
611
613
|
comment = @comments_by_line[line]
|
612
614
|
break unless comment
|
613
615
|
|
614
|
-
|
616
|
+
text = comment.location.slice
|
617
|
+
|
618
|
+
# If we find a RBS comment continuation `#|`, we store it until we find the start with `#:`
|
619
|
+
if text.start_with?("#|")
|
620
|
+
rbs_continuation << comment
|
621
|
+
@comments_by_line.delete(line)
|
622
|
+
next
|
623
|
+
end
|
624
|
+
|
625
|
+
loc = Loc.from_prism(@file, comment.location)
|
626
|
+
|
627
|
+
# If we find the start of a RBS comment, we create a new RBSComment
|
628
|
+
# Note that we ignore RDoc directives such as `:nodoc:`
|
629
|
+
# See https://ruby.github.io/rdoc/RDoc/MarkupReference.html#class-RDoc::MarkupReference-label-Directives
|
630
|
+
if text.start_with?("#:") && !(text =~ /^#:[a-z_]+:/)
|
631
|
+
text = text.sub(/^#: ?/, "").rstrip
|
632
|
+
|
633
|
+
# If we found continuation comments, we merge them in reverse order (since we go from bottom to top)
|
634
|
+
rbs_continuation.reverse_each do |rbs_comment|
|
635
|
+
continuation_text = rbs_comment.location.slice.sub(/^#\| ?/, "").strip
|
636
|
+
continuation_loc = Loc.from_prism(@file, rbs_comment.location)
|
637
|
+
loc = loc.join(continuation_loc)
|
638
|
+
text = "#{text}#{continuation_text}"
|
639
|
+
end
|
640
|
+
|
641
|
+
rbs_continuation.clear
|
642
|
+
comments.unshift(RBSComment.new(text, loc: loc))
|
643
|
+
else
|
644
|
+
# If we have unused continuation comments, we should inject them back to not lose them
|
645
|
+
rbs_continuation.each do |rbs_comment|
|
646
|
+
comments.unshift(parse_comment(rbs_comment))
|
647
|
+
end
|
648
|
+
|
649
|
+
rbs_continuation.clear
|
650
|
+
comments.unshift(parse_comment(comment))
|
651
|
+
end
|
652
|
+
|
615
653
|
@comments_by_line.delete(line)
|
616
654
|
end
|
617
655
|
|
656
|
+
# If we have unused continuation comments, we should inject them back to not lose them
|
657
|
+
rbs_continuation.each do |rbs_comment|
|
658
|
+
comments.unshift(parse_comment(rbs_comment))
|
659
|
+
end
|
660
|
+
rbs_continuation.clear
|
661
|
+
|
618
662
|
comments
|
619
663
|
end
|
620
664
|
|
621
665
|
#: (Prism::Comment node) -> Comment
|
622
666
|
def parse_comment(node)
|
667
|
+
text = node.location.slice.sub(/^# ?/, "").rstrip
|
623
668
|
loc = Loc.from_prism(@file, node.location)
|
624
|
-
|
625
|
-
# We also ignore RDoc directives such as `:nodoc:`
|
626
|
-
# See https://ruby.github.io/rdoc/RDoc/MarkupReference.html#class-RDoc::MarkupReference-label-Directives
|
627
|
-
if string.start_with?("#:") && !(string =~ /^#:[a-z_]+:/)
|
628
|
-
RBSComment.new(string.gsub(/^#: ?/, "").rstrip, loc: loc)
|
629
|
-
else
|
630
|
-
Comment.new(string.gsub(/^# ?/, "").rstrip, loc: loc)
|
631
|
-
end
|
669
|
+
Comment.new(text, loc: loc)
|
632
670
|
end
|
633
671
|
|
634
672
|
#: (Prism::Node? node) -> Array[Arg]
|
@@ -659,76 +697,68 @@ module RBI
|
|
659
697
|
|
660
698
|
#: (Prism::Node? node) -> Array[Param]
|
661
699
|
def parse_params(node)
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
node.optionals.each do |param|
|
676
|
-
next unless param.is_a?(Prism::OptionalParameterNode)
|
677
|
-
|
678
|
-
params << OptParam.new(
|
679
|
-
param.name.to_s,
|
680
|
-
node_string!(param.value),
|
681
|
-
loc: node_loc(param),
|
682
|
-
comments: node_comments(param),
|
683
|
-
)
|
684
|
-
end
|
685
|
-
|
686
|
-
rest = node.rest
|
687
|
-
if rest.is_a?(Prism::RestParameterNode)
|
688
|
-
params << RestParam.new(
|
689
|
-
rest.name&.to_s || "*args",
|
690
|
-
loc: node_loc(rest),
|
691
|
-
comments: node_comments(rest),
|
692
|
-
)
|
693
|
-
end
|
694
|
-
|
695
|
-
node.keywords.each do |param|
|
700
|
+
return [] unless node.is_a?(Prism::ParametersNode)
|
701
|
+
|
702
|
+
node_params = [
|
703
|
+
*node.requireds,
|
704
|
+
*node.optionals,
|
705
|
+
*node.rest,
|
706
|
+
*node.posts,
|
707
|
+
*node.keywords,
|
708
|
+
*node.keyword_rest,
|
709
|
+
*node.block,
|
710
|
+
].flatten
|
711
|
+
|
712
|
+
node_params.map do |param|
|
696
713
|
case param
|
714
|
+
when Prism::RequiredParameterNode
|
715
|
+
ReqParam.new(
|
716
|
+
param.name.to_s,
|
717
|
+
loc: node_loc(param),
|
718
|
+
comments: node_comments(param),
|
719
|
+
)
|
720
|
+
when Prism::OptionalParameterNode
|
721
|
+
OptParam.new(
|
722
|
+
param.name.to_s,
|
723
|
+
node_string!(param.value),
|
724
|
+
loc: node_loc(param),
|
725
|
+
comments: node_comments(param),
|
726
|
+
)
|
727
|
+
when Prism::RestParameterNode
|
728
|
+
RestParam.new(
|
729
|
+
param.name&.to_s || "*args",
|
730
|
+
loc: node_loc(param),
|
731
|
+
comments: node_comments(param),
|
732
|
+
)
|
697
733
|
when Prism::RequiredKeywordParameterNode
|
698
|
-
|
734
|
+
KwParam.new(
|
699
735
|
param.name.to_s.delete_suffix(":"),
|
700
736
|
loc: node_loc(param),
|
701
737
|
comments: node_comments(param),
|
702
738
|
)
|
703
739
|
when Prism::OptionalKeywordParameterNode
|
704
|
-
|
740
|
+
KwOptParam.new(
|
705
741
|
param.name.to_s.delete_suffix(":"),
|
706
742
|
node_string!(param.value),
|
707
743
|
loc: node_loc(param),
|
708
744
|
comments: node_comments(param),
|
709
745
|
)
|
746
|
+
when Prism::KeywordRestParameterNode
|
747
|
+
KwRestParam.new(
|
748
|
+
param.name&.to_s || "**kwargs",
|
749
|
+
loc: node_loc(param),
|
750
|
+
comments: node_comments(param),
|
751
|
+
)
|
752
|
+
when Prism::BlockParameterNode
|
753
|
+
BlockParam.new(
|
754
|
+
param.name&.to_s || "&block",
|
755
|
+
loc: node_loc(param),
|
756
|
+
comments: node_comments(param),
|
757
|
+
)
|
758
|
+
else
|
759
|
+
raise ParseError.new("Unexpected parameter node `#{param.class}`", node_loc(param))
|
710
760
|
end
|
711
761
|
end
|
712
|
-
|
713
|
-
rest_kw = node.keyword_rest
|
714
|
-
if rest_kw.is_a?(Prism::KeywordRestParameterNode)
|
715
|
-
params << KwRestParam.new(
|
716
|
-
rest_kw.name&.to_s || "**kwargs",
|
717
|
-
loc: node_loc(rest_kw),
|
718
|
-
comments: node_comments(rest_kw),
|
719
|
-
)
|
720
|
-
end
|
721
|
-
|
722
|
-
block = node.block
|
723
|
-
if block.is_a?(Prism::BlockParameterNode)
|
724
|
-
params << BlockParam.new(
|
725
|
-
block.name&.to_s || "&block",
|
726
|
-
loc: node_loc(block),
|
727
|
-
comments: node_comments(block),
|
728
|
-
)
|
729
|
-
end
|
730
|
-
|
731
|
-
params
|
732
762
|
end
|
733
763
|
|
734
764
|
#: (Prism::CallNode node) -> Sig
|
@@ -5,35 +5,31 @@ module RBI
|
|
5
5
|
module RBS
|
6
6
|
class TypeTranslator
|
7
7
|
class << self
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
)
|
34
|
-
end
|
35
|
-
|
36
|
-
#: (NodeType) -> Type
|
8
|
+
#: (
|
9
|
+
#| ::RBS::Types::Alias |
|
10
|
+
#| ::RBS::Types::Bases::Any |
|
11
|
+
#| ::RBS::Types::Bases::Bool |
|
12
|
+
#| ::RBS::Types::Bases::Bottom |
|
13
|
+
#| ::RBS::Types::Bases::Class |
|
14
|
+
#| ::RBS::Types::Bases::Instance |
|
15
|
+
#| ::RBS::Types::Bases::Nil |
|
16
|
+
#| ::RBS::Types::Bases::Self |
|
17
|
+
#| ::RBS::Types::Bases::Top |
|
18
|
+
#| ::RBS::Types::Bases::Void |
|
19
|
+
#| ::RBS::Types::ClassSingleton |
|
20
|
+
#| ::RBS::Types::ClassInstance |
|
21
|
+
#| ::RBS::Types::Function |
|
22
|
+
#| ::RBS::Types::Interface |
|
23
|
+
#| ::RBS::Types::Intersection |
|
24
|
+
#| ::RBS::Types::Literal |
|
25
|
+
#| ::RBS::Types::Optional |
|
26
|
+
#| ::RBS::Types::Proc |
|
27
|
+
#| ::RBS::Types::Record |
|
28
|
+
#| ::RBS::Types::Tuple |
|
29
|
+
#| ::RBS::Types::Union |
|
30
|
+
#| ::RBS::Types::UntypedFunction |
|
31
|
+
#| ::RBS::Types::Variable
|
32
|
+
#| ) -> Type
|
37
33
|
def translate(type)
|
38
34
|
case type
|
39
35
|
when ::RBS::Types::Alias
|
@@ -100,7 +96,7 @@ module RBI
|
|
100
96
|
return Type.simple(type.name.to_s) if type.args.empty?
|
101
97
|
|
102
98
|
type_name = translate_t_generic_type(type.name.to_s)
|
103
|
-
|
99
|
+
Type.generic(type_name, *type.args.map { |arg| translate(arg) })
|
104
100
|
end
|
105
101
|
|
106
102
|
#: (::RBS::Types::Function) -> Type
|
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
|
@@ -386,6 +396,23 @@ module RBI
|
|
386
396
|
|
387
397
|
#: (RBI::Method node, Sig sig) -> void
|
388
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)
|
389
416
|
unless sig.type_params.empty?
|
390
417
|
print("[#{sig.type_params.join(", ")}] ")
|
391
418
|
end
|
@@ -446,6 +473,72 @@ module RBI
|
|
446
473
|
print(" # #{loc}") if loc && print_locs
|
447
474
|
end
|
448
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
|
+
|
449
542
|
#: (Sig node) -> void
|
450
543
|
def visit_sig(node)
|
451
544
|
if node.params
|
@@ -856,7 +949,7 @@ module RBI
|
|
856
949
|
def parse_type(type)
|
857
950
|
return type if type.is_a?(Type)
|
858
951
|
|
859
|
-
Type.parse_string(type)
|
952
|
+
Type.parse_string(type).simplify
|
860
953
|
rescue Type::Error => e
|
861
954
|
raise Error, "Failed to parse type `#{type}` (#{e.message})"
|
862
955
|
end
|
@@ -890,9 +983,10 @@ module RBI
|
|
890
983
|
#: String
|
891
984
|
attr_reader :string
|
892
985
|
|
893
|
-
#: -> void
|
894
|
-
def initialize
|
986
|
+
#: (?max_line_length: Integer?) -> void
|
987
|
+
def initialize(max_line_length: nil)
|
895
988
|
@string = String.new #: String
|
989
|
+
@max_line_length = max_line_length
|
896
990
|
end
|
897
991
|
|
898
992
|
#: (Type node) -> void
|