Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def visit_singleton_class_node(node)
#: (Prism::DefNode) -> void
def visit_def_node(node)
last_sigs = collect_last_sigs
last_sigs.reject! { |_, sig| sig.is_abstract } unless @translate_abstract_methods
return if last_sigs.empty?

apply_member_annotations(last_sigs)
Expand All @@ -77,8 +78,6 @@ def visit_def_node(node)
rbi_node = builder.tree.nodes.first #: as RBI::Method

last_sigs.each do |node, sig|
next if sig.is_abstract && !@translate_abstract_methods

preserve_multiline_signatures = !!(@preserve_multiline_signatures && sig.loc&.multiline?)

out = rbs_print(
Expand All @@ -90,7 +89,7 @@ def visit_def_node(node)
@rewriter << Source::Replace.new(node.location.start_offset, node.location.end_offset, out)
end

if @translate_abstract_methods && last_sigs.any? { |_, sig| sig.is_abstract }
if last_sigs.any? { |_, sig| sig.is_abstract }
@rewriter << Source::Replace.new(
node.rparen_loc&.end_offset || node.name_loc.end_offset,
node.location.end_offset - 1,
Expand Down Expand Up @@ -299,7 +298,7 @@ def apply_member_annotations(sigs)
@rewriter << Source::Insert.new(insert_pos, "# @final\n#{indent}")
end

if sigs.any? { |_, sig| sig.is_abstract } && @translate_abstract_methods
if sigs.any? { |_, sig| sig.is_abstract }
@rewriter << Source::Insert.new(insert_pos, "# @abstract\n#{indent}")
end

Expand Down
26 changes: 26 additions & 0 deletions test/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,32 @@ def bar=(x)
RBS
end

def test_translate_to_rbs_skips_abstract_methods_without_runtime
contents = <<~RB
class Foo
T::Sig::WithoutRuntime.sig do
abstract.params(query: Collect::PrivateAssociationRelation).returns(T::Array[T.attached_class])
end
def collect(query); end

sig { returns(Integer) }
def count = 42
end
RB

assert_equal(<<~RBS, sorbet_sigs_to_rbs_comments(contents, translate_abstract_methods: false))
class Foo
T::Sig::WithoutRuntime.sig do
abstract.params(query: Collect::PrivateAssociationRelation).returns(T::Array[T.attached_class])
end
def collect(query); end

#: -> Integer
def count = 42
end
RBS
end

def test_translate_method_sigs_to_rbs_without_positional_names
contents = <<~RBI
class A
Expand Down
Loading