-
Notifications
You must be signed in to change notification settings - Fork 46
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 Tapioca compiler #533
Draft
st0012
wants to merge
1
commit into
main
Choose a base branch
from
add-tapioca-compiler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+480
−0
Draft
Add Tapioca compiler #533
Changes from all commits
Commits
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 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 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 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,120 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(JobIteration::Iteration) | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
class JobIteration < Compiler | ||
extend T::Sig | ||
|
||
ConstantType = type_member { { fixed: T.class_of(::JobIteration::Iteration) } } | ||
|
||
sig { override.void } | ||
def decorate | ||
return unless constant.instance_methods(false).include?(:build_enumerator) | ||
|
||
root.create_path(constant) do |job| | ||
method = constant.instance_method(:build_enumerator) | ||
constant_name = name_of(constant) | ||
expanded_parameters = compile_method_parameters_to_rbi(method).reject do |typed_param| | ||
typed_param.param.name == "cursor" | ||
end | ||
|
||
job.create_method( | ||
"perform_later", | ||
parameters: perform_later_parameters(expanded_parameters, constant_name), | ||
return_type: "T.any(#{constant_name}, FalseClass)", | ||
class_method: true, | ||
) | ||
|
||
job.create_method( | ||
"perform_now", | ||
parameters: expanded_parameters, | ||
return_type: "void", | ||
class_method: true, | ||
) | ||
|
||
job.create_method( | ||
"perform", | ||
parameters: expanded_parameters, | ||
return_type: "void", | ||
class_method: false, | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
sig do | ||
params( | ||
parameters: T::Array[RBI::TypedParam], | ||
constant_name: T.nilable(String), | ||
).returns(T::Array[RBI::TypedParam]) | ||
end | ||
def perform_later_parameters(parameters, constant_name) | ||
if ::Gem::Requirement.new(">= 7.0").satisfied_by?(::ActiveJob.gem_version) | ||
parameters.reject! { |typed_param| RBI::BlockParam === typed_param.param } | ||
parameters + [create_block_param( | ||
"block", | ||
type: "T.nilable(T.proc.params(job: #{constant_name}).void)", | ||
)] | ||
else | ||
parameters | ||
end | ||
end | ||
|
||
def compile_method_parameters_to_rbi(method_def) | ||
signature = signature_of(method_def) | ||
method_def = signature.nil? ? method_def : signature.method | ||
method_types = parameters_types_from_signature(method_def, signature) | ||
|
||
parameters = T.let(method_def.parameters, T::Array[[Symbol, T.nilable(Symbol)]]) | ||
|
||
parameters.each_with_index.flat_map do |(type, name), index| | ||
fallback_arg_name = "_arg#{index}" | ||
|
||
name = name ? name.to_s : fallback_arg_name | ||
name = fallback_arg_name unless valid_parameter_name?(name) | ||
method_type = T.must(method_types[index]) | ||
|
||
case type | ||
when :req | ||
if signature && (type_value = signature.arg_types[index][1]) && type_value.is_a?(T::Types::FixedHash) | ||
type_value.types.map do |key, value| | ||
create_kw_param(key.to_s, type: value.to_s) | ||
end | ||
else | ||
create_param(name, type: method_type) | ||
end | ||
when :opt | ||
create_opt_param(name, type: method_type, default: "T.unsafe(nil)") | ||
when :rest | ||
create_rest_param(name, type: method_type) | ||
when :keyreq | ||
create_kw_param(name, type: method_type) | ||
when :key | ||
create_kw_opt_param(name, type: method_type, default: "T.unsafe(nil)") | ||
when :keyrest | ||
create_kw_rest_param(name, type: method_type) | ||
when :block | ||
create_block_param(name, type: method_type) | ||
else | ||
raise "Unknown type `#{type}`." | ||
end | ||
end | ||
end | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
all_classes.select { |c| ::JobIteration::Iteration > c } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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.
Do you need to copy this complex method over? I would have instead done a call to
compile_method_parameters_to_rbi
and then walked over the result to find all required params of typeT::Types:FixedHash
and replace them with the keyword parameters instead.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.
I want to do that too but AFAICT the
T::Types
information is not captured in the rbi types: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.
Right, but you can always match that data with
method_types = parameters_types_from_signature(method_def, signature)
to find all required params of that type.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.
Ah, sorry, those are also strings. I guess you could do:
and loop over those to match the params to the ones you need to replace.