-
Notifications
You must be signed in to change notification settings - Fork 160
Share reflected method parameter compilation #2689
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,34 @@ def extract_type_parameters(type_strings) | |
| type_strings.join(", ").scan(TYPE_PARAMETER_MATCHER).flatten.uniq | ||
| end | ||
|
|
||
| #: (Symbol type, String? name, Integer index) -> [RBI::Param, String] | ||
| def create_method_parameter(type, name, index) | ||
| fallback_name = "_arg#{index}" | ||
| name ||= fallback_name | ||
| name = fallback_name unless valid_parameter_name?(name) | ||
|
|
||
| parameter = case type | ||
| when :req | ||
| RBI::ReqParam.new(name) | ||
| when :opt | ||
| RBI::OptParam.new(name, "T.unsafe(nil)") | ||
| when :rest | ||
| RBI::RestParam.new(name) | ||
| when :keyreq | ||
| RBI::KwParam.new(name) | ||
| when :key | ||
| RBI::KwOptParam.new(name, "T.unsafe(nil)") | ||
| when :keyrest | ||
| RBI::KwRestParam.new(name) | ||
| when :block | ||
| RBI::BlockParam.new(name) | ||
| else | ||
| Kernel.raise "Unknown type `#{type}`." | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL, agent found that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There will also be a def foo(*args, &nil) = 42
method(:foo).parameters # => [[:rest, :args], [:noblock]] |
||
| end | ||
|
|
||
| [parameter, name] | ||
| end | ||
|
|
||
| #: (String name) -> bool | ||
| def valid_method_name?(name) | ||
| Prism.parse_success?("def self.#{name}(a); end") | ||
|
|
||
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 we are allocating a
fallback_nameunnecessarily for each parameter here. Can we also refactorvalid_parameter_name?to accept aString?and rejectnilvalues, so that we can do:This way the new string allocation will only happen for invalid or missing parameter names