class RuboCop::Cop::InternalAffairs::MethodNameEndWith

Checks potentially usage of method identifier predicates defined in rubocop-ast instead of ‘method_name.end_with?`.

@example

# bad
node.method_name.to_s.end_with?('=')

# good
node.assignment_method?

# bad
node.method_name.to_s.end_with?('?')

# good
node.predicate_method?

# bad
node.method_name.to_s.end_with?('!')

# good
node.bang_method?

Constants

MSG
RESTRICT_ON_SEND
SUGGEST_METHOD_FOR_SUFFIX

Public Instance Methods

on_csend(node)
Alias for: on_send
on_send(node) click to toggle source
# File lib/rubocop/cop/internal_affairs/method_name_end_with.rb, line 53
def on_send(node)
  method_name_end_with?(node) do |method_name_node, end_with_arg|
    next unless method_name_node.receiver

    preferred_method = SUGGEST_METHOD_FOR_SUFFIX[end_with_arg.value]
    range = range(method_name_node, node)
    message = format(MSG, method_name: preferred_method, method_suffix: range.source)

    add_offense(range, message: message) do |corrector|
      corrector.replace(range, preferred_method)
    end
  end
end
Also aliased as: on_csend

Private Instance Methods

range(method_name_node, node) click to toggle source
# File lib/rubocop/cop/internal_affairs/method_name_end_with.rb, line 70
def range(method_name_node, node)
  range = if method_name_node.call_type?
            method_name_node.loc.selector
          else
            method_name_node.source_range
          end

  range_between(range.begin_pos, node.source_range.end_pos)
end