Skip to content

Commit bfcb999

Browse files
committed
chore: Refactor AST traversal to use generators
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 7185b10 commit bfcb999

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

packages/cxx-frontend/src/ASTCursor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,15 @@ export class ASTCursor {
158158
/**
159159
* Pre-order traversal of the AST.
160160
*
161-
* @param accept The function to call for each node.
161+
* @returns A generator that yields the nodes in pre-order.
162162
*/
163-
preVisit(accept: (args: AcceptArgs) => void | boolean) {
163+
*preVisit() {
164164
let done = false;
165165
let depth = 0;
166166

167167
while (!done) {
168168
do {
169-
if (accept({ node: this.node, slot: this.slot, depth }) === true)
170-
return;
169+
yield { node: this.node, slot: this.slot, depth };
171170
++depth;
172171
} while (this.gotoFirstChild());
173172

packages/cxx-storybook/src/SyntaxTree.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ export function SyntaxTree({ parser, cursorPosition }: SyntaxTreeProps) {
7373

7474
const ast = parser?.getAST();
7575

76-
ast?.walk().preVisit(({ node, slot, depth: level }) => {
77-
if (!(node instanceof AST)) return;
76+
for (const { node, slot, depth: level } of ast?.walk().preVisit() ?? []) {
77+
if (!(node instanceof AST)) continue;
7878

7979
const kind = ASTKind[node.getKind()];
8080

@@ -93,7 +93,7 @@ export function SyntaxTree({ parser, cursorPosition }: SyntaxTreeProps) {
9393
const handle = node.getHandle();
9494

9595
nodes.push({ description, slot, handle, level });
96-
});
96+
}
9797
setNodes(nodes);
9898
}, [parser]);
9999

templates/cxx-browser-esm-vite/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ async function main() {
5050

5151
const ast = parser.getAST();
5252

53-
ast?.walk().preVisit(({ node, depth }) => {
53+
for (const { node, depth } of ast?.walk().preVisit() ?? []) {
5454
if (!(node instanceof AST)) {
55-
return;
55+
continue;
5656
}
5757

5858
const nodeWithAttributes: AST & Partial<Attributes> = node;
@@ -77,7 +77,7 @@ async function main() {
7777
}
7878

7979
rows.push(description);
80-
});
80+
}
8181

8282
parser.dispose();
8383

templates/cxx-parse-esm/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ async function main() {
6161

6262
const ast = parser.getAST();
6363

64-
ast?.walk().preVisit(({ node, slot, depth }) => {
65-
if (!node instanceof AST) return;
64+
for (const { node, slot, depth } of ast?.walk().preVisit() ?? []) {
65+
if ((!node) instanceof AST) continue;
6666
const ind = " ".repeat(depth * 2);
6767
const kind = ASTKind[node.getKind()];
6868
const member = slot !== undefined ? `${ASTSlot[slot]}: ` : "";
6969
console.log(`${ind}- ${member}${kind}`);
70-
});
70+
}
7171

7272
parser.dispose();
7373
}

templates/cxx-parse/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ async function main() {
5555

5656
const ast = parser.getAST();
5757

58-
ast?.walk().preVisit(({ node, slot, depth }) => {
59-
if (!node instanceof AST) return;
58+
for (const { node, slot, depth } of ast?.walk().preVisit() ?? []) {
59+
if ((!node) instanceof AST) continue;
6060
const ind = " ".repeat(depth * 2);
6161
const kind = ASTKind[node.getKind()];
6262
const member = slot !== undefined ? `${ASTSlot[slot]}: ` : "";
6363
console.log(`${ind}- ${member}${kind}`);
64-
});
64+
}
6565

6666
parser.dispose();
6767
}

0 commit comments

Comments
 (0)