microsoft / microsoft/TypeScript

Compiler API: allow retrieving Symbols for anonymous function and classes

Open
#55,433 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 Search Terms

"compiler api anonymous"
"anonymous getSymbolAtLocation"

✅ Viability Checklist
  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion

Today when working with AST tree it's convenient to retrieve Symbols of function/classes/variables and use them to represent program "entities". For named function and classes Symbol can be retrieved from a node using typeChecker.getSymbolAtLocation(node.name). Unfortunately for anonymous nodes there doesn't seem to be a way to get Symbol. Even though Symbol is present on the Node object but it's not in public API.

Additionally mentally it's easier to assume that all functions, classes and variables have Symbols.

This is similar to https://github.com/microsoft/TypeScript/issues/26511

Example using ts-ast-viewer:
image

📃 Motivating Example

No motivating example really. It's pretty niche compiler API improvement that makes working with Compiler more consistent and helps to avoid implementing workarounds.

💻 Use Cases
  1. What do you want to use this for?
    It's needed for proper implementation of TS indexer in kythe.io.

  2. What shortcomings exist with current approaches?
    Code indexing is built around having Symbols. Places where Symbol is possible to retrieve using public API (e.g. anonymous functions) require hacks or introducing custom symbol-like entity.

  3. What workarounds are you using in the meantime?
    Acessing internal Node.symbol property.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the public typeChecker.getSymbolAtLocation entry point and compare it with the internal Node.symbol property described in the issue. Review the related issue #26511 and determine the public API shape needed for anonymous functions and classes; done means their Symbols can be retrieved without accessing internal properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.