Class: RuboCop::Cop::RSpec::HooksBeforeExamples

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/hooks_before_examples.rb

Overview

Checks for before/around/after hooks that come after an example.

Examples:

# bad
it 'checks what foo does' do
  expect(foo).to be
end

before { prepare }
after { clean_up }

# good
before { prepare }
after { clean_up }

it 'checks what foo does' do
  expect(foo).to be
end

Constant Summary collapse

MSG =
'Move `%<hook>s` above the examples in the group.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

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

#example_or_group?(node) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/rspec/hooks_before_examples.rb', line 31

def_node_matcher :example_or_group?, <<~PATTERN
  {
    ({block numblock} {
      (send #rspec? #ExampleGroups.all ...)
      (send nil? #Examples.all ...)
    } ...)
    (send nil? #Includes.examples ...)
  }
PATTERN

#on_block(node) ⇒ Object Also known as: on_numblock



41
42
43
44
45
# File 'lib/rubocop/cop/rspec/hooks_before_examples.rb', line 41

def on_block(node)
  return unless example_group_with_body?(node)

  check_hooks(node.body) if multiline_block?(node.body)
end