Sha256: 0d3f9a854430588911c8abf334c6a3efb7a66ac2a318818e1342f651c047c8b0

Contents?: true

Size: 1.97 KB

Versions: 16

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for before/around/after hooks that come after an example.
      #
      # @example
      #   # Bad
      #
      #   it 'checks what foo does' do
      #     expect(foo).to be
      #   end
      #
      #   before { prepare }
      #   after { clean_up }
      #
      #   # Good
      #   before { prepare }
      #   after { clean_up }
      #
      #   it 'checks what foo does' do
      #     expect(foo).to be
      #   end
      #
      class HooksBeforeExamples < Base
        extend AutoCorrector

        MSG = 'Move `%<hook>s` above the examples in the group.'

        # @!method example_or_group?(node)
        def_node_matcher :example_or_group?, <<-PATTERN
          {
            #{block_pattern('{#ExampleGroups.all #Examples.all}')}
            #{send_pattern('#Includes.examples')}
          }
        PATTERN

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

          check_hooks(node.body) if multiline_block?(node.body)
        end

        private

        def multiline_block?(block)
          block.begin_type?
        end

        def check_hooks(node)
          first_example = find_first_example(node)
          return unless first_example

          node.each_child_node do |child|
            next if child.sibling_index < first_example.sibling_index
            next unless hook?(child)

            msg = format(MSG, hook: child.method_name)
            add_offense(child, message: msg) do |corrector|
              autocorrect(corrector, child, first_example)
            end
          end
        end

        def find_first_example(node)
          node.children.find { |sibling| example_or_group?(sibling) }
        end

        def autocorrect(corrector, node, first_example)
          RuboCop::RSpec::Corrector::MoveNode.new(
            node, corrector, processed_source
          ).move_before(first_example)
        end
      end
    end
  end
end

Version data entries

16 entries across 14 versions & 2 rubygems

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