Sha256: 5fa9da4afb394b4ef8865e75b9f84252ee5937a3c8321b79459b37a8130a62e1

Contents?: true

Size: 1.47 KB

Versions: 24

Compression:

Stored size: 1.47 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)
      #
      class MethodNameEqual < Cop
        include RangeHelp

        MSG = 'Use `method?(%<method_name>s)` instead of ' \
              '`method_name == %<method_name>s`.'

        def_node_matcher :method_name?, <<~PATTERN
          (send
            $(send
              (...) :method_name) :==
            $...)
        PATTERN

        def on_send(node)
          method_name?(node) do |method_name_node, method_name_arg|
            message = format(MSG, method_name: method_name_arg.first.source)

            range = range(method_name_node, node)

            add_offense(node, location: range, message: message)
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            method_name?(node) do |method_name_node, method_name_arg|
              corrector.replace(
                range(method_name_node, node),
                "method?(#{method_name_arg.first.source})"
              )
            end
          end
        end

        private

        def range(method_name_node, node)
          range_between(
            method_name_node.loc.selector.begin_pos, node.source_range.end_pos
          )
        end
      end
    end
  end
end

Version data entries

24 entries across 23 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.88.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.87.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.87.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.86.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/internal_affairs/method_name_equal.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.85.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.85.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.84.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.83.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.82.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.81.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.80.1 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.80.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/internal_affairs/method_name_equal.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/internal_affairs/method_name_equal.rb