-
Notifications
You must be signed in to change notification settings - Fork 148
Add support for RBS signature comments #2236
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
696023a
Add dependency to `ruby-next`
Morriar 19fc66b
Bump spoom to v1.6.3
Morriar 8cfd78a
Consider `<compiled>` source locations
Morriar da5dc46
Add runtime rewriter for RBS signature comments
Morriar e038530
Add test for DSL compilers using RBS comments
Morriar c277c2d
Reorder requires so the RBS rewriter gets loaded first
Morriar 237e6fc
Update expectation on `require` since the file is not loaded in the s…
Morriar e244f28
Add support for bootsnap
Morriar c5106c5
Mark a few methods as `@without_runtime`
Morriar 6b6a62b
Bump spoom and rbs
st0012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "require-hooks/setup" | ||
|
||
# This code rewrites RBS comments back into Sorbet's signatures as the files are being loaded. | ||
# This will allow `sorbet-runtime` to wrap the methods as if they were originally written with the `sig{}` blocks. | ||
# This will in turn allow Tapioca to use this signatures to generate typed RBI files. | ||
|
||
begin | ||
# When in a `bootsnap` environment, files are loaded from the cache and won't trigger the `source_transform` method. | ||
# The `require-hooks` gem comes with a `bootsnap` mode that will disable the `bootsnap/compile_cache/iseq` caching. | ||
# Sadly, we're way to early in the boot process to use it as bootsnap won't be loaded yet and the `require-hooks` | ||
# setup won't pick it up. | ||
# | ||
# As a workaround, if we can preemptively require `bootsnap` and `bootsnap/compile_cache/iseq` we manually override | ||
# the `load_iseq` method to disable the caching mechanism. | ||
# | ||
# This will make the Rails app load slower but allows us to trigger the RBS -> RBI source transform. | ||
require "bootsnap" | ||
require "bootsnap/compile_cache/iseq" | ||
|
||
module Bootsnap | ||
module CompileCache | ||
module ISeq | ||
module InstructionSequenceMixin | ||
#: (String) -> RubyVM::InstructionSequence | ||
def load_iseq(path) | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
rescue LoadError | ||
# Bootsnap is not in the bundle, we don't need to do anything. | ||
end | ||
|
||
# We need to include `T::Sig` very early to make sure that the `sig` method is available since gems using RBS comments | ||
# are unlikely to include `T::Sig` in their own classes. | ||
Module.include(T::Sig) | ||
|
||
# Trigger the source transformation for each Ruby file being loaded. | ||
RequireHooks.source_transform(patterns: ["**/*.rb"]) do |path, source| | ||
# The source is most likely nil since no `source_transform` hook was triggered before this one. | ||
source ||= File.read(path) | ||
|
||
# For performance reasons, we only rewrite files that use Sorbet. | ||
if source =~ /^\s*#\s*typed: (ignore|false|true|strict|strong|__STDLIB_INTERNAL)/ | ||
Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs(source, file: path) | ||
end | ||
rescue RBI::RBS::MethodTypeTranslator::Error | ||
# If we can't translate the RBS comments back into Sorbet's signatures, we just skip the file. | ||
source | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KaanOzkan Is there any adverse effect to do this in the context of the LSP addon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it should be fine. Rewriting itself could be an issue with the addon, I'll test it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems okay on a first glance, not blocking.