TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
Node class represents a single node within a syntax tree. Nodes are the primary way to inspect and traverse the structure of parsed source code.
Properties
Identity and Location
id
The numeric id for this node that is unique within its tree.
If a new tree is created based on an older tree (via incremental parsing), and a node from the old tree is reused, that node will have the same id in both trees.
tree
The tree that this node belongs to.
startIndex
The byte index where this node starts.
endIndex
The byte index where this node ends.
startPosition
The position where this node starts.
endPosition
The position where this node ends.
Type Information
type
Get this node’s type as a string.
typeId
Get this node’s type as a numerical id.
grammarType
Get this node’s grammar type as a string, ignoring aliases.
grammarId
Get this node’s grammar type as a numerical id, ignoring aliases.
Node Classification
isNamed
Check if this node is named. Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.
isExtra
Check if this node is extra. Extra nodes represent things like comments, which are not required by the grammar but can appear anywhere.
isError
Check if this node represents a syntax error.
isMissing
Check if this node is missing. Missing nodes are inserted by the parser to recover from certain kinds of syntax errors.
hasError
Check if this node represents a syntax error or contains any syntax errors anywhere within it.
hasChanges
Check if this node has been edited.
Content
text
Get the string content of this node.
Parse State
parseState
Get this node’s parse state.
nextParseState
Get the parse state after this node.
Parse states are advanced features used for error recovery and lookahead. Most applications don’t need to use them.
Child Access
Counts
childCount
Get this node’s number of children.
namedChildCount
Get this node’s number of named children.
Individual Children
child(index)
Get this node’s child at the given index.
This method is fairly fast, but its cost is technically log(n). If you’re iterating over a long list of children, use the
children property instead.namedChild(index)
Get this node’s named child at the given index.
firstChild
Get this node’s first child.
lastChild
Get this node’s last child.
firstNamedChild
Get this node’s first named child.
lastNamedChild
Get this node’s last named child.
All Children
children
Iterate over this node’s children.
namedChildren
Iterate over this node’s named children.
If you’re walking the tree recursively, consider using a
TreeCursor instead for better performance.Field Access
Many nodes have fields, which are named children that play specific roles in the grammar.childForFieldName(fieldName)
Get the first child with the given field name.
childForFieldId(fieldId)
Get this node’s child with the given numerical field id.
You can convert a field name to an id using
language.fieldIdForName().childrenForFieldName(fieldName)
Get an array of this node’s children with a given field name.
childrenForFieldId(fieldId)
Get an array of this node’s children with a given field id.
fieldNameForChild(index)
Get the field name of this node’s child at the given index.
fieldNameForNamedChild(index)
Get the field name of this node’s named child at the given index.
Positional Queries
Children at Index/Position
firstChildForIndex(index)
Get the node’s first child that contains or starts after the given byte offset.
firstNamedChildForIndex(index)
Get the node’s first named child that contains or starts after the given byte offset.
Descendants
descendantForIndex(start, end?)
Get the smallest node within this node that spans the given byte range.
namedDescendantForIndex(start, end?)
Get the smallest named node within this node that spans the given byte range.
descendantForPosition(start, end?)
Get the smallest node within this node that spans the given point range.
namedDescendantForPosition(start, end?)
Get the smallest named node within this node that spans the given point range.
descendantsOfType(types, startPosition?, endPosition?)
Get all descendants of this node that are the given type(s).
descendantCount
Get the node’s number of descendants, including itself.
Siblings
nextSibling
Get this node’s next sibling.
previousSibling
Get this node’s previous sibling.
nextNamedSibling
Get this node’s next named sibling.
previousNamedSibling
Get this node’s previous named sibling.
Parent and Ancestors
parent
Get this node’s immediate parent.
Prefer using
childWithDescendant() for iterating over ancestors, as it’s more efficient.childWithDescendant(descendant)
Get the node that contains descendant.
descendant, or descendant itself if it’s a direct child
Example:
Tree Cursor
walk()
Create a new TreeCursor starting from this node.
Utility Methods
equals(other)
Check if this node is equal to another node.
toString()
Get the S-expression representation of this node.
edit(edit)
Edit this node to keep it in sync with source code that has been edited.
This function is rarely needed. When you edit a tree with
tree.edit(), all nodes retrieved afterward will already reflect the edit. You only need node.edit() when you have a specific node instance you want to keep and continue using after an edit.Common Patterns
Traversing All Nodes
Finding Nodes by Type
Checking for Errors
See Also
- Tree class - Working with syntax trees
- Parser class - Creating parsers
- Query API - Pattern matching on nodes