Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Type parsing #332

Merged
merged 2 commits into from
Aug 2, 2024
Merged

Add Type parsing #332

merged 2 commits into from
Aug 2, 2024

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Jul 31, 2024

This PR adds parsing for the Type builder introduced in #331.

This allows the user to parse types from a string and get a RBI::Type from them:

type = Type.parse_string("T.nilable(Foo)")
type.class # => Type::Nilable
type.type # => Type::Simple("Foo")

Note that we do not migrate the RBI::Parser to parse types at this point, the general parser still uses Strings to represent types in signatures and T::Structs.

cc. @paracycle

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar requested a review from a team as a code owner July 31, 2024 19:40
@Morriar Morriar requested review from amomchilov and egiurleo July 31, 2024 19:40
@Morriar Morriar self-assigned this Jul 31, 2024
@Morriar Morriar added the feature New feature label Jul 31, 2024
case node
when Prism::ConstantReadNode
# `Foo`
Type.simple(node.slice)
Copy link
Member

Choose a reason for hiding this comment

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

I know you had concerns about the parser potentially raising when parsing non-sensical input instead of parsing it as is and allowing another system to show errors.

For that reason, I wonder if we should use the constructors directly here, instead of using the helper methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm still conflicted on the choice with made regarding the builder API.

Calling new directly is a good escape hatch but by making it private we made it impossible to use for external users. Maybe the right thing to do would be to keep the new public?

In the meantime you're right, I changed all the calls from the parser to use new directly rather than the builder interface.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, but it is easier to make the new public later than to make it private later. So, I think I like how we are exposing a more minimal API for the time being.

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar merged commit e57da3e into main Aug 2, 2024
8 checks passed
@Morriar Morriar deleted the at-type-parser branch August 2, 2024 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants