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 command to translate some inline type annotations to RBS comments #658

Merged
merged 3 commits into from
Feb 28, 2025

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Feb 26, 2025

This PR add translation of T.let when found in an assign to RBS comments.

This:

a = T.let(nil, T.nilable(Integer))
b = ::T.let([], T::Array[Integer])

is translated into:

a = nil #: Integer?
b = [] #: Array[Integer]

Note that we only support translation in assigns for local, global, instance, class variables, constants and constant paths.

Annotations not in an assign node are ignored:

ignored = foo(T.let(nil, T.nilable(Integer))
T.let(also_ignored, T.nilable(Integer))

We also try our best to make the resulting code pass style checks and reindent properly:

a = T.let(
  [
    1, 
    2, 
    3
  ],
  T::Array[Integer]
)

is thus rewrote as:

a = [
  1, 
  2, 
  3
] #: Array[Integer]

This is still experimental as I'm sure we're missing a bunch of edge-cases but that should be enough to get us started.

Here are two examples running this tool on other open-source projects:

@Morriar Morriar added the feature New feature label Feb 26, 2025
@Morriar Morriar self-assigned this Feb 26, 2025
@Morriar Morriar requested a review from a team as a code owner February 26, 2025 21:17
@Morriar Morriar force-pushed the at-translate-srb-casts branch from a13f32a to b556db0 Compare February 26, 2025 22:22
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar force-pushed the at-translate-srb-casts branch from b556db0 to 1692179 Compare February 28, 2025 21:37
@Morriar
Copy link
Collaborator Author

Morriar commented Feb 28, 2025

Fixed review comments, also enhanced the placement of the comment to be close to the \n so we can support cases like this:

(x = T.let(y, String))
# becomes
(x = y) #: String

x = T.let(y, String) if z
# becomes
x = y if z #: String

@Morriar Morriar merged commit 22bf3e3 into main Feb 28, 2025
8 checks passed
@Morriar Morriar deleted the at-translate-srb-casts branch February 28, 2025 22:18
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.

3 participants