Commit 379c7f55 authored by Thong Kuah's avatar Thong Kuah Committed by Albert Salim

Use the last non-block arg for cop

In Ruby, the explicit block argument is special, and for our case cannot
be converted into a keyword argument. The argument we want instead is
the last non-block argument.
parent 9943e2ac
...@@ -16,7 +16,7 @@ module RuboCop ...@@ -16,7 +16,7 @@ module RuboCop
KEYWORD_DEPRECATION_STR = 'maybe ** should be added to the call' KEYWORD_DEPRECATION_STR = 'maybe ** should be added to the call'
def on_send(node) def on_send(node)
arg = node.arguments.last arg = get_last_argument(node)
return unless arg return unless arg
return unless known_match?(processed_source.file_path, node.first_line, node.method_name.to_s) return unless known_match?(processed_source.file_path, node.first_line, node.method_name.to_s)
...@@ -44,6 +44,12 @@ module RuboCop ...@@ -44,6 +44,12 @@ module RuboCop
private private
def get_last_argument(node)
return node.arguments[-2] if node.block_argument?
node.arguments.last
end
def known_match?(file_path, line_number, method_name) def known_match?(file_path, line_number, method_name)
file_path_from_root = file_path.sub(File.expand_path('../../..', __dir__), '') file_path_from_root = file_path.sub(File.expand_path('../../..', __dir__), '')
......
...@@ -95,6 +95,23 @@ RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument do ...@@ -95,6 +95,23 @@ RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument do
SOURCE SOURCE
end end
it 'registers an offense on the last non-block argument' do
expect_offense(<<~SOURCE, 'create_service.rb')
users.call(id, params, &block)
^^^^^^ Using the last argument as keyword parameters is deprecated
SOURCE
expect_correction(<<~SOURCE)
users.call(id, **params, &block)
SOURCE
end
it 'does not register an offense if the only argument is a block argument' do
expect_no_offenses(<<~SOURCE, 'create_service.rb')
users.call(&block)
SOURCE
end
it 'registers an offense and corrects by converting splat to double splat' do it 'registers an offense and corrects by converting splat to double splat' do
expect_offense(<<~SOURCE, 'create_service.rb') expect_offense(<<~SOURCE, 'create_service.rb')
users.call(id, *params) users.call(id, *params)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment