Sha256: ac593cc13898f029803c7ac3d5be51e4ee8fc43f6e09ed7617ffcde6e305f9a1

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 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
            (send _ :around ...)
            (args _?)
            (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

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/redundant_around.rb