Sha256: d72568dec0b749ac0131894c5fb38964b7a296832040e0c8f32457e46842ba2e

Contents?: true

Size: 1.06 KB

Versions: 16

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer `not_to receive(...)` over `receive(...).never`.
      #
      # @example
      #
      #     # bad
      #     expect(foo).to receive(:bar).never
      #
      #     # good
      #     expect(foo).not_to receive(:bar)
      #
      class ReceiveNever < Base
        extend AutoCorrector
        MSG = 'Use `not_to receive` instead of `never`.'
        RESTRICT_ON_SEND = %i[never].freeze

        # @!method method_on_stub?(node)
        def_node_search :method_on_stub?, '(send nil? :receive ...)'

        def on_send(node)
          return unless node.method?(:never) && method_on_stub?(node)

          add_offense(node.loc.selector) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, node)
          corrector.replace(node.parent.loc.selector, 'not_to')
          range = node.loc.dot.with(end_pos: node.loc.selector.end_pos)
          corrector.remove(range)
        end
      end
    end
  end
end

Version data entries

16 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/receive_never.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/receive_never.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/receive_never.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/receive_never.rb