Sha256: 1a994f13af1703af5df50ae0cefc64f36ab5d9c5caa540f358a653a4ee13d5d8

Contents?: true

Size: 1.46 KB

Versions: 38

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Remove redundant `around` hook.
      #
      # @example
      #   # bad
      #   around do |example|
      #     example.run
      #   end
      #
      #   # good
      #
      class RedundantAround < Base
        extend AutoCorrector

        MSG = 'Remove redundant `around` hook.'

        RESTRICT_ON_SEND = %i[around].freeze

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

          add_offense(node) do |corrector|
            autocorrect(corrector, node)
          end
        end
        alias on_numblock on_block

        def on_send(node)
          return unless match_redundant_around_hook_send?(node)

          add_offense(node) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        # @!method match_redundant_around_hook_block?(node)
        def_node_matcher :match_redundant_around_hook_block?, <<~PATTERN
          ({block numblock} (send _ :around ...) ... (send _ :run))
        PATTERN

        # @!method match_redundant_around_hook_send?(node)
        def_node_matcher :match_redundant_around_hook_send?, <<~PATTERN
          (send
            _
            :around
            ...
            (block-pass
              (sym :run)
            )
          )
        PATTERN

        def autocorrect(corrector, node)
          corrector.remove(node)
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 7 rubygems

Version Path
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/redundant_around.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/redundant_around.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/redundant_around.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/redundant_around.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/redundant_around.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/redundant_around.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/redundant_around.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/redundant_around.rb