class RuboCop::Cop::Performance::AncestorsInclude

This cop is used to identify usages of `ancestors.include?` and change them to use `<=` instead.

@example

# bad
A.ancestors.include?(B)

# good
A <= B

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/performance/ancestors_include.rb, line 27
def on_send(node)
  return unless (subclass, superclass = ancestors_include_candidate?(node))
  return if subclass && !subclass.const_type?

  add_offense(range(node)) do |corrector|
    subclass_source = subclass ? subclass.source : 'self'

    corrector.replace(node, "#{subclass_source} <= #{superclass.source}")
  end
end

Private Instance Methods

range(node) click to toggle source
# File lib/rubocop/cop/performance/ancestors_include.rb, line 40
def range(node)
  location_of_ancestors = node.children[0].loc.selector.begin_pos
  end_location = node.loc.selector.end_pos

  range_between(location_of_ancestors, end_location)
end