Sha256: 3d7df706c40a5003d957356f7be2257401c69474a23c2a82d005eff67085de58

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 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)

          _describe, _args, body = *node

          check_let_declarations(body)
        end

        def check_let_declarations(node)
          let_found = false
          mix_found = false

          node.each_child_node do |child|
            if let?(child)
              add_offense(child, :expression) if mix_found
              let_found = true
            elsif let_found
              mix_found = true
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.15.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-1.15.0 lib/rubocop/cop/rspec/scattered_let.rb