Commit 0b34288f authored by Albert Salim's avatar Albert Salim

Merge branch 'fix_lint_last_block_arg' into 'master'

[RUN ALL RSPEC] Use the last non-block arg for cop

See merge request gitlab-org/gitlab!51276
parents f867acf3 e55d7b67
......@@ -423,7 +423,7 @@ rspec:deprecations:
variables:
SETUP_DB: "false"
script:
- bundle exec rubocop --only Lint/LastKeywordArgument --parallel
- run_timed_command "bundle exec rubocop --only Lint/LastKeywordArgument --parallel"
artifacts:
expire_in: 31d
when: always
......
......@@ -16,7 +16,7 @@ module RuboCop
KEYWORD_DEPRECATION_STR = 'maybe ** should be added to the call'
def on_send(node)
arg = node.arguments.last
arg = get_last_argument(node)
return unless arg
return unless known_match?(processed_source.file_path, node.first_line, node.method_name.to_s)
......@@ -44,6 +44,12 @@ module RuboCop
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)
file_path_from_root = file_path.sub(File.expand_path('../../..', __dir__), '')
......
......@@ -38,6 +38,8 @@ RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument do
- |
DEPRECATION WARNING: /Users/tkuah/code/ee-gdk/gitlab/create_service.rb:1: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/tkuah/code/ee-gdk/gitlab/user.rb:17: warning: The called method `call' is defined here
- |
DEPRECATION WARNING: /Users/tkuah/code/ee-gdk/gitlab/other_warning_type.rb:1: warning: Some other warning type
YAML
end
......@@ -62,7 +64,7 @@ RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument do
allow(File).to receive(:read).and_return(create_spec_yaml, projects_spec_yaml)
end
it 'registers an offense' do
it 'registers an offense for last keyword warning' do
expect_offense(<<~SOURCE, 'create_service.rb')
users.call(params)
^^^^^^ Using the last argument as keyword parameters is deprecated
......@@ -73,6 +75,12 @@ RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument do
SOURCE
end
it 'does not register an offense for other warning types' do
expect_no_offenses(<<~SOURCE, 'other_warning_type.rb')
users.call(params)
SOURCE
end
it 'registers an offense for the new method call' do
expect_offense(<<~SOURCE, 'projects_spec.rb')
Project.new(params)
......@@ -95,6 +103,23 @@ RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument do
SOURCE
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
expect_offense(<<~SOURCE, 'create_service.rb')
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