class RuboCop::Cop::RSpec::LetBeforeExamples
end
expect(some).to be
it ‘checks what some does’ do
end
expect(foo).to be
it ‘checks what foo does’ do
let(:some) { other }
let(:foo) { bar }
# good
end
expect(some).to be
it ‘checks what some does’ do
let(:some) { other }
end
expect(foo).to be
it ‘checks what foo does’ do
let(:foo) { bar }
# bad
@example
Checks for ‘let` definitions that come after an example.
def self.autocorrect_incompatible_with
def self.autocorrect_incompatible_with [RSpec::ScatteredLet] end
def autocorrect(corrector, node, first_example)
def autocorrect(corrector, node, first_example) RuboCop::RSpec::Corrector::MoveNode.new( node, corrector, processed_source ).move_before(first_example) end
def check_let_declarations(node)
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) if correct end end end
def example_group_with_include_examples?(body)
def example_group_with_include_examples?(body) body.children.any? { |sibling| include_examples?(sibling) } 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) # rubocop:disable InternalAffairs/NumblockHandler
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