Sha256: 9132819534ec78cb4bbc7bc2a423f7ad3a0677a4bca39cb4ac527d55676f1851

Contents?: true

Size: 1.49 KB

Versions: 26

Compression:

Stored size: 1.49 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 < Base
        extend AutoCorrector

        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) do |corrector|
              RuboCop::RSpec::Corrector::MoveNode.new(
                node, corrector, processed_source
              ).move_after(first_let)
            end
          end
        end

        def find_first_let(node)
          node.children.find { |child| let?(child) }
        end
      end
    end
  end
end

Version data entries

26 entries across 24 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/scattered_let.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/scattered_let.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/scattered_let.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.2.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.1.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/scattered_let.rb