Class: RuboCop::Cop::Sidekiq::PerformMethodParameters

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sidekiq/perform_method_parameters.rb

Overview

Checks that the perform method does not use keyword arguments.

Sidekiq serializes job arguments to JSON, which does not support Ruby keyword arguments. Using keyword arguments will cause errors or unexpected behavior.

Examples:

# bad
def perform(user_id:, status:)
end

# bad
def perform(id, status: 'pending')
end

# good
def perform(user_id, status)
end

Constant Summary collapse

MSG =
'Do not use keyword arguments in the `perform` method. ' \
'Sidekiq cannot serialize keyword arguments to JSON.'

Instance Method Summary collapse

Methods included from Sidekiq::Language

#active_job_class?, #perform_call?, #sidekiq_include?, #sidekiq_options_call?

Instance Method Details

#on_def(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/sidekiq/perform_method_parameters.rb', line 34

def on_def(node)
  return unless perform_method?(node)
  return unless in_sidekiq_job?(node)

  node.arguments.each do |arg|
    next unless keyword_argument?(arg)

    add_offense(arg)
  end
end

#perform_method?(node) ⇒ Object



30
31
32
# File 'lib/rubocop/cop/sidekiq/perform_method_parameters.rb', line 30

def_node_matcher :perform_method?, "(def :perform ...)\n"