Skip to content

Commit

Permalink
[Fix: rubocop#12109] Fix an error for Style/ArgumentsForwarding cop…
Browse files Browse the repository at this point in the history
… when forwarding kwargs/block arg and an additional arg

Fix: rubocop#12109
  • Loading branch information
ydah committed Aug 9, 2023
1 parent 1e143c9 commit 1fbfb83
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12109](https://github.com/rubocop/rubocop/issues/12109): Fix an error for `Style/ArgumentsForwarding` cop when forwarding kwargs/block arg and an additional arg. ([@ydah][])
7 changes: 4 additions & 3 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ArgumentsForwarding < Base
minimum_target_ruby_version 2.7

FORWARDING_LVAR_TYPES = %i[splat kwsplat block_pass].freeze
ADDITIONAL_ARG_TYPES = %i[lvar arg].freeze

FORWARDING_MSG = 'Use shorthand syntax `...` for arguments forwarding.'
ARGS_MSG = 'Use anonymous positional arguments forwarding (`*`).'
Expand Down Expand Up @@ -212,7 +213,7 @@ def register_forward_all_offense(def_or_send, send_or_arguments, rest_or_splat)
end

def arguments_range(node, first_node)
arguments = node.arguments
arguments = node.arguments.reject { |arg| ADDITIONAL_ARG_TYPES.include?(arg.type) }

start_node = first_node || arguments.first

Expand Down Expand Up @@ -332,9 +333,9 @@ def target_ruby_version
end

def no_post_splat_args?
splat_index = arguments.index(forwarded_rest_arg)
arg_after_splat = arguments[splat_index + 1]
return true unless (splat_index = arguments.index(forwarded_rest_arg))

arg_after_splat = arguments[splat_index + 1]
[nil, :hash, :block_pass].include?(arg_after_splat&.type)
end

Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,38 @@ def self.get(...)
end
RUBY
end

it 'registers an offense when forwarding kwargs/block arg' do
expect_offense(<<~RUBY)
def foo(**kwargs, &block)
^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding.
baz(**kwargs, &block)
^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding.
end
RUBY

expect_correction(<<~RUBY)
def foo(...)
baz(...)
end
RUBY
end

it 'registers an offense when forwarding kwargs/block arg and an additional arg' do
expect_offense(<<~RUBY)
def foo(x, **kwargs, &block)
^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding.
baz(x, **kwargs, &block)
^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding.
end
RUBY

expect_correction(<<~RUBY)
def foo(x, ...)
baz(x, ...)
end
RUBY
end
end

context 'TargetRubyVersion >= 3.1', :ruby31 do
Expand Down

0 comments on commit 1fbfb83

Please sign in to comment.