forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_method.rb
53 lines (47 loc) · 1.18 KB
/
context_method.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# `context` should not be used for specifying methods.
#
# @example
# # bad
# context '#foo_bar' do
# # ...
# end
#
# context '.foo_bar' do
# # ...
# end
#
# # good
# describe '#foo_bar' do
# # ...
# end
#
# describe '.foo_bar' do
# # ...
# end
#
class ContextMethod < Base
extend AutoCorrector
MSG = 'Use `describe` for testing methods.'
# @!method context_method(node)
def_node_matcher :context_method, <<-PATTERN
(block (send #rspec? :context $(str #method_name?) ...) ...)
PATTERN
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
context_method(node) do |context|
add_offense(context) do |corrector|
corrector.replace(node.send_node.loc.selector, 'describe')
end
end
end
private
def method_name?(description)
description.start_with?('.', '#')
end
end
end
end
end