Sha256: 99feba720999841a70b6bfb77c3daa5f10d75282436e1eebf6f65facaa9770c5

Contents?: true

Size: 926 Bytes

Versions: 54

Compression:

Stored size: 926 Bytes

Contents

# frozen_string_literal: true

module Mutant
  # AST helpers
  module AST

    # Find last node satisfying predicate (as block)
    #
    # @return [Parser::AST::Node]
    #   if satisfying node is found
    #
    # @yield [Parser::AST::Node]
    #
    # @yieldreturn [Boolean]
    #   true in case node satisfies predicate
    #
    # @return [nil]
    #   otherwise
    def self.find_last_path(node, &predicate)
      fail ArgumentError, 'block expected' unless block_given?
      path = []
      walk(node, [node]) do |candidate, stack|
        if predicate.call(candidate)
          path = stack.dup
        end
      end
      path
    end

    def self.walk(node, stack, &block)
      block.call(node, stack)
      node.children.grep(::Parser::AST::Node) do |child|
        stack.push(child)
        walk(child, stack, &block)
        stack.pop
      end
    end
    private_class_method :walk

  end # AST
end # Mutant

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
mutant-0.11.13 lib/mutant/ast.rb
mutant-0.11.12 lib/mutant/ast.rb
mutant-0.11.11 lib/mutant/ast.rb
mutant-0.11.10 lib/mutant/ast.rb
mutant-0.11.9 lib/mutant/ast.rb
mutant-0.11.8 lib/mutant/ast.rb
mutant-0.11.7 lib/mutant/ast.rb
mutant-0.11.6 lib/mutant/ast.rb
mutant-0.11.5 lib/mutant/ast.rb
mutant-0.11.4 lib/mutant/ast.rb
mutant-0.11.3 lib/mutant/ast.rb
mutant-0.11.2 lib/mutant/ast.rb
mutant-0.11.1 lib/mutant/ast.rb
mutant-0.11.0 lib/mutant/ast.rb
mutant-0.10.35 lib/mutant/ast.rb
mutant-0.10.34 lib/mutant/ast.rb
mutant-0.10.33 lib/mutant/ast.rb
mutant-0.10.32 lib/mutant/ast.rb
mutant-0.10.31 lib/mutant/ast.rb
mutant-0.10.30 lib/mutant/ast.rb