API: add getChildren and token getters to Node - #4440
API: add getChildren and token getters to Node#4440Matheus Mol (oMatheusmol) wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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
NodeAPI withgetChildren,getChildCount,getChildAt,getFirstToken, andgetLastTokenand implement them for both local (NodeObject) and remote (RemoteNode) nodes. - Implement
astnavhelpers to materialize/cached children (including synthetic punctuation/keyword tokens andSyntaxListwrapping forNodeArrays). - 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). |
| 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"); | ||
| } | ||
| } |
|
Thank you for contributing to the TypeScript native port! Development has moved from this repository back to the main microsoft/TypeScript repository. GitHub does not have PR transfer functionality, so we're closing this PR here. If this change is still relevant, please reopen it as a new pull request in See microsoft/typescript-go#4918 for more information about the move. |
Follow-up to #4308, completing #4216.
Adds the remaining child/token getters to the API:
getChildren, getChildCount, getChildAt, getFirstToken, getLastToken