Sha256: df154a23d940e00f68340c3b1913556d0a7eaec8a7256f895570983887ba59a8

Contents?: true

Size: 1.27 KB

Versions: 24

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

24 entries across 24 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/be_eq.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/be_eq.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/be_eq.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/be_eq.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/be_eq.rb