Class: RuboCop::Cop::Sidekiq::PerformInline

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

Overview

Suggest to use ‘perform_inline` instead of `new.perform` for Sidekiq workers.

Constant Summary collapse

MSG =
'Use `perform_inline` instead of `new.perform`'
RESTRICT_ON_SEND =
%i[perform].freeze

Instance Method Summary collapse

Instance Method Details

#new_perform?(node) ⇒ Object



21
22
23
# File 'lib/rubocop/cop/sidekiq/perform_inline.rb', line 21

def_node_matcher :new_perform?, <<~PATTERN
  (send (send _ :new) :perform ...)
PATTERN

#on_send(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/sidekiq/perform_inline.rb', line 25

def on_send(node)
  return unless new_perform?(node)

  new_perform_node = node.source_range.with(
    begin_pos: node.receiver.receiver.source_range.end_pos + 1,
    end_pos: node.loc.selector.end_pos
  )

  add_offense(new_perform_node) do |corrector|
    corrector.replace(new_perform_node, 'perform_inline')
  end
end