Sha256: 117c480028b77cc2db024dfc71d4fb9df624ff52b65f9f59a2e06e70b4d45f2b

Contents?: true

Size: 1.46 KB

Versions: 104

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies usages of `ancestors.include?` and change them to use `<=` instead.
      #
      # @safety
      #   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`.
      #
      # @example
      #   # bad
      #   A.ancestors.include?(B)
      #
      #   # good
      #   A <= B
      #
      class AncestorsInclude < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `<=` instead of `ancestors.include?`.'
        RESTRICT_ON_SEND = %i[include?].freeze

        def_node_matcher :ancestors_include_candidate?, <<~PATTERN
          (send (send $_subclass :ancestors) :include? $_superclass)
        PATTERN

        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

        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
      end
    end
  end
end

Version data entries

104 entries across 102 versions & 7 rubygems

Version Path
rubocop-performance-1.23.0 lib/rubocop/cop/performance/ancestors_include.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/ancestors_include.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/ancestors_include.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/ancestors_include.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/ancestors_include.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/ancestors_include.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/ancestors_include.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/ancestors_include.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/ancestors_include.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/ancestors_include.rb