Sha256: 0ca02fe2ab335a096984bb8b629c6c5eb9bc4e7b94d46667cb98560cc6c15856

Contents?: true

Size: 1.78 KB

Versions: 21

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for redundant predicate matcher.
      #
      # @example
      #   # bad
      #   expect(foo).to be_exist(bar)
      #   expect(foo).not_to be_include(bar)
      #   expect(foo).to be_all(bar)
      #
      #   # good
      #   expect(foo).to exist(bar)
      #   expect(foo).not_to include(bar)
      #   expect(foo).to all be(bar)
      #
      class RedundantPredicateMatcher < Base
        extend AutoCorrector

        MSG = 'Use `%<good>s` instead of `%<bad>s`.'
        RESTRICT_ON_SEND =
          %i[be_all be_cover be_end_with be_eql be_equal
             be_exist be_exists be_include be_match
             be_respond_to be_start_with].freeze

        def on_send(node)
          return if node.parent.block_type? || node.arguments.empty?
          return unless replaceable_arguments?(node)

          method_name = node.method_name.to_s
          replaced = replaced_method_name(method_name)
          add_offense(node, message: message(method_name,
                                             replaced)) do |corrector|
            unless node.method?(:be_all)
              corrector.replace(node.loc.selector, replaced)
            end
          end
        end

        private

        def message(bad_method, good_method)
          format(MSG, bad: bad_method, good: good_method)
        end

        def replaceable_arguments?(node)
          if node.method?(:be_all)
            node.first_argument.send_type?
          else
            true
          end
        end

        def replaced_method_name(method_name)
          name = method_name.to_s.delete_prefix('be_')
          if name == 'exists'
            'exist'
          else
            name
          end
        end
      end
    end
  end
end

Version data entries

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