Skip to content

Allow getSymbol on any Node, overload for Declaration - #65647

Merged
Andrew Branch (andrewbranch) merged 1 commit into
microsoft:mainfrom
andrewbranch:symbol-lookup-overloads
Nov 24, 2026
Merged

Andrew Branch (andrewbranch) merged 1 commit into
microsoft:mainfrom
andrewbranch:symbol-lookup-overloads

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Someone pointed out that because SourceFile wasn't a Declaration, the types reject putting into getSymbol. That was half-right; SourceFiles are only Declarations if they're modules. Accept any Node in getSymbol, with an overload for Declaration that preserves the non-optional result. Also, make isExternalModule narrow its input to include Declaration.

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.

🟡 Changes recommended

Symbol-less declarations now return undefined despite overloads promising a non-optional Symbol, allowing type-checked callers to crash rather than receiving a rejected lookup.

1 open issue
What changed in this PR

Broadens the TypeScript API’s getSymbol lookup to accept any Node, including source files, while retaining non-optional overloads for declarations.

Changes:

  • Updates async, sync, and generator APIs to handle and cache missing symbols.
  • Makes the server response nullable and adds declaration narrowing to isExternalModule.
  • Adds coverage for arbitrary nodes, module symbols, and cached missing-symbol results.
File Description
tsc/​internal/​api/​session.go Allows arbitrary nodes and nullable symbol responses.
tsc/​internal/​api/​session_createsourcefile_test.go Tests lookup of a node without a symbol.
packages/​typescript/​test/​sync/​api.test.ts Tests widened sync lookups and module narrowing.
packages/​typescript/​test/​sync/​api-generators.test.ts Tests generator lookups and missing-symbol caching.
packages/​typescript/​test/​async/​api.test.ts Tests widened async lookups and module narrowing.
packages/​typescript/​src/​ast/​utils.ts Adds a declaration type predicate to isExternalModule.
packages/​typescript/​src/​api/​sync/​api.ts Updates sync and generator overloads and caching.
packages/​typescript/​src/​api/​sourceFileCache.ts Allows cached and pending results without symbols.
packages/​typescript/​src/​api/​proto.generated.ts Marks symbol lookup responses as nullable.
packages/​typescript/​src/​api/​async/​api.ts Updates async overloads, caching, and sync-generation source.

Review effort: Balanced

symbol := node.Symbol()
if symbol == nil {
return nil, fmt.Errorf("%w: declaration node index %d has no binder symbol", ErrClientError, params.Index)
return nil, nil

@andrewbranch Andrew Branch (andrewbranch) Nov 24, 2026 •

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.

I wonder if we can split VariableDeclaration into IdentifierVariableDeclaration (a real Declaration) and PatternVariableDeclaration (not a real Declaration) 🤔

@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Nov 24, 2026
@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Nov 24, 2026
Merged via the queue into microsoft:main with commit f15678d Nov 24, 2026
61 of 63 checks passed
@andrewbranch
Andrew Branch (andrewbranch) deleted the symbol-lookup-overloads branch November 24, 2026 17:52
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants