Sha256: 64c54dcee8dfd71d5dcf4a731b48235008a04d58609d547f704626061fd9eb99

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Mutant
  class AST
    # Given an AST, finds the sclass that directly(-ish) contains the provided
    # node.
    # This won't match arbitrarily complex structures - it only searches the
    # first level deep (no begins-in-begins, for example). This is in
    # keeping with mutant generally not supporting 'weird' syntax.
    # Descending into 'begin' nodes is supported because these are generated for
    # the one-line syntax class << self; def foo; end
    class FindMetaclassContaining
      include NodePredicates, Concord.new(:ast, :target), Procto

      SCLASS_BODY_INDEX = 1

      private_constant(*constants(false))

      # Find metaclass node containing target node
      #
      # @return [Parser::AST::Node, nil]
      #
      # @api private
      def call
        Structure.for(ast.node.type).each_node(ast.node) do |current|
          return current if n_sclass?(current) && metaclass_of?(current)
        end

        nil
      end

    private

      def metaclass_of?(sclass)
        body = sclass.children.fetch(SCLASS_BODY_INDEX)
        body.equal?(target) || transparently_contains?(body)
      end

      def transparently_contains?(body)
        n_begin?(body) && include_exact?(body.children, target)
      end

      def include_exact?(haystack, needle)
        haystack.any? { |elem| elem.equal?(needle) }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mutant-0.11.18 lib/mutant/ast/find_metaclass_containing.rb
mutant-0.11.17 lib/mutant/ast/find_metaclass_containing.rb
mutant-0.11.16 lib/mutant/ast/find_metaclass_containing.rb
mutant-0.11.15 lib/mutant/ast/find_metaclass_containing.rb
mutant-0.11.14 lib/mutant/ast/find_metaclass_containing.rb