This represents a node in the tree. It is the parent class of all of the various node types.
A pointer to the source that this node was created from.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 115
def self.fields
# This method should only be called on subclasses of Node, not Node
# itself.
raise NoMethodError, "undefined method `fields' for #{inspect}" if self == Node
Reflection.fields_for(self)
end
Returns a list of the fields that exist for this node class. Fields describe the structure of the node. This kind of reflection is useful for things like recursively visiting each node and field in the tree.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 167
def self.type
raise NoMethodError, "undefined method `type' for #{inspect}"
end
Returns the type of the node as a symbol.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 131
def accept(visitor)
raise NoMethodError, "undefined method `accept' for #{inspect}"
end
Accepts a visitor and calls back into the specialized visit function.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 137
def child_nodes
raise NoMethodError, "undefined method `child_nodes' for #{inspect}"
end
Returns an array of child nodes, including ‘nil`s in the place of optional nodes that were not present.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 151
def comment_targets
raise NoMethodError, "undefined method `comment_targets' for #{inspect}"
end
Returns an array of child nodes and locations that could potentially have comments attached to them.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 145
def compact_child_nodes
raise NoMethodError, "undefined method `compact_child_nodes' for #{inspect}"
end
Returns an array of child nodes, excluding any ‘nil`s in the place of optional nodes that were not present.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 34
def end_offset
location = @location
location.is_a?(Location) ? location.end_offset : ((location >> 32) + (location & 0xFFFFFFFF))
end
The end offset of the node in the source. This method is effectively a delegate method to the location object.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 162
def inspect
raise NoMethodError, "undefined method `inspect' for #{inspect}"
end
Returns a string representation of the node.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 19
def location
location = @location
return location if location.is_a?(Location)
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
A Location
instance that represents the location of this node in the source.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 62
def pretty_print(q)
q.seplist(inspect.chomp.each_line, -> { q.breakable }) do |line|
q.text(line.chomp)
end
q.current_group.break
end
Similar to inspect, but respects the current level of indentation given by the pretty print object.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 49
def slice
location.slice
end
Slice the location of the node from the source.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 56
def slice_lines
location.slice_lines
end
Slice the location of the node from the source, starting at the beginning of the line that the location starts on, ending at the end of the line that the location ends on.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 40
def source_lines
location.source_lines
end
Returns all of the lines of the source code associated with this node.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 27
def start_offset
location = @location
location.is_a?(Location) ? location.start_offset : location >> 32
end
The start offset of the node in the source. This method is effectively a delegate method to the location object.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 70
def to_dot
# @type self: node
DotVisitor.new.tap { |visitor| accept(visitor) }.to_dot
end
Convert this node into a graphviz dot graph string.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 81
def tunnel(line, column)
queue = [self] #: Array[Prism::node]
result = []
while (node = queue.shift)
result << node
node.compact_child_nodes.each do |child_node|
child_location = child_node.location
start_line = child_location.start_line
end_line = child_location.end_line
if start_line == end_line
if line == start_line && column >= child_location.start_column && column < child_location.end_column
queue << child_node
break
end
elsif (line == start_line && column >= child_location.start_column) || (line == end_line && column < child_location.end_column)
queue << child_node
break
elsif line > start_line && line < end_line
queue << child_node
break
end
end
end
result
end
Returns a list of nodes that are descendants of this node that contain the given line and column. This is useful for locating a node that is selected based on the line and column of the source code.
Important to note is that the column given to this method should be in bytes, as opposed to characters or code units.
# File tmp/rubies/ruby-3.4.0-preview1/lib/prism/node.rb, line 157
def type
raise NoMethodError, "undefined method `type' for #{inspect}"
end
Returns a symbol symbolizing the type of node that this represents. This is particularly useful for case statements and array comparisons.