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
rubocop-0.79.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.78.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/internal_affairs/method_name_equal.rb
rubocop-0.77.0 lib/rubocop/cop/internal_affairs/method_name_equal.rb