Skip to content

Commit 89c80fe

Browse files
kjrockerMoustaphaDevsarah11918
authored
feat: introduce asynchronous walk function (#1087)
* Introduce walkAsync, an awaited walk function * Update documentation with walkAsync * Add Changeset * Update .changeset/tough-months-melt.md Co-authored-by: Happydev <[email protected]> * Update .changeset/tough-months-melt.md Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Happydev <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent ab9b285 commit 89c80fe

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

.changeset/tough-months-melt.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@astrojs/compiler": minor
3+
---
4+
5+
Adds a `walkAsync` utility function that returns a Promise from the tree traversal process.
6+
7+
Unlike the existing `walk` function which doesn't provide a way to wait for traversal completion, `walkAsync` allows consumers to `await` the full traversal of the AST.

packages/compiler/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ The Astro compiler can emit an AST using the `parse` method.
3737

3838
- Position data is currently incomplete and in some cases incorrect. We're working on it!
3939
- A `TextNode` can represent both HTML `text` and JavaScript/TypeScript source code.
40-
- The `@astrojs/compiler/utils` entrypoint exposes a `walk` function that can be used to traverse the AST. It also exposes the `is` helper which can be used as guards to derive the proper types for each `node`.
40+
- The `@astrojs/compiler/utils` entrypoint exposes `walk` and `walkAsync` functions that can be used to traverse the AST. It also exposes the `is` helper which can be used as guards to derive the proper types for each `node`.
4141

4242
```js
4343
import { parse } from "@astrojs/compiler";
44-
import { walk, is } from "@astrojs/compiler/utils";
44+
import { walk, walkAsync, is } from "@astrojs/compiler/utils";
4545

4646
const result = await parse(source, {
4747
position: false, // defaults to `true`
@@ -53,6 +53,12 @@ walk(result.ast, (node) => {
5353
console.log(node.name);
5454
}
5555
});
56+
57+
await walkAsync(result.ast, async (node) => {
58+
if (is.tag(node)) {
59+
node.value = await expensiveCalculation(node)
60+
}
61+
});
5662
```
5763

5864
## Develop

packages/compiler/src/browser/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export function walk(node: ParentNode, callback: Visitor): void {
7171
walker.visit(node);
7272
}
7373

74+
export function walkAsync(node: ParentNode, callback: Visitor): Promise<void> {
75+
const walker = new Walker(callback);
76+
return walker.visit(node);
77+
}
78+
7479
function serializeAttributes(node: TagLikeNode): string {
7580
let output = '';
7681
for (const attr of node.attributes) {

packages/compiler/src/node/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export function walk(node: ParentNode, callback: Visitor): void {
7171
walker.visit(node);
7272
}
7373

74+
export function walkAsync(node: ParentNode, callback: Visitor): Promise<void> {
75+
const walker = new Walker(callback);
76+
return walker.visit(node);
77+
}
78+
7479
function serializeAttributes(node: TagLikeNode): string {
7580
let output = '';
7681
for (const attr of node.attributes) {

0 commit comments

Comments
 (0)