typescript-node-ids
Version:
TypeScript support for naming Nodes uniquely and query them per SourceFile. This really make sense in libraries like ts-simple-ast when modifying AST in a loop. Implementations for native typescript compiler API and ts-simple-ast library
87 lines (86 loc) • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_1 = require("typescript");
const typescript_ast_util_1 = require("typescript-ast-util");
class TypeScriptImpl {
/**
* Installs ids on each descendants of given node. Use `this.getId` on any node to obtain a node's id or
* `getNodeById` to query a node from a parent one using its id.
*
* In general users will provide a SourceFile so the entire AST Nodes filled with ids, but it could also pass
* a fragment hierarchy, for example, to be faster in case only that fragment is invalidated / modified (see
* uninstall method)
*
* If given node's parent doesn't' have an id, the whole source file containing given node will be used as parent.
*
*/
install(node) {
let startingId;
if (typescript_1.isSourceFile(node)) {
startingId = '0';
}
else if (node.parent && this.getId(node.parent)) {
startingId = this.getId(node.parent);
}
else {
// installing ids on whole source tree since given node's parent doesn't have id
node = node.getSourceFile();
startingId = '0';
}
this.setId(node, startingId);
typescript_ast_util_1.visitChildrenRecursiveDeepFirst(node, (child, index, level) => this.setId(child, `${child.parent ? this.getId(child.parent) : '0'}.${index}`));
return node;
}
/**
* User could choose to put ids only to certain nodes and don't use install at all which name all AST's nodes
*/
setId(node, id) {
node.__$node$_$id$__ = id;
}
/**
* Returns given node's id or throws exception if node has't one
*/
getIdOrThrow(node) {
if (!this.getId(node)) {
throw new Error(`Expected node ${typescript_ast_util_1.getKindName(node)} to have an Id`);
}
return this.getId(node);
}
/**
* Returns given node's id or undefined if it hasn't one
*/
getId(node) {
return node.__$node$_$id$__;
}
/**
* removes given node's id and returns it.
*/
deleteId(node) {
const id = node.__$node$_$id$__;
delete node.__$node$_$id$__;
return id;
}
/**
* Remove given node and all its descendants ids.
*
* This could be useful when user modify the ast and wants to invalidate that AST so further getNodeById calls won't find anything
*/
uninstall(node) {
typescript_ast_util_1.visitChildrenRecursiveDeepFirst(node, c => (this.deleteId(c), undefined));
// throw new Error('Not implemented')
}
/**
* Return the first descendant of given parent node with given id or undefined if none is found
*/
getNodeById(parent, id) {
// TODO very very slow implementation we should use next getChildMatchingId declaration or similar - get indexes and kinds from ids, never iterate1
return typescript_ast_util_1.findChild(parent, child => this.getId(child) === id);
}
}
exports.TypeScriptImpl = TypeScriptImpl;
// export typescript
function createTypeScriptImpl() {
return new TypeScriptImpl();
}
exports.createTypeScriptImpl = createTypeScriptImpl;
exports.typeScriptImpl = createTypeScriptImpl();