question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compiler API: Expose More from typescript.d.ts

See original GitHub issue

TypeScript Version: 2.9.0-dev.20180426

While Traversing the TypeScript AST of a source file, there have been a number of helpful things I’ve found by extending ts.* from typescript.d.ts and I would like it if these could be exposed (or not marked /* @internal */).

Search Terms: typescript.d.ts Node.locals Symbol.parent getContainingFunction isAssignmentTarget

Code

declare namespace ts {
    interface Node {
        /* @internal */ readonly locals?: SymbolTable; // Locals associated with node (initialized by binding)
    }

    interface Symbol {
        /* @internal */ readonly parent?: Symbol; // Parent symbol
    }

    function getContainingFunction(node: Node): SignatureDeclaration | undefined;

    function isAssignmentTarget(node: Node): boolean;
}

Note:

  • Added readonly for Node#locals & Symbol#parent
  • For Symbol#parent, the goal is: given the symbol for a method (e.g., methodSymbol for Number#toString) find where that method is defined (e.g., Number)… maybe program.getTypeChecker().getSymbolAtLocation(methodSymbol.valueDeclaration.parent) is what is “supposed” to be used instead?
  • For ts.getContainingFunction() it seems more appropriate to have a return type of SignatureDeclaration | undefined instead of SignatureDeclaration

Related Issues: #15841

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jirutkacommented, Oct 11, 2019

@granmoe, take a look at TypeChecker#getSymbolsInScope (you can get its instance from the program).

2reactions
Zemnmezcommented, Feb 8, 2020

I second symbol.parent! It’s very hard to operate on symbol trees without it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Compiler and Compiler API (Part 2) | by Sunny Sun
TypeScript Compiler API is a hidden gem. The compiler API exposes extremely powerful tools to process TypeScript code programmatically, and ...
Read more >
Documentation - Modules .d.ts - TypeScript
Types in Modules​​ Now the type of the array propagates into the ArrayMetadata type. The types which are exported can then be re-used...
Read more >
Processing TypeScript using TypeScript - Convinced Coder
One of the interesting things about TypeScript is that it exposes a compiler API that you can use to process TypeScript code ...
Read more >
Parsing d.ts files using typescript compiler-Week 1 GSoC'21
Now we not only can make typescript compiler to execute our typescript files but we can also import it as a module in...
Read more >
How can I use the TS Compiler API to find where a variable ...
For resolving module names, use ts.resolveModuleName , as recommended on this TS issue. Here is the signature, from typescript.d.ts :
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found