TIP: Visit Children
There is a utility function ts.forEachChild
that allows you to visit all the child nodes of any Node in the AST.
Here is simplified snippet of the source code to demonstrate how it functions:
Basically, it checks node.kind
and based on that assumes an interface offered by the node
and calls the cbNode
on the children. However, note that this function doesn't call visitNode
for all children (e.g. SyntaxKind.SemicolonToken). If you want all the children of a node in the AST just call .getChildren
member function of the Node
.
E.g. here is a function that prints the verbose AST
of a node:
We will see a sample usage of this function when we discuss the parser further.
Last updated