Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a9bf11

Browse files
committed
Small arity refactor
1 parent 51056cd commit 5a9bf11

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

lib/syntax_tree/node.rb

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -854,18 +854,17 @@ def ===(other)
854854
end
855855

856856
def arity
857-
accepts_infinite_arguments? ? Float::INFINITY : parts.length
858-
end
859-
860-
private
861-
862-
def accepts_infinite_arguments?
863-
parts.any? do |part|
864-
part.is_a?(ArgStar) || part.is_a?(ArgsForward) ||
865-
(
866-
part.is_a?(BareAssocHash) &&
867-
part.assocs.any? { |p| p.is_a?(AssocSplat) }
868-
)
857+
parts.sum do |part|
858+
case part
859+
when ArgStar, ArgsForward
860+
Float::INFINITY
861+
when BareAssocHash
862+
part.assocs.sum do |assoc|
863+
assoc.is_a?(AssocSplat) ? Float::INFINITY : 1
864+
end
865+
else
866+
1
867+
end
869868
end
870869
end
871870
end
@@ -8383,18 +8382,24 @@ def ===(other)
83838382

83848383
# Returns a range representing the possible number of arguments accepted
83858384
# by this params node not including the block. For example:
8386-
# def foo(a, b = 1, c:, d: 2, &block)
8387-
# ...
8388-
# end
8389-
# has arity 2..4
8385+
#
8386+
# def foo(a, b = 1, c:, d: 2, &block)
8387+
# ...
8388+
# end
8389+
#
8390+
# has arity 2..4.
8391+
#
83908392
def arity
83918393
optional_keywords = keywords.count { |_label, value| value }
8394+
83928395
lower_bound =
83938396
requireds.length + posts.length + keywords.length - optional_keywords
83948397

83958398
upper_bound =
8396-
lower_bound + optionals.length +
8397-
optional_keywords if keyword_rest.nil? && rest.nil?
8399+
if keyword_rest.nil? && rest.nil?
8400+
lower_bound + optionals.length + optional_keywords
8401+
end
8402+
83988403
lower_bound..upper_bound
83998404
end
84008405

0 commit comments

Comments
 (0)