Commit 485f84fc authored by Arun Kumar Mohan's avatar Arun Kumar Mohan

Add Rubocop cop to flag keyword arguments usage in Sidekiq workers

parent 3cb7eebb
......@@ -28,6 +28,12 @@ AllCops:
- 'file_hooks/**/*'
CacheRootDirectory: tmp
Cop/AvoidKeywordArgumentsInSidekiqWorkers:
Enabled: true
Include:
- 'app/workers/**/*'
- 'ee/app/workers/**/*'
Cop/StaticTranslationDefinition:
Enabled: true
Exclude:
......
---
title: Add Rubocop cop to flag keyword arguments usage in Sidekiq workers
merge_request: 31551
author: Arun Kumar Mohan
type: added
module RuboCop
module Cop
# Cop that blacklists keyword arguments usage in Sidekiq workers
class AvoidKeywordArgumentsInSidekiqWorkers < RuboCop::Cop::Cop
MSG = "Do not use keyword arguments in Sidekiq workers. " \
"For details, check https://github.com/mperham/sidekiq/issues/2372".freeze
OBSERVED_METHOD = :perform
def on_def(node)
return if node.method_name != OBSERVED_METHOD
node.arguments.each do |argument|
if argument.type == :kwarg || argument.type == :kwoptarg
add_offense(node, location: :expression)
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers'
describe RuboCop::Cop::AvoidKeywordArgumentsInSidekiqWorkers do
include CopHelper
subject(:cop) { described_class.new }
it 'flags violation for keyword arguments usage in perform method signature' do
expect_offense(<<~RUBY)
def perform(id:)
^^^^^^^^^^^^^^^^ Do not use keyword arguments in Sidekiq workers. For details, check https://github.com/mperham/sidekiq/issues/2372
end
RUBY
end
it 'flags violation for optional keyword arguments usage in perform method signature' do
expect_offense(<<~RUBY)
def perform(id: nil)
^^^^^^^^^^^^^^^^^^^^ Do not use keyword arguments in Sidekiq workers. For details, check https://github.com/mperham/sidekiq/issues/2372
end
RUBY
end
it 'does not flag a violation for standard optional arguments usage in perform method signature' do
expect_no_offenses(<<~RUBY)
def perform(id = nil)
end
RUBY
end
it 'does not flag a violation for keyword arguments usage in non-perform method signatures' do
expect_no_offenses(<<~RUBY)
def helper(id:)
end
RUBY
end
it 'does not flag a violation for optional keyword arguments usage in non-perform method signatures' do
expect_no_offenses(<<~RUBY)
def helper(id: nil)
end
RUBY
end
end
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