Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// taze: Set from //third_party/javascript/node_modules/typescript:es2015.collection
import * as fs from 'fs'; // from //third_party/javascript/typings/node
import * as ts from 'typescript';
function load(fileName: string, names: Set<string>) {
const text = fs.readFileSync(fileName, 'utf8');
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.ES5, true);
sf.forEachChild((node) => {
switch (node.kind) {
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.TypeAliasDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
const named = node as (ts.InterfaceDeclaration | ts.TypeAliasDeclaration |
ts.FunctionDeclaration | ts.ModuleDeclaration);
if (named.name) names.add(named.name.getText());
break;
case ts.SyntaxKind.VariableStatement:
const stmt = node as ts.VariableStatement;
for (const decl of stmt.declarationList.declarations) {
if (decl.name.kind === ts.SyntaxKind.Identifier) {
names.add(decl.name.getText());
} else {
console.error(`TODO: '${decl.name.getText()}' var`);
}
}
break;
case ts.SyntaxKind.EndOfFileToken:
break;
default:
console.error(`TODO: ${ts.SyntaxKind[node.kind]}`);
}
});
}
const nameSet = new Set<string>();
for (const fileName of process.argv.slice(2)) {
load(fileName, nameSet);
}
const names = Array.from(nameSet);
names.sort();
for (const name of names) {
console.log(`"${name}",`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment