microsoft / microsoft/TypeScript
Compiler API: allow retrieving Symbols for anonymous function and classes
Nobody has claimed this yet.
- 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:
📃 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
-
What do you want to use this for?
It's needed for proper implementation of TS indexer in kythe.io. -
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. -
What workarounds are you using in the meantime?
Acessing internalNode.symbolproperty.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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