Sha256: e1191986c38f67e9e805d80497e28a7bed96e5904057fc369728f821d8324482

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # 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
      #
      class ScatteredLet < Cop
        MSG = 'Group all let/let! blocks in the example group together.'.freeze

        def_node_matcher :let?, '(block (send nil? {:let :let!} ...) ...)'

        def on_block(node)
          return unless example_group_with_body?(node)

          check_let_declarations(node.body)
        end

        private

        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, location: :expression)
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/scattered_let.rb