Sha256: 163e6e26b5c3cf28ae59237881a1b4770dac3d4ce2431f7dc533087dedb705ba

Contents?: true

Size: 1.27 KB

Versions: 20

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check for expectations where `be(...)` can replace `eq(...)`.
      #
      # The `be` matcher compares by identity while the `eq` matcher compares
      # using `==`. Booleans and nil can be compared by identity and therefore
      # the `be` matcher is preferable as it is a more strict test.
      #
      # @safety
      #   This cop is unsafe because it changes how values are compared.
      #
      # @example
      #   # bad
      #   expect(foo).to eq(true)
      #   expect(foo).to eq(false)
      #   expect(foo).to eq(nil)
      #
      #   # good
      #   expect(foo).to be(true)
      #   expect(foo).to be(false)
      #   expect(foo).to be(nil)
      #
      class BeEq < Base
        extend AutoCorrector

        MSG = 'Prefer `be` over `eq`.'
        RESTRICT_ON_SEND = %i[eq].freeze

        # @!method eq_type_with_identity?(node)
        def_node_matcher :eq_type_with_identity?, <<-PATTERN
          (send nil? :eq {true false nil})
        PATTERN

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

          add_offense(node.loc.selector) do |corrector|
            corrector.replace(node.loc.selector, 'be')
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/be_eq.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_eq.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_eq.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_eq.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.17.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.16.0 lib/rubocop/cop/rspec/be_eq.rb