Sha256: 9cde175260f7727bec901b4d3b78a715703b6182b747b7aa28ad4f0b75a5df48

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks invalid usage for predicate matcher.
      #
      # Predicate matcher does not need a question.
      # This cop checks an unnecessary question in predicate matcher.
      #
      # @example
      #
      #   # bad
      #   expect(foo).to be_something?
      #
      #   # good
      #   expect(foo).to be_something
      class InvalidPredicateMatcher < Base
        MSG = 'Omit `?` from `%<matcher>s`.'

        def_node_matcher :invalid_predicate_matcher?, <<-PATTERN
          (send (send nil? :expect ...) #{Runners::ALL.node_pattern_union} $(send nil? #predicate?))
        PATTERN

        def on_send(node)
          invalid_predicate_matcher?(node) do |predicate|
            add_offense(predicate,
                        message: format(MSG, matcher: predicate.method_name))
          end
        end

        private

        def predicate?(name)
          name = name.to_s
          name.start_with?('be_', 'have_') && name.end_with?('?')
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-rspec-2.0.0.pre lib/rubocop/cop/rspec/invalid_predicate_matcher.rb
rubocop-rspec-1.44.1 lib/rubocop/cop/rspec/invalid_predicate_matcher.rb
rubocop-rspec-1.44.0 lib/rubocop/cop/rspec/invalid_predicate_matcher.rb
rubocop-rspec-1.43.2 lib/rubocop/cop/rspec/invalid_predicate_matcher.rb
rubocop-rspec-1.43.1 lib/rubocop/cop/rspec/invalid_predicate_matcher.rb
rubocop-rspec-1.43.0 lib/rubocop/cop/rspec/invalid_predicate_matcher.rb