While navigating through each child node of a parent node using visitEachChild
, is there a way to stop the process when I no longer wish to visit the subsequent child nodes? For example:
- Parent node
- Node 1
- Node 2 <-- My target point.
- Node 3
- Node 4
- Node 5
How can we halt the traversal at a specific point? Meaning, I do not want to proceed with iterating over Node 3, 4, 5, etc., similar to using a "break" statement.
For instance:
class A {
public template() { // <-- My intended stop here.
return 'Template';
}
someThing() {}
foo() {}
}
Transformer:
const transformerFactory: ts.TransformerFactory<ts.Node> =
(context: ts.TransformationContext) => (rootNode) => {
const visit = (node: ts.Node): ts.Node => {
if (ts.isClassDeclaration(node)) {
ts.visitEachChild(node, (child) => {
if (ts.isMethodDeclaration(child)) {
console.log(child.name.getText());
if (child.name.getText() === 'template') {
return "I don't want to move to the sibling node. Break right here!!!";
} else return child;
} else return child;
}, context);
return node;
}
return ts.visitEachChild(node, visit, context);
};
return ts.visitNode(rootNode, visit);
};