API: add getChildren and token getters to Node#4440
Open
oMatheusmol wants to merge 2 commits into
Open
Conversation
Contributor
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). |
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"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #4308, completing #4216.
Adds the remaining child/token getters to the API:
getChildren, getChildCount, getChildAt, getFirstToken, getLastToken