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:
readonly for Node#locals & Symbol#parentSymbol#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?ts.getContainingFunction() it seems more appropriate to have a return type of SignatureDeclaration | undefined instead of SignatureDeclarationRelated Issues:
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.
Most helpful comment
@granmoe, take a look at
TypeChecker#getSymbolsInScope(you can get its instance from theprogram).