-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
memoized_helpers_in_example_group.rb
80 lines (75 loc) · 2 KB
/
memoized_helpers_in_example_group.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for usage of `let` blocks in specs.
#
# This cop can be configured with the option `AllowSubject` which
# will configure the cop to only register offenses on explicit calls to
# `let` and not calls to `subject`
#
# @example
# # bad
# describe MyClass do
# let(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# describe MyClass do
# subject(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# # good
# describe MyClass do
# it do
# foo = []
# expect(foo).to be_empty
# end
# end
#
# @example with AllowSubject configuration
#
# # rubocop.yml
# # RSpec/MemoizedHelpersInExampleGroup:
# # AllowSubject: true
#
# # bad
# describe MyClass do
# let(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# # good
# describe MyClass do
# subject(:foo) { [] }
# it { expect(foo).to be_empty }
# end
#
# describe MyClass do
# it do
# foo = []
# expect(foo).to be_empty
# end
# end
#
class MemoizedHelpersInExampleGroup < Cop
MSG = 'Avoid using `%<method>s` ' \
'– use a method call or local variable instead.'
def_node_matcher :let?, Helpers::ALL.send_pattern
def_node_matcher :subject?, Subject::ALL.send_pattern
def on_send(node)
if subject?(node) && !allow_subject?
add_offense(node, message: format(MSG, method: 'subject'))
elsif let?(node)
add_offense(node, message: format(MSG, method: 'let'))
end
end
private
def allow_subject?
cop_config['AllowSubject']
end
end
end
end
end