Typescript: Compiler API: Expose More from typescript.d.ts

Created on 26 Apr 2018  路  7Comments  路  Source: microsoft/TypeScript

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

API Suggestion help wanted

Most helpful comment

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

All 7 comments

There's a package that provides declarations even for the internal stuff: https://www.npmjs.com/package/byots

Regarding isAssignmentTarget: you might want to use isReassignmentTarget of my library tsutils.

I do not think we want to expose symbol.parent, nor locals. these are internal implementation details that we can change with no prior notice.

getContainingFunction andisAssignmentTarget are fine though. we would take a PR to expose them.

@mhegazy So instead of symbol.parent is my second note along the right plan of action (get symbol's valueDeclaration, get its parent, get its symbol)?

I'll have to dig more into why I needed node.locals sometime soon.

correct.

Is there any reliable way to find all symbols available in module scope without using node.locals? This is what I desperately wish to do.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

uber5001 picture uber5001  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

seanzer picture seanzer  路  3Comments