Class: RuboCop::Cop::Sidekiq::SymbolArgument

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Helpers
Defined in:
lib/rubocop/cop/sidekiq/symbol_argument.rb

Overview

This cop checks for symbols passed as arguments to a Sidekiq worker’s perform method. Symbols cannot be properly serialized for Redis and should be avoided. Use strings instead.

Examples:

# bad
MyWorker.perform_async(:foo)

# good
MyWorker.perform_async("foo")

# bad
MyWorker.perform_async(%i(foo))

# good
MyWorker.perform_async(%w(foo))

# bad
MyWorker.perform_async([:foo])

# good
MyWorker.perform_async(["foo"]))

# bad
MyWorker.perform_async(foo: 1)

# good
MyWorker.perform_async('foo' => 1)

# bad
MyWorker.perform_async('foo' => :baz)

# good
MyWorker.perform_async('foo' => "baz")

# bad
MyWorker.perform_async('foo' => [:bar]

# good
MyWorker.perform_async('foo' => ["baz"])

# bad
MyWorker.perform_async('foo' => %i(baz)

# good
MyWorker.perform_async('foo' => %w(baz))

# bad
MyWorker.perform_async('foo' => { bar: %i(baz) })

# good
MyWorker.perform_async('foo' => { bar: %w(baz) })

# bad
MyWorker.perform_async('foo' => { bar: [:baz]) })

# good
MyWorker.perform_async('foo' => { bar: ["baz"] })

Constant Summary collapse

MSG =
'Symbols are not native JSON types; use strings instead.'

Constants included from Helpers

Helpers::NODE_MATCHERS

Instance Method Summary collapse

Methods included from Helpers

#approve_node, #expand_array_node, #expand_hash_array_node, #expand_hash_node, #expand_node, #expand_nodes, #in_sidekiq_worker?, included, #node_approved?, #sidekiq_arguments, #within?

Instance Method Details

#on_send(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/sidekiq/symbol_argument.rb', line 74

def on_send(node)
  sidekiq_arguments(node)
    .lazy
    .select { |n| node_contains_symbol?(n) }
    .each do |selected_node|
    offense_data(selected_node) do |offense_node, replace_node, replace_value|
      add_offense(offense_node) do |corrector|
        corrector.replace(replace_node, replace_value)
      end
    end
  end
end