Module: Mutant::AST

Defined in:
lib/mutant/ast.rb,
lib/mutant/ast/meta.rb,
lib/mutant/ast/sexp.rb,
lib/mutant/ast/nodes.rb,
lib/mutant/ast/types.rb,
lib/mutant/ast/named_children.rb,
lib/mutant/ast/node_predicates.rb

Overview

AST helpers

Defined Under Namespace

Modules: Meta, NamedChildren, NodePredicates, Nodes, Sexp, Types

Class Method Summary collapse

Class Method Details

.find_last(node) {|Parser::AST::Node| ... } ⇒ Parser::AST::Node?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find last node satisfing predicate (as block)

Yields:

  • (Parser::AST::Node)

Yield Returns:

  • (Boolean)

    true in case node satisfies predicate

Returns:

  • (Parser::AST::Node)

    if satisfing node is found

  • (nil)

    otherwise

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/mutant/ast.rb', line 42

def self.find_last(node, &predicate)
  raise ArgumentError, 'block expected' unless block_given?
  neddle = nil
  walk(node) do |candidate|
    neddle = candidate if predicate.call(candidate, &predicate)
  end
  neddle
end

.walk(node) {|Parser::AST::Node| ... } ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Walk all ast nodes

Parameters:

  • (Parser::AST::Node)

Yields:

  • (Parser::AST::Node)

    all nodes recursively including root

Returns:

  • (self)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/mutant/ast.rb', line 16

def self.walk(node, &block)
  raise ArgumentError, 'block expected' unless block_given?

  block.call(node)
  node.children.grep(Parser::AST::Node).each do |child|
    walk(child, &block)
  end

  self
end