Class

This represents a node in the tree. It is the parent class of all of the various node types.

Attributes
Read

A pointer to the source that this node was created from.

Read

A unique identifier for this node. This is used in a very specific use case where you want to keep around a reference to a node without having to keep around the syntax tree in memory. This unique identifier will be consistent across multiple parses of the same source code.

Read

An bitset of flags for this node. There are certain flags that are common for all nodes, and then some nodes have specific flags.

Class Methods

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.

Similar to type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

Instance Methods

Accepts a visitor and calls back into the specialized visit function.

Returns the first node that matches the given block when visited in a depth-first search. This is useful for finding a node that matches a particular condition.

node.breadth_first_search { |node| node.node_id == node_id }

Delegates to the cached_end_code_units_column of the associated location object.

Delegates to the cached_end_code_units_offset of the associated location object.

Delegates to the cached_start_code_units_column of the associated location object.

Delegates to the cached_start_code_units_offset of the associated location object.

Returns an array of child nodes, including ‘nil`s in the place of optional nodes that were not present.

Returns an array of child nodes and locations that could potentially have comments attached to them.

Delegates to the comments of the associated location object.

Returns an array of child nodes, excluding any ‘nil`s in the place of optional nodes that were not present.

An alias for child_nodes

Delegates to the end_character_column of the associated location object.

Delegates to the end_character_offset of the associated location object.

Delegates to the end_column of the associated location object.

Delegates to the end_line of the associated location object.

The end offset of the node in the source. This method is effectively a delegate method to the location object.

Returns a string representation of the node.

Delegates to the leading_comments of the associated location object.

A Location instance that represents the location of this node in the source.

Returns true if the node has the newline flag set.

Similar to inspect, but respects the current level of indentation given by the pretty print object.

Save this node using a saved source so that it can be retrieved later.

Save the location using a saved source so that it can be retrieved later.

An alias for source_lines

Slice the location of the node from the source.

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.

Returns all of the lines of the source code associated with this node.

Delegates to the start_character_column of the associated location object.

Delegates to the start_character_offset of the associated location object.

Delegates to the start_column of the associated location object.

Delegates to the start_line of the associated location object.

The start offset of the node in the source. This method is effectively a delegate method to the location object.

Returns true if the node has the static literal flag set.

Convert this node into a graphviz dot graph string.

Delegates to the trailing_comments of the associated location object.

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.

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.