Skip to content

Line-matching rewriter#940

Merged
vinistock merged 9 commits into
mainfrom
Alex/line-matching
Jun 19, 2026
Merged

Line-matching rewriter#940
vinistock merged 9 commits into
mainfrom
Alex/line-matching

Conversation

@amomchilov

@amomchilov amomchilov commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

This PR adds a line preserving rewriter from RBS to Sorbet sigs, which is helpful when validating the runtime so that backtraces don't get shifted by the translation.

The idea is to introduce a new format, which changes the way we translate to not always simply delete the RBS, but instead keep artifacts that will maintain the original line numbers.

@amomchilov amomchilov force-pushed the Alex/line-matching branch 2 times, most recently from 9aad209 to df5c587 Compare June 6, 2026 00:32
@amomchilov amomchilov force-pushed the Alex/line-matching branch from df5c587 to 754641a Compare June 15, 2026 17:19
@amomchilov amomchilov changed the base branch from main to graphite-base/940 June 16, 2026 22:54
@amomchilov amomchilov force-pushed the Alex/line-matching branch from 754641a to 273ef3e Compare June 16, 2026 22:55
@amomchilov amomchilov changed the base branch from graphite-base/940 to Alex/Options-object June 16, 2026 22:55
@amomchilov

amomchilov commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@amomchilov amomchilov force-pushed the Alex/Options-object branch 2 times, most recently from eeb32d8 to d5692f6 Compare June 17, 2026 14:59
@amomchilov amomchilov force-pushed the Alex/line-matching branch from 273ef3e to 4dafa78 Compare June 17, 2026 14:59
Comment thread lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs/base_translator.rb Outdated
@amomchilov amomchilov force-pushed the Alex/line-matching branch from 4dafa78 to 846ea47 Compare June 17, 2026 19:01
@amomchilov amomchilov force-pushed the Alex/Options-object branch from d5692f6 to 7654edc Compare June 17, 2026 19:01
@amomchilov amomchilov force-pushed the Alex/line-matching branch from 846ea47 to 0bdd70e Compare June 17, 2026 19:10
#: LineMatchedRBIFormat
attr_reader :default
end
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will this be used similar to HumanReadableRBIFormat in lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb? Curious why it's not being triggered in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. It's not used yet because I didn't finish any of the nicer line-matching formatting in the parts the RBI gem is responsible for printing.

Instead, I just pad with as many blank lines as we need.

In a future iteration, this will be used to signal to the RBI gem that e.g. it should break a multi-line RBS sig into a multiline Sorbet sig.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When we want to expose this from the CLI, yes we need to do that work. For now, we're only using it via the API, so it's enough for this PR.

#| insert_pos: Integer,
#| sorbet_replacement: String?
#| ) -> void
def apply_class_annotation(annotation, parent_node:, insert_pos:, sorbet_replacement:) = raise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These are making this class harder to read but I'm not sure what we can do. One alternative I kinda like it to move these abstract methods to HumanReadableTranslator instead. Call sites in this class would then delegate to the appropriate "mode" that's being set, @translation_mode.apply_class_anotation. It wouldn't get rid of the abstract but make the intent more clear. Wdyt? Maybe as a future improvement to not hold up the PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@amomchilov I'll leave this one for your assessment. I agree that something along the lines of what Kaan suggested would make the code a bit easier to follow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same. Will follow-up.

Comment thread test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb Outdated
@amomchilov amomchilov force-pushed the Alex/line-matching branch 2 times, most recently from a756398 to 886194e Compare June 19, 2026 14:46
@vinistock vinistock force-pushed the Alex/Options-object branch 3 times, most recently from 9d80524 to 48c4e23 Compare June 19, 2026 15:22
@vinistock vinistock force-pushed the Alex/line-matching branch from 886194e to e92832d Compare June 19, 2026 15:25
Base automatically changed from Alex/Options-object to main June 19, 2026 17:35
@vinistock vinistock force-pushed the Alex/line-matching branch from e92832d to 7b7ef52 Compare June 19, 2026 19:59
@vinistock vinistock marked this pull request as ready for review June 19, 2026 20:04
@vinistock vinistock requested a review from a team as a code owner June 19, 2026 20:04

@amomchilov amomchilov left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice fix!

def apply_member_annotations(annotations, sig)
known = [] #: Array[Spoom::RBS::Annotation]

annotations.each do |annotation|

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll follow-up to tidy this with Array#map

@KaanOzkan KaanOzkan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There are still 2 edge cases (one is hidden in a resolved comment) but I think the implementation is good. If it's not too hard it'd be good to try to fix these too.

#: (Spoom::RBS::Signature, type_params: Array[::RBS::AST::TypeParam]) -> void
def rewrite_type_params_signature(signature, type_params:)
# Rewrite `#: [A, B]` into `# RBS_WRITTEN_ANNOTATION: [A, B]`
@rewriter << Source::Replace.new(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similar to previous comment, multiline + annotation edge case is failing:

#: [A,
#| B]

is translated into

# RBS_WRITTEN_ANNOTATION: [A,
#| B]

@alexcrocha alexcrocha left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving to unblock. We can fix forward any edge cases that come up

@KaanOzkan KaanOzkan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@vinistock vinistock force-pushed the Alex/line-matching branch from 55d9f6a to e7964c9 Compare June 19, 2026 21:02
@vinistock vinistock merged commit 2d05cc8 into main Jun 19, 2026
11 checks passed
@vinistock vinistock deleted the Alex/line-matching branch June 19, 2026 21:14
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.

4 participants