Skip to content

Add childrenIter method to API nodes - #64302

Open
Wesley Wigham (weswigham) wants to merge 3 commits into
microsoft:mainfrom
weswigham:children-iter
Open

Wesley Wigham (weswigham) wants to merge 3 commits into
microsoft:mainfrom
weswigham:children-iter

Conversation

@weswigham

Copy link
Copy Markdown
Member

A proper child generator is a good alternative to the callback-based forEachChild when working in async (or generator) API contexts. You can, for example:

async function *visitNodeForFirstAnyChild(node: Node): AsyncGenerator<Node | undefined, Node | undefined, Node | undefined> {
    for (const n of node.childrenIter()) {
        const t = await checker.getTypeAtLocation(n);
        if (t.flags & TypeFlags.Any) { return n; }
        const res = yield* visitNodeForFirstAnyChild(n)
        if (res) { return res; }
    }
}
const res = (await visitNodeForFirstAnyChild(stmt).next()).value;
assert.strictEqual(res!.getText(), "p: any");

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Factory-created nodes lack the advertised method, and falsy return values are not passed through.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds generator-based child traversal to the TypeScript AST API.

Changes:

  • Adds Node.childrenIter.
  • Generates traversal for remote nodes.
  • Adds synchronous and asynchronous traversal tests.
File summaries
File Description
tools/scripts/tsc/generate-encoder.ts Generates remote child iteration.
packages/typescript/src/api/node/node.generated.ts Implements iteration on RemoteNode.
packages/typescript/src/ast/ast.ts Exposes the method on Node.
packages/typescript/test/sync/ast.test.ts Tests traversal and async usage.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/typescript/src/ast/ast.ts
Comment thread tools/scripts/tsc/generate-encoder.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

2 participants