Class: Woods::Ast::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/woods/ast/node.rb

Overview

Normalized AST node struct used by all consumers.

Provides a parser-independent representation of Ruby AST nodes. Both Prism and the parser gem are normalized to this common structure.

Examples:

Creating a send node

node = Ast::Node.new(
  type: :send,
  children: [],
  line: 42,
  receiver: "User",
  method_name: "find",
  arguments: ["id"]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def arguments
  @arguments
end

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def children
  @children
end

#end_lineObject

Returns the value of attribute end_line

Returns:

  • (Object)

    the current value of end_line



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def end_line
  @end_line
end

#lineObject

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def line
  @line
end

#method_nameObject

Returns the value of attribute method_name

Returns:

  • (Object)

    the current value of method_name



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def method_name
  @method_name
end

#receiverObject

Returns the value of attribute receiver

Returns:

  • (Object)

    the current value of receiver



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def receiver
  @receiver
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def source
  @source
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



20
21
22
# File 'lib/woods/ast/node.rb', line 20

def type
  @type
end

Instance Method Details

#find_all(target_type) ⇒ Array<Ast::Node>

Find all descendant nodes matching a type.

Parameters:

  • target_type (Symbol)

    The node type to search for

Returns:

  • (Array<Ast::Node>)

    All matching descendant nodes



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/woods/ast/node.rb', line 35

def find_all(target_type)
  results = []
  queue = [self]
  while (current = queue.shift)
    results << current if current.type == target_type
    (current.children || []).each do |child|
      queue << child if child.is_a?(Ast::Node)
    end
  end
  results
end

#find_first(target_type) ⇒ Ast::Node?

Find the first descendant node matching a type (depth-first).

Parameters:

  • target_type (Symbol)

    The node type to search for

Returns:

  • (Ast::Node, nil)

    The first matching node or nil



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/woods/ast/node.rb', line 51

def find_first(target_type)
  return self if type == target_type

  (children || []).each do |child|
    next unless child.is_a?(Ast::Node)

    result = child.find_first(target_type)
    return result if result
  end
  nil
end

#to_sourceString

Return source text representation.

Returns:

  • (String)

    The source field if present, otherwise a reconstruction



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/woods/ast/node.rb', line 66

def to_source
  return source if source

  case type
  when :send
    parts = []
    parts << receiver if receiver
    parts << method_name if method_name
    parts.join('.')
  when :const
    parts = []
    parts << receiver if receiver
    parts << method_name if method_name
    parts.join('::')
  when :def, :defs
    "def #{method_name}"
  else
    type.to_s
  end
end