Sha256: 568e7049b90bfc71f0919718ca63e73ac58bd60f3a13995cfaf0ea15e9ea7162

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks for missing `numblock` handlers. The blocks with numbered
      # arguments introduced in Ruby 2.7 are parsed with a node type of
      # `numblock` instead of block. Cops that define `block` handlers
      # need to define `numblock` handlers or disable this cope for them.
      #
      # @example
      #
      #   # bad
      #   class BlockRelatedCop < Base
      #     def on_block(node)
      #     end
      #   end
      #
      #   # good
      #   class BlockRelatedCop < Base
      #     def on_block(node)
      #     end
      #
      #     alias on_numblock on_block
      #   end
      #
      #   class BlockRelatedCop < Base
      #     def on_block(node)
      #     end
      #
      #     alias_method :on_numblock, :on_block
      #   end
      #
      #   class BlockRelatedCop < Base
      #     def on_block(node)
      #     end
      #
      #     def on_numblock(node)
      #     end
      #   end
      class NumblockHandler < Base
        MSG = 'Define on_numblock to handle blocks with numbered arguments.'

        def on_def(node)
          return unless block_handler?(node)
          return unless node.parent

          add_offense(node) unless numblock_handler?(node.parent)
        end

        private

        # @!method block_handler?(node)
        def_node_matcher :block_handler?, <<~PATTERN
          (def :on_block (args (arg :node)) ...)
        PATTERN

        # @!method numblock_handler?(node)
        def_node_matcher :numblock_handler?, <<~PATTERN
          {
            `(def :on_numblock (args (arg :node)) ...)
            `(alias (sym :on_numblock) (sym :on_block))
            `(send nil? :alias_method (sym :on_numblock) (sym :on_block))
          }
        PATTERN
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb