Sha256: 10ce78f575c9b5cc118de9e30bf1d8db17b80462f31bd2693a970f005fcdc18f

Contents?: true

Size: 1.32 KB

Versions: 22

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks void `expect()`.
      #
      # @example
      #   # bad
      #   expect(something)
      #
      #   # good
      #   expect(something).to be(1)
      #
      class VoidExpect < Base
        MSG = 'Do not use `expect()` without `.to` or `.not_to`. ' \
              'Chain the methods or remove it.'
        RESTRICT_ON_SEND = %i[expect].freeze

        # @!method expect?(node)
        def_node_matcher :expect?, <<~PATTERN
          (send nil? :expect ...)
        PATTERN

        # @!method expect_block?(node)
        def_node_matcher :expect_block?, <<~PATTERN
          (block #expect? (args) _body)
        PATTERN

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

          check_expect(node)
        end

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless expect_block?(node)

          check_expect(node)
        end

        private

        def check_expect(node)
          return unless void?(node)

          add_offense(node)
        end

        def void?(expect)
          parent = expect.parent
          return true unless parent
          return true if parent.begin_type?

          parent.block_type? && parent.body == expect
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/void_expect.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/void_expect.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/void_expect.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/void_expect.rb