forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredundant_around.rb
65 lines (54 loc) · 1.46 KB
/
redundant_around.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Remove redundant `around` hook.
#
# @example
# # bad
# around do |example|
# example.run
# end
#
# # good
#
class RedundantAround < Base
extend AutoCorrector
MSG = 'Remove redundant `around` hook.'
RESTRICT_ON_SEND = %i[around].freeze
def on_block(node)
return unless match_redundant_around_hook_block?(node)
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
alias on_numblock on_block
def on_send(node)
return unless match_redundant_around_hook_send?(node)
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
private
# @!method match_redundant_around_hook_block?(node)
def_node_matcher :match_redundant_around_hook_block?, <<~PATTERN
({block numblock} (send _ :around ...) ... (send _ :run))
PATTERN
# @!method match_redundant_around_hook_send?(node)
def_node_matcher :match_redundant_around_hook_send?, <<~PATTERN
(send
_
:around
...
(block-pass
(sym :run)
)
)
PATTERN
def autocorrect(corrector, node)
corrector.remove(node)
end
end
end
end
end