forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathempty_hook.rb
49 lines (45 loc) · 1.13 KB
/
empty_hook.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for empty before and after hooks.
#
# @example
# # bad
# before {}
# after do; end
# before(:all) do
# end
# after(:all) { }
#
# # good
# before { create_users }
# after do
# cleanup_users
# end
# before(:all) do
# create_feed
# end
# after(:all) { cleanup_feed }
#
class EmptyHook < Base
extend AutoCorrector
include RuboCop::Cop::RangeHelp
MSG = 'Empty hook detected.'
# @!method empty_hook?(node)
def_node_matcher :empty_hook?, <<~PATTERN
(block $#{send_pattern('#Hooks.all')} _ nil?)
PATTERN
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
empty_hook?(node) do |hook|
add_offense(hook) do |corrector|
corrector.remove(
range_with_surrounding_space(node.loc.expression, side: :left)
)
end
end
end
end
end
end
end