Interface SyntaxNode

The syntax tree that contains this node

interface SyntaxNode {
    childCount: number;
    children: SyntaxNode[];
    descendantCount: number;
    endIndex: number;
    endPosition: Point;
    firstChild: SyntaxNode;
    firstNamedChild: SyntaxNode;
    grammarId: number;
    grammarType: string;
    hasChanges: boolean;
    hasError: boolean;
    id: number;
    isError: boolean;
    isExtra: boolean;
    isMissing: boolean;
    isNamed: boolean;
    lastChild: SyntaxNode;
    lastNamedChild: SyntaxNode;
    namedChildCount: number;
    namedChildren: SyntaxNode[];
    nextNamedSibling: SyntaxNode;
    nextParseState: number;
    nextSibling: SyntaxNode;
    parent: SyntaxNode;
    parseState: number;
    previousNamedSibling: SyntaxNode;
    previousSibling: SyntaxNode;
    startIndex: number;
    startPosition: Point;
    text: string;
    tree: Tree;
    type: string;
    typeId: number;
    child(index: number): SyntaxNode;
    childForFieldId(fieldId: number): SyntaxNode;
    childForFieldName(fieldName: string): SyntaxNode;
    childrenForFieldId(fieldId: number): SyntaxNode[];
    childrenForFieldName(fieldName: string): SyntaxNode[];
    childWithDescendant(descendant: SyntaxNode): SyntaxNode;
    closest(types: String | String[]): SyntaxNode;
    descendantForIndex(index: number): SyntaxNode;
    descendantForIndex(startIndex: number, endIndex: number): SyntaxNode;
    descendantForPosition(position: Point): SyntaxNode;
    descendantForPosition(startPosition: Point, endPosition: Point): SyntaxNode;
    descendantsOfType(
        types: String | String[],
        startPosition?: Point,
        endPosition?: Point,
    ): SyntaxNode[];
    fieldNameForChild(childIndex: number): string;
    fieldNameForNamedChild(namedChildIndex: number): string;
    firstChildForIndex(index: number): SyntaxNode;
    firstNamedChildForIndex(index: number): SyntaxNode;
    namedChild(index: number): SyntaxNode;
    namedDescendantForIndex(index: number): SyntaxNode;
    namedDescendantForIndex(startIndex: number, endIndex: number): SyntaxNode;
    namedDescendantForPosition(position: Point): SyntaxNode;
    namedDescendantForPosition(
        startPosition: Point,
        endPosition: Point,
    ): SyntaxNode;
    toString(): string;
    walk(): TreeCursor;
}

Properties

childCount: number

The number of children this node has

children: SyntaxNode[]

Array of all child nodes

descendantCount: number

The number of descendants this node has, including itself

endIndex: number

The byte offset where this node ends

endPosition: Point

The position where this node ends in terms of rows and columns

firstChild: SyntaxNode

The first child of this node

firstNamedChild: SyntaxNode

The first named child of this node

grammarId: number

This node's type as a numeric id as it appears in the grammar, ignoring aliases

grammarType: string

This node's symbol name as it appears in the grammar, ignoring aliases

hasChanges: boolean

Whether this node has been edited

hasError: boolean

Whether this node represents a syntax error or contains any syntax errors within it

id: number

A unique numeric identifier for this node. Within a given syntax tree, no two nodes have the same id. If a new tree is created based on an older tree and reuses a node, that node will have the same id in both trees.

isError: boolean

Whether this node represents a syntax error. Syntax errors represent parts of the code that could not be incorporated into a valid syntax tree.

isExtra: boolean

Whether this node is extra. Extra nodes represent things like comments, which are not required by the grammar but can appear anywhere.

isMissing: boolean

Whether this node is missing. Missing nodes are inserted by the parser in order to recover from certain kinds of syntax errors.

isNamed: boolean

Whether this node is named. Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.

lastChild: SyntaxNode

The last child of this node

lastNamedChild: SyntaxNode

The last child of this node

namedChildCount: number

The number of named children this node has.

namedChildren: SyntaxNode[]

Array of all named child nodes

nextNamedSibling: SyntaxNode

This node's next named sibling

nextParseState: number

The parse state that follows this node

nextSibling: SyntaxNode

This node's next sibling

parent: SyntaxNode

This node's immediate parent. For iterating over ancestors, prefer using childWithDescendant

parseState: number

The parse state of this node

previousNamedSibling: SyntaxNode

This node's previous named sibling

previousSibling: SyntaxNode

This node's previous sibling

startIndex: number

The byte offset where this node starts

startPosition: Point

The position where this node starts in terms of rows and columns

text: string

The text content for this node from the source code

tree: Tree

The syntax tree that contains this node

type: string

This node's type as a string

typeId: number

This node's type as a numeric id

