lib/rubocop/cop/rspec/let_before_examples.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/let_before_examples.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -29,10 +29,12 @@
#
# it 'checks what some does' do
# expect(some).to be
# end
class LetBeforeExamples < Cop
+ extend AutoCorrector
+
MSG = 'Move `let` before the examples in the group.'
def_node_matcher :example_or_group?, <<-PATTERN
{
#{(Examples::ALL + ExampleGroups::ALL).block_pattern}
@@ -44,19 +46,10 @@
return unless example_group_with_body?(node)
check_let_declarations(node.body) if multiline_block?(node.body)
end
- 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
-
private
def multiline_block?(block)
block.begin_type?
end
@@ -65,16 +58,25 @@
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 let?(child)
- add_offense(child) if let?(child)
+ add_offense(child) do |corrector|
+ autocorrect(corrector, child, first_example)
+ end
end
end
def find_first_example(node)
node.children.find { |sibling| example_or_group?(sibling) }
+ end
+
+ def autocorrect(corrector, node, first_example)
+ RuboCop::RSpec::Corrector::MoveNode.new(
+ node, corrector, processed_source
+ ).move_before(first_example)
end
end
end
end
end