lib/rubocop/cop/rspec/let_before_examples.rb in rubocop-rspec-2.13.2 vs lib/rubocop/cop/rspec/let_before_examples.rb in rubocop-rspec-2.14.0
- old
+ new
@@ -41,30 +41,44 @@
#{block_pattern('{#ExampleGroups.all #Examples.all}')}
#{send_pattern('#Includes.examples')}
}
PATTERN
+ # @!method include_examples?(node)
+ def_node_matcher :include_examples?, <<~PATTERN
+ {
+ #{block_pattern(':include_examples')}
+ #{send_pattern(':include_examples')}
+ }
+ PATTERN
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
return unless example_group_with_body?(node)
check_let_declarations(node.body) if multiline_block?(node.body)
end
private
+ def example_group_with_include_examples?(body)
+ body.children.any? { |sibling| include_examples?(sibling) }
+ end
+
def multiline_block?(block)
block.begin_type?
end
def check_let_declarations(node)
first_example = find_first_example(node)
return unless first_example
+ correct = !example_group_with_include_examples?(node)
+
first_example.right_siblings.each do |sibling|
next unless let?(sibling)
add_offense(sibling) do |corrector|
- autocorrect(corrector, sibling, first_example)
+ autocorrect(corrector, sibling, first_example) if correct
end
end
end
def find_first_example(node)