Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sidekiq/PerformInline #82

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# main

* Add Add Sidekiq/PerformInline ([#82](https://github.com/petalmd/rubocop-petal/pull/82))

# v1.4.0 (2024-06-14)

* Set `Rails/Date`, `AllowToTime` config to false. ([#80](https://github.com/petalmd/rubocop-petal/pull/80))
Expand Down
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
40 changes: 40 additions & 0 deletions lib/rubocop/cop/sidekiq/perform_inline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 _ :new) :perform ...)
PATTERN

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
end
end
end
end
38 changes: 38 additions & 0 deletions spec/rubocop/cop/sidekiq/perform_inline_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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

expect_offense(<<~RUBY)
MyWorker.new.perform(123, 'abc')
^^^^^^^^^^^ Sidekiq/PerformInline: Use `perform_inline` instead of `new.perform`
RUBY

expect_correction(<<~RUBY)
MyWorker.perform_inline(123, 'abc')
RUBY

expect_offense(<<~RUBY)
MyWorker
.new
^^^^^^ Sidekiq/PerformInline: Use `perform_inline` instead of `new.perform`
.perform(123, 'abc')
RUBY
end

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