Class: RuboCop::Cop::Sidekiq::ConstArgument

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

Overview

This cop checks for Sidekiq worker perform arguments that look like classes or modules. These cannot be serialized for Redis, and should not be used with Sidekiq.

Constants other than classes/modules are not flagged by this cop.

Examples:

# bad
MyWorker.perform_async(MyClass)
MyWorker.perform_async(MyModule)
MyWorker.perform_async(Namespace::Class)

# good
MyWorker.perform_async(MY_CONSTANT)
MyWorker.perform_async(MyClass::MY_CONSTANT)

Constant Summary collapse

CONSTANT_NAME =
/\A[A-Z0-9_]+\z/.freeze
MSG =
'Objects are not Sidekiq-serializable.'
MSG_SELF =
'`self` is not Sidekiq-serializable.'

Constants included from Helpers

Helpers::NODE_MATCHERS

Instance Method Summary collapse

Methods included from Helpers

#approve_node, #expand_arguments, #in_sidekiq_worker?, included, #node_approved?, #sidekiq_arguments, #within?

Instance Method Details

#on_send(node) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/sidekiq/const_argument.rb', line 43

def on_send(node)
  sidekiq_arguments(node).each do |arg|
    next unless const_argument?(arg)
    next if non_class_constant?(arg)

    add_offense(arg, message: arg.self_type? ? MSG_SELF : MSG)
  end
end