Sha256: 6c120ccd07444af4b7bead781a9c07f61ad52b663da2a926d46153a29c741f5d

Contents?: true

Size: 1.27 KB

Versions: 34

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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

34 entries across 34 versions & 8 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.68.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.67.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.66.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.66.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.65.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.65.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.64.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.63.4 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.63.3 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.63.2 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.63.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.63.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.62.1 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.62.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
rubocop-1.61.0 lib/rubocop/cop/style/missing_respond_to_missing.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/missing_respond_to_missing.rb