Skip to content

Commit c73f532

Browse files
committed
Fix incorrect extend T::Helpers for foreign annotations
1 parent a34b3fc commit c73f532

4 files changed

Lines changed: 71 additions & 12 deletions

File tree

lib/spoom/rbs.rb

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,64 @@
44
module Spoom
55
module RBS
66
class Comments
7-
#: Array[Annotations]
7+
#: Array[Annotation]
88
attr_reader :annotations
99

1010
#: Array[Signature]
1111
attr_reader :signatures
1212

1313
#: -> void
1414
def initialize
15-
@annotations = [] #: Array[Annotations]
15+
@annotations = [] #: Array[Annotation]
1616
@signatures = [] #: Array[Signature]
1717
end
1818

1919
#: -> bool
2020
def empty?
2121
@annotations.empty? && @signatures.empty?
2222
end
23+
24+
#: -> Array[Annotation]
25+
def class_annotations
26+
@annotations.select do |annotation|
27+
case annotation.string
28+
when "@abstract"
29+
true
30+
when "@interface"
31+
true
32+
when "@sealed"
33+
true
34+
when "@final"
35+
true
36+
when /^@requires_ancestor: /
37+
true
38+
else
39+
false
40+
end
41+
end
42+
end
43+
44+
#: -> Array[Annotation]
45+
def method_annotations
46+
@annotations.select do |annotation|
47+
case annotation.string
48+
when "@abstract"
49+
true
50+
when "@final"
51+
true
52+
when "@override"
53+
true
54+
when "@override(allow_incompatible: true)"
55+
true
56+
when "@overridable"
57+
true
58+
when "@without_runtime"
59+
true
60+
else
61+
false
62+
end
63+
end
64+
end
2365
end
2466

2567
class Comment
@@ -36,7 +78,7 @@ def initialize(string, location)
3678
end
3779
end
3880

39-
class Annotations < Comment; end
81+
class Annotation < Comment; end
4082
class Signature < Comment; end
4183

4284
module ExtractRBSComments
@@ -54,7 +96,7 @@ def node_rbs_comments(node)
5496

5597
if string.start_with?("# @")
5698
string = string.delete_prefix("#").strip
57-
res.annotations << Annotations.new(string, comment.location)
99+
res.annotations << Annotation.new(string, comment.location)
58100
elsif string.start_with?("#: ")
59101
string = string.delete_prefix("#:").strip
60102
location = comment.location

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def visit_def_node(node)
4848
translator = RBI::RBS::MethodTypeTranslator.new(rbi_node)
4949
translator.visit(method_type)
5050
sig = translator.result
51-
apply_member_annotations(comments.annotations, sig)
51+
apply_member_annotations(comments.method_annotations, sig)
5252

5353
@rewriter << Source::Replace.new(
5454
signature.location.start_offset,
@@ -99,7 +99,7 @@ def visit_attr(node)
9999

100100
sig.return_type = RBI::RBS::TypeTranslator.translate(attr_type)
101101

102-
apply_member_annotations(comments.annotations, sig)
102+
apply_member_annotations(comments.method_annotations, sig)
103103

104104
@rewriter << Source::Replace.new(
105105
signature.location.start_offset,
@@ -127,12 +127,13 @@ def apply_class_annotations(node)
127127
node.expression.location.end_offset
128128
end
129129

130-
if comments.annotations.any?
130+
class_annotations = comments.class_annotations
131+
if class_annotations.any?
131132
unless already_extends?(node, /^(::)?T::Helpers$/)
132133
@rewriter << Source::Insert.new(insert_pos, "\n#{indent}extend T::Helpers\n")
133134
end
134135

135-
comments.annotations.reverse_each do |annotation|
136+
class_annotations.reverse_each do |annotation|
136137
from = adjust_to_line_start(annotation.location.start_offset)
137138
to = adjust_to_line_end(annotation.location.end_offset)
138139

@@ -209,7 +210,7 @@ def apply_class_annotations(node)
209210
end
210211
end
211212

212-
#: (Array[RBS::Annotations], RBI::Sig) -> void
213+
#: (Array[RBS::Annotation], RBI::Sig) -> void
213214
def apply_member_annotations(annotations, sig)
214215
annotations.each do |annotation|
215216
case annotation.string

rbi/spoom.rbi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,7 +2579,7 @@ class Spoom::Printer
25792579
end
25802580

25812581
module Spoom::RBS; end
2582-
class Spoom::RBS::Annotations < ::Spoom::RBS::Comment; end
2582+
class Spoom::RBS::Annotation < ::Spoom::RBS::Comment; end
25832583

25842584
class Spoom::RBS::Comment
25852585
sig { params(string: ::String, location: ::Prism::Location).void }
@@ -2596,12 +2596,18 @@ class Spoom::RBS::Comments
25962596
sig { void }
25972597
def initialize; end
25982598

2599-
sig { returns(T::Array[::Spoom::RBS::Annotations]) }
2599+
sig { returns(T::Array[::Spoom::RBS::Annotation]) }
26002600
def annotations; end
26012601

2602+
sig { returns(T::Array[::Spoom::RBS::Annotation]) }
2603+
def class_annotations; end
2604+
26022605
sig { returns(T::Boolean) }
26032606
def empty?; end
26042607

2608+
sig { returns(T::Array[::Spoom::RBS::Annotation]) }
2609+
def method_annotations; end
2610+
26052611
sig { returns(T::Array[::Spoom::RBS::Signature]) }
26062612
def signatures; end
26072613
end
@@ -2914,7 +2920,7 @@ class Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs < ::Spoom::Sorbet::Trans
29142920
sig { params(node: T.any(::Prism::ClassNode, ::Prism::ModuleNode, ::Prism::SingletonClassNode)).void }
29152921
def apply_class_annotations(node); end
29162922

2917-
sig { params(annotations: T::Array[::Spoom::RBS::Annotations], sig: ::RBI::Sig).void }
2923+
sig { params(annotations: T::Array[::Spoom::RBS::Annotation], sig: ::RBI::Sig).void }
29182924
def apply_member_annotations(annotations, sig); end
29192925

29202926
sig { params(node: ::Prism::CallNode).void }

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ class << self
283283
RB
284284
end
285285

286+
def test_translate_to_rbi_does_not_insert_t_helpers_for_random_annotations
287+
contents = <<~RB
288+
# @private
289+
class Foo
290+
end
291+
RB
292+
293+
assert_equal(contents, rbs_comments_to_sorbet_sigs(contents))
294+
end
295+
286296
def test_translate_to_rbi_helpers_with_right_order
287297
contents = <<~RB
288298
# @foo

0 commit comments

Comments
 (0)