class RuboCop::Cop::RSpec::LetBeforeExamples

Checks for `let` definitions that come after an example.

@example

# Bad
let(:foo) { bar }

it 'checks what foo does' do
  expect(foo).to be
end

let(:some) { other }

it 'checks what some does' do
  expect(some).to be
end

# Good
let(:foo) { bar }
let(:some) { other }

it 'checks what foo does' do
  expect(foo).to be
end

it 'checks what some does' do
  expect(some).to be
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/let_before_examples.rb, line 43
def on_block(node)
  return unless example_group_with_body?(node)

  check_let_declarations(node.body) if multiline_block?(node.body)
end

Private Instance Methods

check_let_declarations(node) click to toggle source
# File lib/rubocop/cop/rspec/let_before_examples.rb, line 55
def check_let_declarations(node)
  example_found = false

  node.each_child_node do |child|
    if let?(child)
      add_offense(child, location: :expression) if example_found
    elsif example_or_group?(child)
      example_found = true
    end
  end
end
multiline_block?(block) click to toggle source
# File lib/rubocop/cop/rspec/let_before_examples.rb, line 51
def multiline_block?(block)
  block.begin_type?
end