lib/rubocop/cop/rspec/hooks_before_examples.rb in rubocop-rspec-2.12.1 vs lib/rubocop/cop/rspec/hooks_before_examples.rb in rubocop-rspec-2.13.0
- old
+ new
@@ -4,20 +4,19 @@
module Cop
module RSpec
# Checks for before/around/after hooks that come after an example.
#
# @example
- # # Bad
- #
+ # # bad
# it 'checks what foo does' do
# expect(foo).to be
# end
#
# before { prepare }
# after { clean_up }
#
- # # Good
+ # # good
# before { prepare }
# after { clean_up }
#
# it 'checks what foo does' do
# expect(foo).to be
@@ -30,36 +29,38 @@
# @!method example_or_group?(node)
def_node_matcher :example_or_group?, <<-PATTERN
{
#{block_pattern('{#ExampleGroups.all #Examples.all}')}
+ #{numblock_pattern('{#ExampleGroups.all #Examples.all}')}
#{send_pattern('#Includes.examples')}
}
PATTERN
def on_block(node)
return unless example_group_with_body?(node)
check_hooks(node.body) if multiline_block?(node.body)
end
+ alias on_numblock on_block
+
private
def multiline_block?(block)
block.begin_type?
end
def check_hooks(node)
first_example = find_first_example(node)
return unless first_example
- node.each_child_node do |child|
- next if child.sibling_index < first_example.sibling_index
- next unless hook?(child)
+ first_example.right_siblings.each do |sibling|
+ next unless hook?(sibling)
- msg = format(MSG, hook: child.method_name)
- add_offense(child, message: msg) do |corrector|
- autocorrect(corrector, child, first_example)
+ msg = format(MSG, hook: sibling.method_name)
+ add_offense(sibling, message: msg) do |corrector|
+ autocorrect(corrector, sibling, first_example)
end
end
end
def find_first_example(node)