Skip to content

API: add getChildren and token getters to Node - #63893

Merged
Andrew Branch (andrewbranch) merged 10 commits into
microsoft:mainfrom
oMatheusmol:api/node-child-token-getters
Sep 3, 2026
Merged

API: add getChildren and token getters to Node#63893
Andrew Branch (andrewbranch) merged 10 commits into
microsoft:mainfrom
oMatheusmol:api/node-child-token-getters

Conversation

@oMatheusmol

Copy link
Copy Markdown
Contributor

Fixes #63892

Adds the remaining child/token getters to the native API's Node: getChildren(), getChildCount(), getChildAt(), getFirstToken() and getLastToken().

Context

This is a re-submission of a contribution originally made to the typescript-go repo. I previously contributed the position/text getters in microsoft/typescript-go#4308, and this follow-up (completing microsoft/typescript-go#4216) was submitted as microsoft/typescript-go#4440. That PR was closed when development moved back to this repository, and the maintainers asked contributors to re-open their PRs here — so here it is.

Compared to the original PR, the only changes are path remappings to this repo's layout (_packages/native-preview/packages/typescript/, _scripts/tools/scripts/tsc/) and a rebase onto current main. It also incorporates the review feedback from the original PR (reusing shouldSkipChild instead of duplicating the JSDoc predicate).

Validation

  • npm run generate reproduces the generated files (node.generated.ts, factory.generated.ts) with no extra diff
  • npm run build in packages/typescript passes
  • npm test in packages/typescript: 612 tests pass, 0 fail (includes the new getChildren/token getter tests in test/sync/ast.test.ts)

Adds the remaining child/token getters to the API: getChildren, getChildCount, getChildAt, getFirstToken, getLastToken.
Copilot AI balanced review requested due to automatic review settings August 20, 2026 05:25
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 20, 2026
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Aug 20, 2026

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.

Pull request overview

Adds child and token navigation getters to the native TypeScript Node API.

Changes:

  • Implements child/token materialization and caching.
  • Adds getters to local and remote nodes via generators.
  • Adds comprehensive AST navigation tests.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tools/scripts/tsc/generate-ts-ast.ts Generates local node getters.
tools/scripts/tsc/generate-encoder.ts Generates remote node getters.
packages/typescript/test/sync/ast.test.ts Tests child/token navigation.
packages/typescript/src/ast/factory.generated.ts Adds generated local getters.
packages/typescript/src/ast/astnav.ts Implements navigation and caching.
packages/typescript/src/ast/ast.ts Extends the public node interfaces.
packages/typescript/src/api/node/node.generated.ts Adds generated remote getters.

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

Comment thread packages/typescript/src/ast/astnav.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your patience on this review!

Comment thread packages/typescript/src/ast/ast.ts Outdated
/** @internal */
tokenCache?: Map<string, Node>;
/** @internal */
childrenCache?: WeakMap<Node, readonly Node[]>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should just be a Map since it’s owned by the SourceFile that also owns the children.

Comment thread packages/typescript/src/ast/astnav.ts Outdated
if (pos >= end) {
return;
}
const scanner = getScannerForSourceFile(sourceFile, pos);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This creates a new scanner for every run of tokens between nodes. Please pass a shared one down from whatever top-level functions eventually call into this.

Comment thread packages/typescript/src/ast/astnav.ts Outdated
}

let pos = node.pos;
const consumed = new Set<Node>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this should be necessary—I think there was a bug in forEachChild that led you to add this, but it's been fixed since.


let pos = node.pos;
const consumed = new Set<Node>();
const processNode = (child: Node): undefined => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You do need to skip NodeFlags.Reparsed subtrees, like findNextToken does.

@github-project-automation github-project-automation Bot moved this from Not started to Waiting on author in PR Backlog Aug 28, 2026
@oMatheusmol

Copy link
Copy Markdown
Contributor Author

I made the changes as requested, the childrenCache is a plain Map now, getChildren uses one shared scanner per call, the consumed set is gone (you were right, it's no longer needed), and reparsed subtrees are skipped like in findNextToken.
My tests missed that last one because the corpus was .ts/.tsx only, added 16 .js/.jsx cases that fail without the fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! A lot of people are going to be excited to have these back 👍

@github-project-automation github-project-automation Bot moved this from Waiting on author to Needs merge in PR Backlog Sep 3, 2026
auto-merge was automatically disabled September 3, 2026 16:57

Head branch was pushed to by a user without write access

@oMatheusmol

Copy link
Copy Markdown
Contributor Author

my bad, I forgot to run dprint fmt, haha

@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Sep 3, 2026
Merged via the queue into microsoft:main with commit e73c923 Sep 3, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

API: Node is missing getChildren(), getFirstToken(), getLastToken() and related getters

3 participants