Sha256: a2c47080d2829cfa26ac1eddbadede666f0b380bc3a24e0e669c52af14f7f242

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for setup scattered across multiple hooks in an example group.
      #
      # Unify `before`, `after`, and `around` hooks when possible.
      #
      # @example
      #   # bad
      #   describe Foo do
      #     before { setup1 }
      #     before { setup2 }
      #   end
      #
      #   # good
      #   describe Foo do
      #     before do
      #       setup1
      #       setup2
      #     end
      #   end
      #
      class ScatteredSetup < Cop
        MSG = 'Do not define multiple hooks in the same example group.'.freeze

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

          analyzable_hooks(node).each do |repeated_hook|
            add_offense(repeated_hook, location: :expression)
          end
        end

        def analyzable_hooks(node)
          RuboCop::RSpec::ExampleGroup.new(node)
            .hooks
            .select { |hook| hook.knowable_scope? && hook.valid_scope? }
            .group_by { |hook| [hook.name, hook.scope] }
            .values
            .reject(&:one?)
            .flatten
            .map(&:to_node)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/scattered_setup.rb