Methods

  • Get the node's child at the given index, where zero represents the first child.

    Note: While fairly fast, this method's cost is technically log(i). For iterating over many children, prefer using the children array.

    Parameters

    • index: number

      Zero-based index of the child to retrieve

    Returns SyntaxNode

    The child node, or null if none exists at the given index

  • Get this node's child with the given numerical field id.

    Field IDs can be obtained from field names using the parser's language object.

    Parameters

    • fieldId: number

      The field ID to search for

    Returns SyntaxNode

    The child node, or null if no child has the given field ID

  • Get the first child with the given field name.

    For fields that may have multiple children, use childrenForFieldName instead.

    Parameters

    • fieldName: string

      The field name to search for

    Returns SyntaxNode

    The child node, or null if no child has the given field name

  • Get all children that have the given field ID

    Parameters

    • fieldId: number

      The field ID to search for

    Returns SyntaxNode[]

    Array of child nodes with the given field ID

  • Get all children that have the given field name

    Parameters

    • fieldName: string

      The field name to search for

    Returns SyntaxNode[]

    Array of child nodes with the given field name

  • Get the immediate child that contains the given descendant node. Note that this can return the descendant itself if it is an immediate child.

    Parameters

    • descendant: SyntaxNode

      The descendant node to find the parent of

    Returns SyntaxNode

    The child containing the descendant, or null if not found

  • Find the closest ancestor of the current node that matches the given type(s).

    Starting from the node's parent, walks up the tree until it finds a node whose type matches any of the given types.

    Parameters

    • types: String | String[]

      A string or array of strings representing the node types to search for

    Returns SyntaxNode

    The closest matching ancestor node, or null if none found

    const property = tree.rootNode.descendantForIndex(5);
    // Find closest unary expression ancestor
    const unary = property.closest('unary_expression');
    // Find closest binary or call expression ancestor
    const expr = property.closest(['binary_expression', 'call_expression']);

    If the argument is not a string or array of strings

  • Get the smallest node within this node that spans the given byte offset.

    Parameters

    • index: number

      The byte offset to search for

    Returns SyntaxNode

    The smallest node spanning the offset

  • Get the smallest node within this node that spans the given byte range.

    Parameters

    • startIndex: number

      The starting byte offset

    • endIndex: number

      The ending byte offset

    Returns SyntaxNode

    The smallest node spanning the range

  • Get the smallest node within this node that spans the given position. When only one position is provided, it's used as both start and end.

    Parameters

    • position: Point

      The point to search for

    Returns SyntaxNode

    The smallest node spanning the position

  • Get the smallest node within this node that spans the given position range.

    Parameters

    • startPosition: Point

      The starting position

    • endPosition: Point

      The ending position

    Returns SyntaxNode

    The smallest node spanning the range

  • Get all descendants of this node that have the given type(s)

    Parameters

    • types: String | String[]

      A string or array of strings of node types to find

    • OptionalstartPosition: Point

      Optional starting position to search from

    • OptionalendPosition: Point

      Optional ending position to search to

    Returns SyntaxNode[]

    Array of descendant nodes matching the given types

  • Get the field name of the child at the given index

    Parameters

    • childIndex: number

      Zero-based index of the child

    Returns string

    The field name, or null if the child has no field name

  • Get the field name of the named child at the given index

    Parameters

    • namedChildIndex: number

      Zero-based index of the named child

    Returns string

    The field name, or null if the named child has no field name

  • Get the node's first child that extends beyond the given byte offset

    Parameters

    • index: number

      The byte offset to search from

    Returns SyntaxNode

    The first child extending beyond the offset, or null if none found

  • Get the node's first named child that extends beyond the given byte offset

    Parameters

    • index: number

      The byte offset to search from

    Returns SyntaxNode

    The first named child extending beyond the offset, or null if none found

  • Get this node's named child at the given index.

    Note: While fairly fast, this method's cost is technically log(i). For iterating over many children, prefer using the namedChildren array.

    Parameters

    • index: number

      Zero-based index of the named child to retrieve

    Returns SyntaxNode

    The named child node, or null if none exists at the given index

  • Get the smallest named node within this node that spans the given byte offset.

    Parameters

    • index: number

      The byte offset to search for

    Returns SyntaxNode

    The smallest named node spanning the offset

  • Get the smallest named node within this node that spans the given byte range.

    Parameters

    • startIndex: number

      The starting byte offset

    • endIndex: number

      The ending byte offset

    Returns SyntaxNode

    The smallest named node spanning the range

  • Get the smallest named node within this node that spans the given position. When only one position is provided, it's used as both start and end.

    Parameters

    • position: Point

      The point to search for

    Returns SyntaxNode

    The smallest named node spanning the position

  • Get the smallest named node within this node that spans the given position range.

    Parameters

    • startPosition: Point

      The starting position

    • endPosition: Point

      The ending position

    Returns SyntaxNode

    The smallest named node spanning the range

  • Convert this node to its string representation

    Returns string