Skip to content

Compiler API: Expose More from typescript.d.ts #23719

Open
@SlurpTheo

Description

@SlurpTheo

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

Activity

ajafff

ajafff commented on Apr 26, 2018

@ajafff
Contributor

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.

mhegazy

mhegazy commented on Apr 26, 2018

@mhegazy
Contributor

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.

added
SuggestionAn idea for TypeScript
APIRelates to the public API for TypeScript
on Apr 26, 2018
added this to the Community milestone on Apr 26, 2018
SlurpTheo

SlurpTheo commented on Apr 27, 2018

@SlurpTheo
Author

@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.

mhegazy

mhegazy commented on Apr 27, 2018

@mhegazy
Contributor

correct.

granmoe

granmoe commented on Mar 6, 2019

@granmoe

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.

jirutka

jirutka commented on Oct 11, 2019

@jirutka

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

Zemnmez

Zemnmez commented on Feb 8, 2020

@Zemnmez

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jirutka@Zemnmez@SlurpTheo@granmoe@RyanCavanaugh

        Issue actions

          Compiler API: Expose More from typescript.d.ts · Issue #23719 · microsoft/TypeScript