@dynatrace/devkit
Version:
The Dynatrace App Toolkit utilities for writing and testing migrations.
37 lines (36 loc) • 1.07 kB
JavaScript
;
/* eslint-disable no-redeclare */
Object.defineProperty(exports, "__esModule", { value: true });
exports.findNodes = findNodes;
/** Find all nodes from the AST in the subtree that satisfy a type guard. */
function findNodes(node, kindOrGuard,
// eslint-disable-next-line @typescript-eslint/typedef
max = Infinity,
// eslint-disable-next-line @typescript-eslint/typedef
recursive = false) {
if (!node || max === 0) {
return [];
}
const test = typeof kindOrGuard === 'function'
? kindOrGuard
: (node) => node.kind === kindOrGuard;
const arr = [];
if (test(node)) {
arr.push(node);
max--;
}
if (max > 0 && (recursive || !test(node))) {
for (const child of node.getChildren()) {
for (const subNode of findNodes(child, test, max, recursive)) {
if (max > 0) {
arr.push(subNode);
}
max--;
}
if (max <= 0) {
break;
}
}
}
return arr;
}