Skip to content

Commit

Permalink
better signal
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Jun 21, 2024
1 parent 3487db4 commit a30d548
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/rubocop/cop/sidekiq/perform_inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ module Sidekiq
#
class PerformInline < Base
extend AutoCorrector
MSG = 'Use `perform_inline` instead of `new.perform`.'
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)
(send (send _ :new) :perform ...)
PATTERN

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

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

def autocorrect(node)
lambda do |corrector|
receiver, _method_name = *node
corrector.replace(receiver.source_range,
receiver.source_range.source.gsub('new.perform', 'perform_inline'))
add_offense(new_perform_node) do |corrector|
corrector.replace(new_perform_node, 'perform_inline')
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/sidekiq/perform_inline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
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
Expand Down

0 comments on commit a30d548

Please sign in to comment.