Sha256: 6fd0e658327b97a73a269390419d0b34a970746f59a9caa5a4914496b94b1dfd

Contents?: true

Size: 1.94 KB

Versions: 24

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check for expectations where `be(...)` can replace `eql(...)`.
      #
      # The `be` matcher compares by identity while the `eql` matcher
      # compares using `eql?`. Integers, floats, booleans, symbols, 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 eql(1)
      #   expect(foo).to eql(1.0)
      #   expect(foo).to eql(true)
      #   expect(foo).to eql(false)
      #   expect(foo).to eql(:bar)
      #   expect(foo).to eql(nil)
      #
      #   # good
      #   expect(foo).to be(1)
      #   expect(foo).to be(1.0)
      #   expect(foo).to be(true)
      #   expect(foo).to be(false)
      #   expect(foo).to be(:bar)
      #   expect(foo).to be(nil)
      #
      # This cop only looks for instances of `expect(...).to eql(...)`. We
      # do not check `to_not` or `not_to` since `!eql?` is more strict
      # than `!equal?`. We also do not try to flag `eq` because if
      # `a == b`, and `b` is comparable by identity, `a` is still not
      # necessarily the same type as `b` since the `#==` operator can
      # coerce objects for comparison.
      #
      class BeEql < Base
        extend AutoCorrector

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

        # @!method eql_type_with_identity(node)
        def_node_matcher :eql_type_with_identity, <<~PATTERN
          (send _ :to $(send nil? :eql {true false int float sym nil}))
        PATTERN

        def on_send(node)
          eql_type_with_identity(node) do |eql|
            add_offense(eql.loc.selector) do |corrector|
              corrector.replace(eql.loc.selector, 'be')
            end
          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_eql.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/be_eql.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/be_eql.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/be_eql.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/be_eql.rb