Class: RuboCop::Cop::RSpec::EmptyHook

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RuboCop::Cop::RangeHelp
Defined in:
lib/rubocop/cop/rspec/empty_hook.rb

Overview

Checks for empty before and after hooks.

Examples:

# 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 }

Constant Summary collapse

MSG =
'Empty hook detected.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#empty_hook?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/rspec/empty_hook.rb', line 33

def_node_matcher :empty_hook?, <<~PATTERN
  (block $(send nil? #Hooks.all ...) _ nil?)
PATTERN

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/rspec/empty_hook.rb', line 37

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.source_range, side: :left)
      )
    end
  end
end