Skip to content

Make NodeHandle generic and generate .Handle members of is-guards for guarding node handles (sync and async) - #4887

Merged
Wesley Wigham (weswigham) merged 4 commits into
microsoft:mainfrom
weswigham:generic-node-handle
Aug 14, 2026
Merged

Make NodeHandle generic and generate .Handle members of is-guards for guarding node handles (sync and async)#4887
Wesley Wigham (weswigham) merged 4 commits into
microsoft:mainfrom
weswigham:generic-node-handle

Conversation

@weswigham

Copy link
Copy Markdown
Member

So you can, for example:

isSignatureDeclaration.Handle(sig.declaration)

and narrow sig.declaration.

Note that I am duplicating the implementations for the .Handle type guards, but we could write func.Handle = func as any and it'd work just as well, if we'd prefer the brevity.

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 generic node handles and generated handle-aware AST guards for async and sync APIs.

Changes:

  • Makes NodeHandle<T> resolve typed AST nodes.
  • Adds .Handle variants to generated AST guards.
  • Adds async and sync narrowing coverage.

Reviewed changes

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

Show a summary per file
File Description
_scripts/generate-ts-ast.ts Generates handle-aware guards.
_packages/native-preview/src/ast/is.generated.ts Contains generated .Handle guards.
_packages/native-preview/src/api/async/api.ts Adds generic async handles.
_packages/native-preview/src/api/sync/api.ts Adds generated generic sync handles.
_packages/native-preview/src/api/async/types.ts Types index declarations.
_packages/native-preview/src/api/sync/types.ts Mirrors typed index declarations.
_packages/native-preview/test/async/api.test.ts Tests async narrowing.
_packages/native-preview/test/sync/api.test.ts Tests sync narrowing.

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

out.push(`} from "./ast.ts";`);
out.push(`import type { NodeHandle as AsyncNodeHandle } from "../api/async/api.ts";`);
out.push(`import type { NodeHandle as SyncNodeHandle } from "../api/sync/api.ts";`);
out.push(`type NodeHandleLike<out T extends Node> = { kind: SyntaxKind; resolve(...args: any): any; };`);
Comment thread _scripts/generate-ts-ast.ts Outdated
Comment on lines +1459 to +1461
out.push(`${g.funcName}.Handle = <T extends NodeHandleLike<Node>>(node: T): node is SpecializeNodeHandle<T, ${g.typeName}> => {`);
out.push(...functionContent);
out.push(`}`)

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 think this is a good suggestion

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, which is why I made it in the OP - only drawback is we lose typechecking of the NodeHandle guard implementations, so it's on us to make sure the implementation is valid for the type.

Comment thread _scripts/generate-ts-ast.ts Outdated
Comment thread _scripts/generate-ts-ast.ts Outdated
Comment thread _packages/native-preview/test/async/api.test.ts Outdated

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 think I caused a merge conflict, sorry 🥴

@weswigham
Wesley Wigham (weswigham) added this pull request to the merge queue Aug 14, 2026
Merged via the queue into microsoft:main with commit 3e58dfc Aug 14, 2026
21 checks passed
@weswigham
Wesley Wigham (weswigham) deleted the generic-node-handle branch August 14, 2026 00:52
Jake Bailey (jakebailey) pushed a commit to jakebailey/TypeScript that referenced this pull request Aug 14, 2026
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.

3 participants