Skip to content

Rewrite type aliases#791

Merged
KaanOzkan merged 3 commits into
mainfrom
uk-type-alias-rewriting
Oct 17, 2025
Merged

Rewrite type aliases#791
KaanOzkan merged 3 commits into
mainfrom
uk-type-alias-rewriting

Conversation

@paracycle

@paracycle paracycle commented Aug 13, 2025

Copy link
Copy Markdown
Member

Resolves #809

@paracycle paracycle requested a review from Morriar August 13, 2025 17:24
@paracycle paracycle force-pushed the uk-type-alias-rewriting branch 4 times, most recently from d1a833a to 1823880 Compare August 13, 2025 19:43

@Morriar Morriar 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.

Do we have any plans for type aliases used in inline annotations?

x = foo #: myTypeAlias

Comment thread lib/spoom/parse.rb Outdated
end

#: (String ruby, file: String) -> [Prism::Node, Array[Prism::Comment]]
def parse_ruby_with_comments(ruby, file:)

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.

Why can't we use parse_ruby(ruby, file:, comments: true)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah yeah, I meant to note this, but forgot.

I need the comments for the whole file to be returned, so that the rewriter can access all comments, and not just the ones that are attached to nodes. That need arises since type alias comments are not supposed to be attached to nodes.

The parse_ruby method was not returning comments, and instead of changing the return type of that method and changing all the call sites, I decided to add another method with different return type.

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.

Which comments are not attached to node?

I tried this:

require "prism"

result = Prism.parse("# foo")
result.attach_comments!
result.value # => ProgramNode
result.value.comments.first.slice # => "# foo"

So it looks like the nodes are properly attached?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, but what I am saying is that we expect to find #: type_alias comments in arbitrary locations in the document. They don't have to be attached to any particular node that we are looking for, they might even end up being attached to a local variable assignment that is happening inside a class body.

That is why I opted to process type aliases from the global list of all comments nodes in the document, as opposed to fishing them out from the nodes they happen to be attached to.

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.

Oh I see, you don't want to have to visit the tree to figure where the comment is?

I think I'm fine with changing the signature of parse_ruby and changing the callsites instead of duplicating 👍

We could also remove the conditional on the comments: keyword parameter.

Let's release with a version change though.

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.

Done in a separate commit.

Comment thread test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb
rbs_type = decls.first
sorbet_type = RBI::RBS::TypeTranslator.translate(rbs_type.type)

alias_name = ::RBS::TypeName.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.

Could we reuse the logic from RBI::RBS::TypeTranslator#translate_type_alias?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I do want to use it, but I am not sure how. There is a TypeAlias for a type-alias type (i.e. type alias reference like myAlias) which only has a name and which RBI::RBS::TypeTranslator#translate_type_alias handles.

What we have here is a TypeAlias for a type-alias declaration (i.e. type alias declaration like type myAlias = Integer), which has a name and a type, which this needs to handle. I am not sure how I can use the logic for the previous one in this context, but let me think about it.

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.

I think the cleanest way to do this would be to modify rbi and create a new public helper function that takes a name. It could be used both here and in TypeTranslator. Since it'd require a version bump in rbi I'll hold off on it for now but let me know if you want it done.

@paracycle

Copy link
Copy Markdown
Member Author

Do we have any plans for type aliases used in inline annotations?

x = foo #: myTypeAlias

Those should just work since the rbi translation change should translate that construct as part of type translation. I will add a test to verify, though.

@paracycle paracycle marked this pull request as ready for review August 15, 2025 19:56
@paracycle paracycle requested a review from a team as a code owner August 15, 2025 19:56
@paracycle paracycle force-pushed the uk-type-alias-rewriting branch from 1823880 to 90ff825 Compare August 25, 2025 21:09
@KaanOzkan KaanOzkan force-pushed the uk-type-alias-rewriting branch from 90ff825 to 2509a94 Compare October 16, 2025 16:00
Comment thread lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb Outdated
@KaanOzkan KaanOzkan force-pushed the uk-type-alias-rewriting branch from 2509a94 to 5fbfe87 Compare October 16, 2025 20:34

@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.

Do we have any plans for type aliases used in inline annotations?
x = foo #: myTypeAlias

Spoom doesn't translate RBS assertions to their RBI equivalents so we're skipping this for now.

rbs_type = decls.first
sorbet_type = RBI::RBS::TypeTranslator.translate(rbs_type.type)

alias_name = ::RBS::TypeName.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.

I think the cleanest way to do this would be to modify rbi and create a new public helper function that takes a name. It could be used both here and in TypeTranslator. Since it'd require a version bump in rbi I'll hold off on it for now but let me know if you want it done.

Comment thread lib/spoom/parse.rb Outdated
end

#: (String ruby, file: String) -> [Prism::Node, Array[Prism::Comment]]
def parse_ruby_with_comments(ruby, file:)

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.

Done in a separate commit.

@KaanOzkan KaanOzkan requested a review from Morriar October 16, 2025 20:53
@KaanOzkan KaanOzkan merged commit 3f4ce87 into main Oct 17, 2025
8 checks passed
@KaanOzkan KaanOzkan deleted the uk-type-alias-rewriting branch October 17, 2025 20:24
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.

Rewrite RBS type aliases into RBI for Tapioca compatibility

4 participants