class RuboCop::Cop::RSpec::HooksBeforeExamples
end
expect(foo).to be
it ‘checks what foo does’ do
after { clean_up }
before { prepare }
# Good
after { clean_up }
before { prepare }
end
expect(foo).to be
it ‘checks what foo does’ do
# Bad
@example
Checks for before/around/after hooks that come after an example.
def autocorrect(node)
def autocorrect(node) lambda do |corrector| first_example = find_first_example(node.parent) RuboCop::RSpec::Corrector::MoveNode.new( node, corrector, processed_source ).move_before(first_example) end end
def check_hooks(node)
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) add_offense( child, message: format(MSG, hook: child.method_name) ) end end
def find_first_example(node)
def find_first_example(node) node.children.find { |sibling| example_or_group?(sibling) } end
def multiline_block?(block)
def multiline_block?(block) block.begin_type? end
def on_block(node)
def on_block(node) return unless example_group_with_body?(node) check_hooks(node.body) if multiline_block?(node.body) end