From a30d548ec3c83783132653d6e600a1cf2dff9d30 Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Fri, 21 Jun 2024 14:36:41 -0400 Subject: [PATCH] better signal --- lib/rubocop/cop/sidekiq/perform_inline.rb | 17 ++++++++--------- spec/rubocop/cop/sidekiq/perform_inline_spec.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/rubocop/cop/sidekiq/perform_inline.rb b/lib/rubocop/cop/sidekiq/perform_inline.rb index 71306c7..39fba48 100644 --- a/lib/rubocop/cop/sidekiq/perform_inline.rb +++ b/lib/rubocop/cop/sidekiq/perform_inline.rb @@ -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 diff --git a/spec/rubocop/cop/sidekiq/perform_inline_spec.rb b/spec/rubocop/cop/sidekiq/perform_inline_spec.rb index 96d1bb6..f913a84 100644 --- a/spec/rubocop/cop/sidekiq/perform_inline_spec.rb +++ b/spec/rubocop/cop/sidekiq/perform_inline_spec.rb @@ -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