Sha256: cb9eced6842990ac15ad7daf7ed18343e6597ee6ad8c31380a84a51fe895fa13

Contents?: true

Size: 1.44 KB

Versions: 53

Compression:

Stored size: 1.44 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) # rubocop:disable InternalAffairs/NumblockHandler
          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
      end
    end
  end
end

Version data entries

53 entries across 51 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/scattered_let.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/scattered_let.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/scattered_let.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/scattered_let.rb