Skip to content

API: add getChildren and token getters to Node#4440

Open
oMatheusmol wants to merge 2 commits into
microsoft:mainfrom
oMatheusmol:api/node-child-token-getters
Open

API: add getChildren and token getters to Node#4440
oMatheusmol wants to merge 2 commits into
microsoft:mainfrom
oMatheusmol:api/node-child-token-getters

Conversation

@oMatheusmol

Copy link
Copy Markdown
Contributor

Follow-up to #4308, completing #4216.
Adds the remaining child/token getters to the API:
getChildren, getChildCount, getChildAt, getFirstToken, getLastToken

Copilot AI review requested due to automatic review settings June 25, 2026 07:55

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 the remaining Node child/token getters to the native-preview TypeScript AST API (follow-up to #4308 / completion of #4216), by synthesizing token children via scanning and exposing them through both NodeObject and RemoteNode.

Changes:

  • Extend the public Node API with getChildren, getChildCount, getChildAt, getFirstToken, and getLastToken and implement them for both local (NodeObject) and remote (RemoteNode) nodes.
  • Implement astnav helpers to materialize/cached children (including synthetic punctuation/keyword tokens and SyntaxList wrapping for NodeArrays).
  • Add sync tests covering token synthesis, invariants, caching, JSDoc behavior, and error behavior on synthesized nodes.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
_scripts/generate-ts-ast.ts Updates generated factory imports so NodeObject includes the new child/token getters.
_scripts/generate-encoder.ts Updates generated remote node codegen to import/emit the new getters.
_packages/native-preview/test/sync/ast.test.ts Adds test coverage for RemoteNode child/token getters and invariants.
_packages/native-preview/src/ast/factory.generated.ts Implements the new getters on NodeObject (generated output).
_packages/native-preview/src/ast/astnav.ts Adds the core implementations for getChildren/getFirstToken/getLastToken plus caching and token synthesis.
_packages/native-preview/src/ast/ast.ts Extends the Node/SourceFile interfaces to include the new API surface and internal cache.
_packages/native-preview/src/api/node/node.generated.ts Implements the new getters on RemoteNode (generated output).

Comment on lines +613 to +617
function assertHasRealPosition(node: Node): void {
if (node.pos < 0 || node.end < 0) {
throw new Error("Node without a real position cannot be scanned and thus has no token nodes - use forEachChild and collect the result if that's fine");
}
}
Comment thread _packages/native-preview/src/ast/astnav.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants