Skip to content

Commit

Permalink
Small refactor of rubocop#9127
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Nov 30, 2020
1 parent a05c2e6 commit 5157f0a
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions lib/rubocop/cop/lint/unexpected_block_arity.rb
Expand Up @@ -39,9 +39,6 @@ class UnexpectedBlockArity < Base
def on_block(node)
return if acceptable?(node)

# If there is a splat argument, then the arity is fine
return true if node.arguments.any?(&:restarg_type?)

expected = expected_arity(node.method_name)
actual = arg_count(node)
return if actual >= expected
Expand All @@ -50,17 +47,7 @@ def on_block(node)
add_offense(node, message: message)
end

def on_numblock(node)
return if acceptable?(node)

expected = expected_arity(node.method_name)
actual = node.children[1] # the maximum numbered param for the block

return if actual >= expected

message = format(MSG, method: node.method_name, expected: expected, actual: actual)
add_offense(node, message: message)
end
alias on_numblock on_block

private

Expand All @@ -81,10 +68,16 @@ def expected_arity(method)
end

def arg_count(node)
return node.children[1] if node.numblock_type? # the maximum numbered param for the block

# Only `arg`, `optarg` and `mlhs` (destructuring) count as arguments that
# can be used. Keyword arguments are not used for these methods so are
# ignored.
node.arguments.count { |arg| arg.arg_type? || arg.optarg_type? || arg.mlhs_type? }
node.arguments.count do |arg|
return Float::INFINITY if arg.restarg_type?

arg.arg_type? || arg.optarg_type? || arg.mlhs_type?
end
end
end
end
Expand Down

0 comments on commit 5157f0a

Please sign in to comment.