Sha256: 7c569958a59a092db718c8cb76c1419df9be3826d4a21db6918e3a04e0e4caf3

Contents?: true

Size: 1.2 KB

Versions: 13

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # This cop checks void `expect()`.
      #
      # @example
      #   # bad
      #   expect(something)
      #
      #   # good
      #   expect(something).to be(1)
      class VoidExpect < Cop
        MSG = 'Do not use `expect()` without `.to` or `.not_to`. ' \
              'Chain the methods or remove it.'.freeze

        def_node_matcher :expect?, <<-PATTERN
          (send nil? :expect ...)
        PATTERN

        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)
          return unless expect_block?(node)
          check_expect(node)
        end

        private

        def check_expect(node)
          return unless void?(node)
          add_offense(node, location: :expression)
        end

        def void?(expect)
          parent = expect.parent
          return true unless parent
          return true if parent.begin_type?
          return true if parent.block_type? && parent.children[2] == expect
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/void_expect.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/void_expect.rb