Skip to content

Instantly share code, notes, and snippets.

@devversion
Created February 8, 2017 22:22
Show Gist options
  • Save devversion/8ff221a6f5e95a1799b72782af99535e to your computer and use it in GitHub Desktop.
Save devversion/8ff221a6f5e95a1799b72782af99535e to your computer and use it in GitHub Desktop.
TypeScript Member Heritage AST
sourceFiles.forEach(function(file) {
ts.forEachChild(file, visitNode);
});
/**
* @param {ts.Node} node
**/
function visitNode(node) {
if (!isNodeExported(node)) {
return;
}
if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
ts.forEachChild(node, visitNode);
}
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
visitClass(node);
}
console.log(ts.formatSyntaxKind(node.kind));
}
/**
* @param {ts.ClassDeclaration} node
*/
function visitClass(node) {
var clazz = typeChecker.getSymbolAtLocation(node.name);
var members = node.members;
if (node.heritageClauses) {
var deps = node.heritageClauses.map(heritageToSymbol);
deps.forEach(function(dependency) {
members = members.concat(dependency.members);
});
}
console.log(clazz.getName(), members.length);
}
/**
* @type {ts.HeritageClause} heritage
* */
function heritageToSymbol(heritage) {
if (heritage.token !== ts.SyntaxKind.ExtendsKeyword || !heritage.types) {
return;
}
var heritages = heritage.types.map(function(expression) {
return typeChecker.getTypeAtLocation(expression).getSymbol();
});
return heritages.reduce(function(dependencies, current) {
return dependencies.concat(current);
}, []);
}
function isNodeExported(node) {
return (node.flags & ts.NodeFlags.Export) !== 0 || (node.parent && node.parent.kind === ts.SyntaxKind.SourceFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment