Sha256: 4fe8b0019d59a7ec8c54cdb08398ad9879b37014f3063c503d29fb0d93fa0f46

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

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

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

          analyzable_hooks(node).each do |repeated_hook|
            add_offense(repeated_hook)
          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

6 entries across 6 versions & 1 rubygems

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