Sha256: ba45721029cba38129cec2868543c43a141b547eca51c7e7028425f237e415ea

Contents?: true

Size: 1.16 KB

Versions: 17

Compression:

Stored size: 1.16 KB

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

    # Walk all ast nodes keeping track of path
    #
    # @param [Parser::AST::Node] root
    # @param [Array<Parser::AST::Node>] stack
    #
    # @yield [Parser::AST::Node]
    #   all nodes visited recursively including root
    #
    # @return [undefined]
    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

17 entries across 17 versions & 1 rubygems

Version Path
mutant-0.9.8 lib/mutant/ast.rb
mutant-0.9.7 lib/mutant/ast.rb
mutant-0.9.6 lib/mutant/ast.rb
mutant-0.9.5 lib/mutant/ast.rb
mutant-0.9.4 lib/mutant/ast.rb
mutant-0.9.3 lib/mutant/ast.rb
mutant-0.9.2 lib/mutant/ast.rb
mutant-0.9.1 lib/mutant/ast.rb
mutant-0.9.0 lib/mutant/ast.rb
mutant-0.8.24 lib/mutant/ast.rb
mutant-0.8.23 lib/mutant/ast.rb
mutant-0.8.22 lib/mutant/ast.rb
mutant-0.8.21 lib/mutant/ast.rb
mutant-0.8.20 lib/mutant/ast.rb
mutant-0.8.19 lib/mutant/ast.rb
mutant-0.8.18 lib/mutant/ast.rb
mutant-0.8.17 lib/mutant/ast.rb