Sha256: d9215d5dc967fb50cd61b6253ce26263e1a26d3394bd89b5f42221841af063b0

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 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, :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

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.15.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.15.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.14.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.13.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.12.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.11.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-1.10.0 lib/rubocop/cop/rspec/scattered_setup.rb