Skip to content

Commit c826de3

Browse files
committed
Preserve inline comments inside sig blocks during RBS translation
Comments inside `sig do params(...) end` blocks were silently dropped because the entire sig node byte range was replaced with the RBS string. Collect any comments within the sig node's range and prepend them to the output before the `#:` annotation.
1 parent 29815dc commit c826de3

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def visit_def_node(node)
7878
out = rbs_print(node.location.start_column) do |printer|
7979
printer.print_method_sig(rbi_node, sig)
8080
end
81+
out = prepend_sig_inline_comments(node, out)
8182
@rewriter << Source::Replace.new(node.location.start_offset, node.location.end_offset, out)
8283
end
8384

@@ -207,6 +208,7 @@ def visit_attr(node)
207208
out = rbs_print(node.location.start_column) do |printer|
208209
printer.print_attr_sig(rbi_node, sig)
209210
end
211+
out = prepend_sig_inline_comments(node, out)
210212
@rewriter << Source::Replace.new(node.location.start_offset, node.location.end_offset, out)
211213
end
212214
end
@@ -379,6 +381,19 @@ def delete_extend_t_generics
379381
@extend_t_generics.clear
380382
end
381383

384+
#: (Prism::CallNode, String) -> String
385+
def prepend_sig_inline_comments(sig_node, out)
386+
inline_comments = @comments.select do |comment|
387+
comment.location.start_offset > sig_node.location.start_offset &&
388+
comment.location.end_offset <= sig_node.location.end_offset
389+
end
390+
return out if inline_comments.empty?
391+
392+
indent_str = " " * sig_node.location.start_column
393+
prefix = inline_comments.map { |c| "#{c.slice}\n#{indent_str}" }.join
394+
prefix + out
395+
end
396+
382397
# Collects the last signatures visited and clears the current list
383398
#: -> Array[[Prism::CallNode, RBI::Sig]]
384399
def collect_last_sigs

test/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,37 @@ def foo(param1:, param2:); end
466466
RBS
467467
end
468468

469+
def test_translate_to_rbs_sig_with_inline_param_comments
470+
contents = <<~RB
471+
sig do
472+
params(
473+
# First param
474+
a: Integer,
475+
# Second param
476+
b: String
477+
).void
478+
end
479+
def foo(a, b); end
480+
481+
sig do
482+
# A comment
483+
returns(Integer)
484+
end
485+
attr_reader :x
486+
RB
487+
488+
assert_equal(<<~RBS, sorbet_sigs_to_rbs_comments(contents))
489+
# First param
490+
# Second param
491+
#: (Integer a, String b) -> void
492+
def foo(a, b); end
493+
494+
# A comment
495+
#: Integer
496+
attr_reader :x
497+
RBS
498+
end
499+
469500
def test_translate_to_rbs_defs_within_send
470501
contents = <<~RB
471502
sig { void }

0 commit comments

Comments
 (0)