Skip to content

Comments

Fix RBS rewriting to preserve line numbers#861

Draft
Morriar wants to merge 1 commit intomainfrom
fix-rbs-line-numbers
Draft

Fix RBS rewriting to preserve line numbers#861
Morriar wants to merge 1 commit intomainfrom
fix-rbs-line-numbers

Conversation

@Morriar
Copy link
Contributor

@Morriar Morriar commented Feb 17, 2026

Fixes #787

Problem

When using Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sig, line numbers weren't preserved. Multi-line RBS comments were replaced with single-line sigs, causing method definitions to shift up in the file. This made errors and stacktraces misalign with the original source.

Example:

# Original (method at line 6)
#: (
#| Integer
#| ) -> void
def foo(i); end

# After translation (method moved to line 4)
sig { params(i: Integer).void }
def foo(i); end

Solution

Added a preserve_line_numbers helper that:

  1. Calculates how many lines the original RBS comment spans
  2. Calculates how many lines the generated sig will have
  3. Adds blank lines after the sig to match the original line count

Result:

# After fix (method stays at line 6)
sig { params(i: Integer).void }


def foo(i); end

Testing

  • All existing tests pass
  • Updated test_translate_to_rbi_multiline_sigs to expect preserved line numbers
  • Manually tested with various RBS comment sizes (single-line, multi-line, complex signatures)

Changes

  • lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb:
    • Added preserve_line_numbers helper method
    • Updated rewrite_def to use line preservation
    • Updated visit_attr to use line preservation
  • test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb:
    • Updated test_translate_to_rbi_multiline_sigs expectations

When converting RBS comments to Sorbet sigs, the line numbers of method
definitions were shifting because multi-line RBS comments were being
replaced with single-line sigs without padding.

This fix adds blank lines after generated sigs to match the line count
of the original RBS comments, ensuring method definitions stay on their
original line numbers.

Fixes #787
@Morriar Morriar requested a review from a team as a code owner February 17, 2026 17:16
padding = "\n" * (original_lines - replacement_lines)
replacement_string + padding
else
replacement_string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if original_lines < replacement_lines? Could that ever happen? Should we handle that explicitly and raise if that is not expected?

@Morriar Morriar marked this pull request as draft February 17, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RBS Rewriting doesn't preserve line numbers

2 participants