How can one find nodes of a specific kind in an ASL tree with typescript compiler api ?
I've tried using forEachChild to get the nodes and then iterate over those to filter the elements I want. However forEachChild doesn't seem to walk the tree ?
What I hope to find
findNodes(node, isReturnStatement);
findNodes(node, isClassDeclaration);
findNodes(node, is...);
What I have tried:
const children: Node[] = [];
node.forEachChild((childNode) => {
children.push(childNode);
});
return node.filter(isReturnStatement);
To find a node of the specific kind you've to traverse the Abstract Syntax Tree (AST) and do some if conditions to find the kind of node you're interested in.
I'll assume you're doing some sort of transformation so I'll use transform API to traverse the AST.
The following code updates the function name and removes the return statement