Sha256: 38c1ab842a5ad4d0e54f68b4480d8307cf6fb495dff8b089067dd4b5d9e1797c

Contents?: true

Size: 1.21 KB

Versions: 39

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks that method names are checked using `method?` method.
      #
      # @example
      #   # bad
      #   node.method_name == :do_something
      #
      #   # good
      #   node.method?(:do_something)
      #
      #   # bad
      #   node.method_name != :do_something
      #
      #   # good
      #   !node.method?(:do_something)
      #
      class MethodNameEqual < Base
        extend AutoCorrector

        MSG = 'Use `%<prefer>s` instead.'
        RESTRICT_ON_SEND = %i[== !=].freeze

        # @!method method_name(node)
        def_node_matcher :method_name, <<~PATTERN
          (send
            (send
              (...) :method_name) {:== :!=}
            $_)
        PATTERN

        def on_send(node)
          method_name(node) do |method_name_arg|
            bang = node.method?(:!=) ? '!' : ''
            prefer = "#{bang}#{node.receiver.receiver.source}.method?(#{method_name_arg.source})"
            message = format(MSG, prefer: prefer)

            add_offense(node, message: message) do |corrector|
              corrector.replace(node, prefer)
            end
          end
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 8 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb