Sha256: fcf74370b125c97af2617876aeac724da85218a1e90d8490cd18a3d145c4dd45

Contents?: true

Size: 1.29 KB

Versions: 36

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for the presence of `method_missing` without also
      # defining `respond_to_missing?`.
      #
      # @example
      #   #bad
      #   def method_missing(name, *args)
      #     # ...
      #   end
      #
      #   #good
      #   def respond_to_missing?(name, include_private)
      #     # ...
      #   end
      #
      #   def method_missing(name, *args)
      #     # ...
      #   end
      #
      class MissingRespondToMissing < Base
        MSG =
          'When using `method_missing`, define `respond_to_missing?`.'

        def on_def(node)
          return unless node.method?(:method_missing)
          return if implements_respond_to_missing?(node)

          add_offense(node)
        end
        alias on_defs on_def

        private

        def implements_respond_to_missing?(node)
          return false unless (grand_parent = node.parent.parent)

          grand_parent.each_descendant(node.type) do |descendant|
            return true if descendant.method?(:respond_to_missing?)

            child = descendant.children.first
            return true if child.respond_to?(:method?) && child.method?(:respond_to_missing?)
          end

          false
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.12.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.12.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.11.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.10.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.9.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.9.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.8.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.8.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.7.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.6.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.6.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.5.2 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.5.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.5.0 lib/rubocop/cop/style/missing_respond_to_missing.rb