Skip to content

Commit

Permalink
Support arguments forwarding in Lint/ToEnumArguments cop (#8961)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Oct 28, 2020
1 parent 349e378 commit 9ceb446
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/to_enum_arguments.rb
Expand Up @@ -44,7 +44,6 @@ class ToEnumArguments < Base
(pair (sym %1) (lvar %1))
PATTERN

# TODO: add support for argument forwarding (`...`) when ruby 3.0 is released
def on_send(node)
def_node = node.each_ancestor(:def, :defs).first
return unless def_node
Expand Down Expand Up @@ -85,6 +84,8 @@ def argument_match?(send_arg, def_arg)
send_arg.pairs.any? { |pair| passing_keyword_arg?(pair, def_arg_name) }
when :kwrestarg
send_arg.each_child_node(:kwsplat).any? { |child| child.source == def_arg.source }
when :forward_arg
send_arg.forwarded_args_type?
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/rspec/shared_contexts.rb
Expand Up @@ -138,3 +138,7 @@ def source_range(range, buffer: source_buffer)
RSpec.shared_context 'ruby 2.7', :ruby27 do
let(:ruby_version) { 2.7 }
end

RSpec.shared_context 'ruby 3.0', :ruby30 do
let(:ruby_version) { 3.0 }
end
23 changes: 20 additions & 3 deletions spec/rubocop/cop/lint/to_enum_arguments_spec.rb
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::ToEnumArguments do
subject(:cop) { described_class.new }

RSpec.describe RuboCop::Cop::Lint::ToEnumArguments, :config do
it 'registers an offense when required arg is missing' do
expect_offense(<<~RUBY)
def m(x)
Expand Down Expand Up @@ -131,4 +129,23 @@ def m(x, y = 1, *args, required:, optional: true, **kwargs, &block)
end
RUBY
end

context 'arguments forwarding', :ruby30 do
it 'registers an offense when enumerator is created with non matching arguments' do
expect_offense(<<~RUBY)
def m(...)
return to_enum(:m, x, ...) unless block_given?
^^^^^^^^^^^^^^^^^^^ Ensure you correctly provided all the arguments.
end
RUBY
end

it 'does not register an offense when enumerator is created with the correct arguments' do
expect_no_offenses(<<~RUBY)
def m(...)
return to_enum(:m, ...) unless block_given?
end
RUBY
end
end
end

0 comments on commit 9ceb446

Please sign in to comment.