Sha256: 4a54eb1a6da9381df180acfaf1b5c459ad004db851092d88bd96e41bae362095

Contents?: true

Size: 1.81 KB

Versions: 175

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

175 entries across 170 versions & 17 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/numblock_handler.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/numblock_handler.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/numblock_handler.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/numblock_handler.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/numblock_handler.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/numblock_handler.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/internal_affairs/numblock_handler.rb
rubocop-1.62.1 lib/rubocop/cop/internal_affairs/numblock_handler.rb