forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invalid_predicate_matcher.rb
44 lines (38 loc) · 1.08 KB
/
invalid_predicate_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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 < Cop
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)
end
end
private
def predicate?(name)
name = name.to_s
name.start_with?('be_', 'have_') && name.end_with?('?')
end
def message(predicate)
format(MSG, matcher: predicate.method_name)
end
end
end
end
end