This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Distinct last positional argument hashes from keywords on Ruby 3 #537
Closed
Closed
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 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 |
---|---|---|
|
@@ -85,12 +85,58 @@ def has_kw_args_in?(args) | |
(args.last.empty? || args.last.keys.any? { |x| x.is_a?(Symbol) }) | ||
end | ||
|
||
# Without considering what the last arg is, could it | ||
# contain keyword arguments? | ||
def could_contain_kw_args?(args) | ||
# If the arg count is less than the required positional args there are no keyword args | ||
return false if args.count <= min_non_kw_args | ||
|
||
@allows_any_kw_args || @allowed_kw_args.any? | ||
# If there is no keyword splat or allowed keywords there are no keyword args | ||
return false unless @allows_any_kw_args || @allowed_kw_args.any? | ||
|
||
# If the args captured are more than the positional arguments then there are keywords | ||
return true if args.count > max_non_kw_args | ||
|
||
# Otherwise we need to check wether the last argument is a positional non default hash, | ||
# e.g. that this: | ||
# | ||
# def capture(_, last = {}, keyword: 2) | ||
# | ||
# Is valid with both: | ||
# | ||
# [1, {:some => :hash}] | ||
# [1, {:keyword => :value}] | ||
# | ||
# but for different reasons. | ||
|
||
# If the keyword splat is allowed, and there are no specified | ||
# keywords args then we will either have returned true on L100 | ||
# or this is invalid. | ||
return false if @allowed_kw_args.empty? && @allows_any_kw_args | ||
|
||
if Hash === args.last | ||
last_arg_matches_any_keywords = !(args.last.keys & @allowed_kw_args).empty? | ||
has_extra_keys = !(args.last.keys - @allowed_kw_args).empty? | ||
|
||
# If we allow the keyword splat, and the last hash contains | ||
# a specified keyword, then this hash contains keywords | ||
return true if @allows_any_kw_args && last_arg_matches_any_keywords | ||
|
||
# If we don't allow the keyword splat, and the last hash contains extra keys | ||
# then it is a positional hash. | ||
return false if !@allows_any_kw_args && has_extra_keys | ||
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. I guess there's a problem between 2.7 and 3.0+ with ( > def arity_kw(x, y = {}, z:2); end
> arity_kw(nil, {a: 1}, {z: 2}) it works fine on 2.7 but fails on 3.0+:
To me, it seems that this expectation is not justified on Ruby 2.7:
as I didn't look at other few failing examples yet. |
||
|
||
# Otherwise assume we have keywords | ||
return true | ||
else | ||
# If the last argument is a matcher nd we're on Ruby 3 which won't mix keyword args | ||
# and a normal hash... | ||
if RubyFeatures.distincts_kw_args_from_positional_hash? | ||
# we've either returned true above on L100 or this is a positional arg matcher | ||
return false | ||
else | ||
# However on older rubies this could contain keyword arguments | ||
return true | ||
end | ||
end | ||
end | ||
|
||
def arbitrary_kw_args? | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.