class RuboCop::Cop::RSpec::ScatteredLet

Checks for let scattered across the example group.

Group lets together

@example

# bad
describe Foo do
  let(:foo) { 1 }
  subject { Foo }
  let(:bar) { 2 }
  before { prepare }
  let!(:baz) { 3 }
end

# good
describe Foo do
  subject { Foo }
  before { prepare }
  let(:foo) { 1 }
  let(:bar) { 2 }
  let!(:baz) { 3 }
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/scattered_let.rb, line 34
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return unless example_group_with_body?(node)

  check_let_declarations(node.body)
end

Private Instance Methods

check_let_declarations(body) click to toggle source
# File lib/rubocop/cop/rspec/scattered_let.rb, line 42
def check_let_declarations(body)
  lets = body.each_child_node.select { |node| let?(node) }

  first_let = lets.first
  lets.each_with_index do |node, idx|
    next if node.sibling_index == first_let.sibling_index + idx

    add_offense(node) do |corrector|
      RuboCop::RSpec::Corrector::MoveNode.new(
        node, corrector, processed_source
      ).move_after(first_let)
    end
  end
end