diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..2152bd0 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,43 @@ +import * as ts from "typescript"; + +// Copied from ts-ast-viewer +export function forEachChild(node: ts.Node) { + const nodes: ts.Node[] = []; + node.forEachChild(child => { + nodes.push(child); + return undefined; + }); + return nodes; +} + +// Copied from ts-ast-viewer +function getSyntaxKindNameMapping() { + // some SyntaxKinds are repeated, so only use the first one + const kindNames: { [kind: number]: string } = {}; + for (const name of Object.keys(ts.SyntaxKind).filter(k => + isNaN(parseInt(k, 10)) + )) { + const value = (ts.SyntaxKind as any)[name] as number; + if (kindNames[value] == null) kindNames[value] = name; + } + return kindNames; +} + +export const syntaxKindNameMapping = getSyntaxKindNameMapping(); + +export class ObjectHash { + static ObjectIDMap = new WeakMap(); + static maxID = 0; + + static getHash(obj: Object) { + let returnID = ObjectHash.ObjectIDMap.get(obj); + if (returnID === undefined) { + this.maxID++; + returnID = this.maxID; + + this.ObjectIDMap.set(obj, returnID); + } + + return returnID; + } +}