flow2schema/src/collector/traverse.js

32 lines
706 B
JavaScript
Raw Normal View History

2018-07-25 08:38:10 +03:00
// @flow
2017-11-18 15:31:35 +03:00
import type {Node} from '@babel/types';
import {VISITOR_KEYS} from '@babel/types';
// Given the AST output of babylon parse, walk through in a depth-first order.
2017-11-18 20:20:26 +03:00
export default function* traverse(node: Node): Generator<Node, void, boolean> {
2017-11-18 15:31:35 +03:00
const keys = VISITOR_KEYS[node.type];
if (!keys) {
return;
}
const done = yield node;
if (done) {
return;
}
for (const key of keys) {
2017-12-01 20:00:40 +03:00
const subNode = (node: $FlowFixMe)[key];
2017-11-18 15:31:35 +03:00
if (subNode instanceof Array) {
for (const node of subNode) {
yield* traverse(node);
}
} else if (subNode) {
yield* traverse(subNode);
}
}
}