ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
41 lines (40 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var typescript_1 = require("../../typescript");
var NodeHandlerHelper = /** @class */ (function () {
function NodeHandlerHelper(compilerFactory) {
this.compilerFactory = compilerFactory;
}
NodeHandlerHelper.prototype.handleForValues = function (handler, currentNode, newNode, newSourceFile) {
if (this.compilerFactory.hasCompilerNode(currentNode))
handler.handleNode(this.compilerFactory.getExistingCompilerNode(currentNode), newNode, newSourceFile);
else if (currentNode.kind === typescript_1.SyntaxKind.SyntaxList) {
// always handle syntax lists because their children might be in the cache
// todo: pass this in for performance reasons
var sourceFile = this.compilerFactory.getExistingCompilerNode(currentNode.getSourceFile());
handler.handleNode(this.compilerFactory.getNodeFromCompilerNode(currentNode, sourceFile), newNode, newSourceFile);
}
};
NodeHandlerHelper.prototype.forgetNodeIfNecessary = function (currentNode) {
if (this.compilerFactory.hasCompilerNode(currentNode))
this.compilerFactory.getExistingCompilerNode(currentNode).forget();
};
/**
* Gets the children of the node according to whether the tokens have previously been parsed.
*/
NodeHandlerHelper.prototype.getChildrenFast = function (currentNode, newNode, newSourceFile) {
if (currentNode._hasParsedTokens())
return [currentNode._getCompilerChildren(), newNode.getChildren(newSourceFile)];
// great, we don't have to parse the tokens and can instead just use ts.forEachChild (faster)
return [getForEachChildren(currentNode.compilerNode), getForEachChildren(newNode)];
function getForEachChildren(node) {
var nodes = [];
typescript_1.ts.forEachChild(node, function (childNode) {
nodes.push(childNode);
});
return nodes;
}
};
return NodeHandlerHelper;
}());
exports.NodeHandlerHelper = NodeHandlerHelper;