Class: Transpec::AST::Node

Inherits:
Parser::AST::Node
  • Object
show all
Defined in:
lib/transpec/ast/node.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, children = [], properties = {}) ⇒ Node

Returns a new instance of Node.



8
9
10
11
12
13
14
15
16
# File 'lib/transpec/ast/node.rb', line 8

def initialize(type, children = [], properties = {})
  @properties = {}

  super

  each_child_node do |child_node|
    child_node.parent_node = self
  end
end

Instance Method Details

#ancestor_nodesObject



39
40
41
# File 'lib/transpec/ast/node.rb', line 39

def ancestor_nodes
  each_ancestor_node.to_a
end

#child_nodesObject



54
55
56
# File 'lib/transpec/ast/node.rb', line 54

def child_nodes
  each_child_node.to_a
end

#descendent_nodesObject



67
68
69
# File 'lib/transpec/ast/node.rb', line 67

def descendent_nodes
  each_descendent_node.to_a
end

#each_ancestor_node(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/transpec/ast/node.rb', line 28

def each_ancestor_node(&block)
  return to_enum(__method__) unless block_given?

  if parent_node
    yield parent_node
    parent_node.each_ancestor_node(&block)
  end

  self
end

#each_child_nodeObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/transpec/ast/node.rb', line 43

def each_child_node
  return to_enum(__method__) unless block_given?

  children.each do |child|
    next unless child.is_a?(self.class)
    yield child
  end

  self
end

#each_descendent_node(&block) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/transpec/ast/node.rb', line 58

def each_descendent_node(&block)
  return to_enum(__method__) unless block_given?

  each_child_node do |child_node|
    yield child_node
    child_node.each_descendent_node(&block)
  end
end

#parent_nodeObject



18
19
20
# File 'lib/transpec/ast/node.rb', line 18

def parent_node
  @properties[:parent_node]
end