Skip to content

Commit 2ef53d7

Browse files
committed
Allow translation of abstract methods
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent bd88374 commit 2ef53d7

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

lib/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def visit_singleton_class_node(node)
4444
def visit_def_node(node)
4545
last_sigs = collect_last_sigs
4646
return if last_sigs.empty?
47-
return if last_sigs.any? { |_, sig| sig.is_abstract }
4847

4948
apply_member_annotations(last_sigs)
5049

@@ -237,23 +236,25 @@ def apply_member_annotations(sigs)
237236
node, _sig = sigs.first #: as [Prism::CallNode, RBI::Sig]
238237
insert_pos = node.location.start_offset
239238

239+
indent = " " * node.location.start_column
240+
240241
if sigs.any? { |_, sig| sig.without_runtime }
241-
@rewriter << Source::Insert.new(insert_pos, "# @without_runtime\n")
242+
@rewriter << Source::Insert.new(insert_pos, "# @without_runtime\n#{indent}")
242243
end
243244

244245
if sigs.any? { |_, sig| sig.is_final }
245-
@rewriter << Source::Insert.new(insert_pos, "# @final\n")
246+
@rewriter << Source::Insert.new(insert_pos, "# @final\n#{indent}")
246247
end
247248

248249
if sigs.any? { |_, sig| sig.is_abstract }
249-
@rewriter << Source::Insert.new(insert_pos, "# @abstract\n")
250+
@rewriter << Source::Insert.new(insert_pos, "# @abstract\n#{indent}")
250251
end
251252

252253
if sigs.any? { |_, sig| sig.is_override }
253254
@rewriter << if sigs.any? { |_, sig| sig.allow_incompatible_override }
254-
Source::Insert.new(insert_pos, "# @override(allow_incompatible: true)\n")
255+
Source::Insert.new(insert_pos, "# @override(allow_incompatible: true)\n#{indent}")
255256
else
256-
Source::Insert.new(insert_pos, "# @override\n")
257+
Source::Insert.new(insert_pos, "# @override\n#{indent}")
257258
end
258259
end
259260

test/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments_test.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,29 @@ def a
7474

7575
def test_does_not_translate_to_rbs_abstract_methods
7676
contents = <<~RB
77-
sig { abstract.void }
78-
def foo; end
77+
class Foo
78+
sig { abstract.void }
79+
def foo; end
7980
80-
sig { void }
81-
def bar; end
81+
class Bar
82+
sig { abstract.void }
83+
def bar; end
84+
end
85+
end
8286
RB
8387

8488
assert_equal(<<~RBS, sorbet_sigs_to_rbs_comments(contents))
85-
sig { abstract.void }
86-
def foo; end
89+
class Foo
90+
# @abstract
91+
#: -> void
92+
def foo; end
8793
88-
#: -> void
89-
def bar; end
94+
class Bar
95+
# @abstract
96+
#: -> void
97+
def bar; end
98+
end
99+
end
90100
RBS
91101
end
92102

0 commit comments

Comments
 (0)