MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extractTypeAlias

Method extractTypeAlias

src/extraction/tree-sitter.ts:2896–2998  ·  view source on GitHub ↗

* Extract a type alias (e.g. `export type X = ...` in TypeScript). * For languages like Go, resolveTypeAliasKind detects when the type_spec * wraps a struct or interface definition and creates the correct node kind. * Returns true if children should be skipped (struct/interface handled body

(node: SyntaxNode)

Source from the content-addressed store, hash-verified

2894 * Returns true if children should be skipped (struct/interface handled body visiting).
2895 */
2896 private extractTypeAlias(node: SyntaxNode): boolean {
2897 if (!this.extractor) return false;
2898
2899 const name = extractName(node, this.source, this.extractor);
2900 if (name === '<anonymous>') return false;
2901 const docstring = getPrecedingDocstring(node, this.source);
2902 const isExported = this.extractor.isExported?.(node, this.source);
2903
2904 // Check if this type alias is actually a struct or interface definition
2905 // (e.g. Go: `type Foo struct { ... }` is a type_spec wrapping struct_type)
2906 const resolvedKind = this.extractor.resolveTypeAliasKind?.(node, this.source);
2907
2908 if (resolvedKind === 'struct') {
2909 const structNode = this.createNode('struct', name, node, { docstring, isExported });
2910 if (!structNode) return true;
2911 // Visit body children for field extraction
2912 this.nodeStack.push(structNode.id);
2913 // Try Go-style 'type' field first, then find inner struct child (C typedef struct)
2914 const typeChild = getChildByField(node, 'type')
2915 || this.findChildByTypes(node, this.extractor.structTypes);
2916 if (typeChild) {
2917 // Extract struct embedding (e.g. Go: `type DB struct { *Head; Queryable }`)
2918 this.extractInheritance(typeChild, structNode.id);
2919 const body = getChildByField(typeChild, this.extractor.bodyField) || typeChild;
2920 for (let i = 0; i < body.namedChildCount; i++) {
2921 const child = body.namedChild(i);
2922 if (child) this.visitNode(child);
2923 }
2924 }
2925 this.nodeStack.pop();
2926 return true;
2927 }
2928
2929 if (resolvedKind === 'enum') {
2930 const enumNode = this.createNode('enum', name, node, { docstring, isExported });
2931 if (!enumNode) return true;
2932 this.nodeStack.push(enumNode.id);
2933 // Find the inner enum type child (e.g. C: typedef enum { ... } name)
2934 const innerEnum = this.findChildByTypes(node, this.extractor.enumTypes);
2935 if (innerEnum) {
2936 this.extractInheritance(innerEnum, enumNode.id);
2937 const body = this.extractor.resolveBody?.(innerEnum, this.extractor.bodyField)
2938 ?? getChildByField(innerEnum, this.extractor.bodyField);
2939 if (body) {
2940 const memberTypes = this.extractor.enumMemberTypes;
2941 for (let i = 0; i < body.namedChildCount; i++) {
2942 const child = body.namedChild(i);
2943 if (!child) continue;
2944 if (memberTypes?.includes(child.type)) {
2945 this.extractEnumMembers(child);
2946 } else {
2947 this.visitNode(child);
2948 }
2949 }
2950 }
2951 }
2952 this.nodeStack.pop();
2953 return true;

Callers 1

visitNodeMethod · 0.95

Calls 13

createNodeMethod · 0.95
findChildByTypesMethod · 0.95
extractInheritanceMethod · 0.95
visitNodeMethod · 0.95
extractEnumMembersMethod · 0.95
getPrecedingDocstringFunction · 0.90
getChildByFieldFunction · 0.90
extractNameFunction · 0.85

Tested by

no test coverage detected