Skip to content

Commit e7964c9

Browse files
committed
Add RBS_WRITTEN_ANNOTATION prefix to multiline annotations
1 parent 75ff215 commit e7964c9

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

lib/spoom/rbs.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,20 @@ def initialize(string, location)
6969
end
7070

7171
class Annotation < Comment; end
72-
class Signature < Comment; end
72+
73+
class Signature < Comment
74+
# Locations of the `#|` continuation comment lines that make up a multiline signature,
75+
# in addition to the `#:` line tracked by `location`.
76+
#: Array[Prism::Location]
77+
attr_reader :continuation_locations
78+
79+
#: (String, Prism::Location, ?continuation_locations: Array[Prism::Location]) -> void
80+
def initialize(string, location, continuation_locations: [])
81+
super(string, location)
82+
@continuation_locations = continuation_locations
83+
end
84+
end
85+
7386
class TypeAlias < Comment; end
7487

7588
module ExtractRBSComments
@@ -99,16 +112,18 @@ def node_rbs_comments(node)
99112
elsif string.start_with?("#: ")
100113
string = string.delete_prefix("#:").strip
101114
location = comment.location
115+
continuation_locations = [] #: Array[Prism::Location]
102116

103117
continuation_comments.reverse_each do |continuation_comment|
104118
string = "#{string}#{continuation_comment.slice.delete_prefix("#|")}"
105119
location = location.join(continuation_comment.location)
120+
continuation_locations << continuation_comment.location
106121
end
107122
continuation_comments.clear
108123

109124
next if string.start_with?("type ")
110125

111-
res.signatures.prepend(Signature.new(string, location))
126+
res.signatures.prepend(Signature.new(string, location, continuation_locations:))
112127
elsif string.start_with?("#|")
113128
continuation_comments << comment
114129
end

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs/line_matching_translator.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class LineMatchingTranslator < BaseTranslator
1616
#: (Spoom::RBS::Signature) -> void
1717
def rewrite_discarded_overload(signature)
1818
@rewriter << Source::Insert.new(signature.location.start_offset + 1, " RBS_DISCARDED_OVERLOAD")
19+
20+
signature.continuation_locations.each do |location|
21+
@rewriter << Source::Insert.new(location.start_offset + 1, " RBS_DISCARDED_OVERLOAD:")
22+
end
1923
end
2024

2125
# @override
@@ -51,6 +55,15 @@ def rewrite_type_params_signature(signature, type_params:)
5155
signature.location.start_offset + 1, # the `#:` prefix
5256
"# RBS_WRITTEN_ANNOTATION:",
5357
)
58+
59+
# Rewrite each continuation line `#| B]` into `# RBS_WRITTEN_ANNOTATION: B]`
60+
signature.continuation_locations.each do |location|
61+
@rewriter << Source::Replace.new(
62+
location.start_offset,
63+
location.start_offset + 1, # the `#|` continuation prefix
64+
"# RBS_WRITTEN_ANNOTATION:",
65+
)
66+
end
5467
end
5568

5669
# @override

rbi/spoom.rbi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2613,7 +2613,20 @@ module Spoom::RBS::ExtractRBSComments
26132613
def node_rbs_comments(node); end
26142614
end
26152615

2616-
class Spoom::RBS::Signature < ::Spoom::RBS::Comment; end
2616+
class Spoom::RBS::Signature < ::Spoom::RBS::Comment
2617+
sig do
2618+
params(
2619+
string: ::String,
2620+
location: ::Prism::Location,
2621+
continuation_locations: T::Array[::Prism::Location]
2622+
).void
2623+
end
2624+
def initialize(string, location, continuation_locations: T.unsafe(nil)); end
2625+
2626+
sig { returns(T::Array[::Prism::Location]) }
2627+
def continuation_locations; end
2628+
end
2629+
26172630
class Spoom::RBS::TypeAlias < ::Spoom::RBS::Comment; end
26182631
Spoom::SPOOM_PATH = T.let(T.unsafe(nil), String)
26192632
module Spoom::Sorbet; end

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,63 @@ def bar(a, b); end
10501050
)
10511051
end
10521052

1053+
def test_translate_multiline_type_parameters
1054+
assert_rewrites_rbs(
1055+
from: <<~RUBY,
1056+
#: [A,
1057+
#| B]
1058+
class Foo
1059+
end
1060+
RUBY
1061+
to_pretty_format_for_humans: <<~RUBY,
1062+
class Foo
1063+
extend T::Generic
1064+
1065+
A = type_member
1066+
B = type_member
1067+
end
1068+
RUBY
1069+
to_line_matched_format_for_machines: <<~RUBY,
1070+
# RBS_WRITTEN_ANNOTATION: [A,
1071+
# RBS_WRITTEN_ANNOTATION: B]
1072+
class Foo; extend T::Generic; A = type_member; B = type_member
1073+
end
1074+
RUBY
1075+
)
1076+
end
1077+
1078+
def test_translate_last_multiline_overloads_marks_entire_discarded_signature
1079+
assert_rewrites_rbs(
1080+
from: <<~RUBY,
1081+
class Foo
1082+
#: (
1083+
#| Integer,
1084+
#| String
1085+
#| ) -> void
1086+
#: (String) -> void
1087+
def foo(x); end
1088+
end
1089+
RUBY
1090+
to_pretty_format_for_humans: <<~RUBY,
1091+
class Foo
1092+
sig { params(x: String).void }
1093+
def foo(x); end
1094+
end
1095+
RUBY
1096+
to_line_matched_format_for_machines: <<~RUBY,
1097+
class Foo
1098+
# RBS_DISCARDED_OVERLOAD: (
1099+
# RBS_DISCARDED_OVERLOAD:| Integer,
1100+
# RBS_DISCARDED_OVERLOAD:| String
1101+
# RBS_DISCARDED_OVERLOAD:| ) -> void
1102+
sig { params(x: String).void }
1103+
def foo(x); end
1104+
end
1105+
RUBY
1106+
overloads_strategy: :translate_last,
1107+
)
1108+
end
1109+
10531110
def test_translate_overloads_raise
10541111
contents = <<~RB
10551112
class Foo

0 commit comments

Comments
 (0)