-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate traversing from the collector
- Loading branch information
Showing
2 changed files
with
43 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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. | ||
export default function* traverse(node: Node & {type: string}): Generator<Node, void, boolean> { | ||
const keys = VISITOR_KEYS[node.type]; | ||
|
||
if (!keys) { | ||
return; | ||
} | ||
|
||
const done = yield node; | ||
|
||
if (done) { | ||
return; | ||
} | ||
|
||
for (const key of keys) { | ||
const subNode = node[key]; | ||
|
||
if (subNode instanceof Array) { | ||
for (const node of subNode) { | ||
yield* traverse(node); | ||
} | ||
} else if (subNode) { | ||
yield* traverse(subNode); | ||
} | ||
} | ||
} |