Sha256: aa2491cfe9a62a9442d3608b1b6b03ece6110f60c80b022a878275d6af6cd2c0

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 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.'

        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)
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/scattered_let.rb