Skip to content

Commit

Permalink
Add Sidekiq/PerformInline
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Jun 21, 2024
1 parent 8e64201 commit 3487db4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ Sidekiq/NoNilReturn:
Include:
- app/workers/**/*

Sidekiq/PerformInline:
Description: 'Suggest to use `perform_inline` instead of `new.perform` for Sidekiq workers.'
Enabled: true

Sidekiq/SymbolArgument:
Description: "Prevent passing keywords arguments in worker's perform method"
Enabled: true
41 changes: 41 additions & 0 deletions lib/rubocop/cop/sidekiq/perform_inline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Sidekiq
# Suggest to use `perform_inline` instead of `new.perform` for Sidekiq workers.
#
# @bad
# MyWorker.new.perform
#
# @good
# MyWorker.perform_inline
#
class PerformInline < Base
extend AutoCorrector
MSG = 'Use `perform_inline` instead of `new.perform`.'

RESTRICT_ON_SEND = %i[perform].freeze

# @!method new_perform?(node)
def_node_matcher :new_perform?, <<~PATTERN
(send (send (const nil? _) :new) :perform)
PATTERN

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

add_offense(node)
end

def autocorrect(node)
lambda do |corrector|
receiver, _method_name = *node
corrector.replace(receiver.source_range,
receiver.source_range.source.gsub('new.perform', 'perform_inline'))
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions spec/rubocop/cop/sidekiq/perform_inline_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Sidekiq::PerformInline, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when using `new.perform`' do
expect_offense(<<~RUBY)
MyWorker.new.perform
^^^^^^^^^^^ Sidekiq/PerformInline: Use `perform_inline` instead of `new.perform`
RUBY

expect_correction(<<~RUBY)
MyWorker.perform_inline
RUBY
end

it 'does not register an offense when using `#good_method`' do
expect_no_offenses(<<~RUBY)
MyWorker.perform_inline
RUBY
end
end

0 comments on commit 3487db4

Please sign in to comment.