123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Performance::AncestorsInclude

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, AutoCorrector, Base
Instance Chain:
self, RangeHelp, Base
Inherits: Base
  • Object
Defined in: lib/rubocop/cop/performance/ancestors_include.rb

Overview

Identifies usages of ancestors.include? and change them to use instead.

Examples:

# bad
A.ancestors.include?(B)

# good
A <= B

Cop Safety Information:

  • This cop is unsafe because it can’t tell whether the receiver is a class or an object. e.g. the false positive was for Nokogiri::XML::Node#ancestors.

Constant Summary

Instance Method Summary

Instance Method Details

#on_send(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/ancestors_include.rb', line 30

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

#range(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/ancestors_include.rb', line 43

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