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

Function handleFunDecl

src/extraction/languages/erlang.ts:134–184  ·  view source on GitHub ↗
(node: SyntaxNode, ctx: ExtractorContext)

Source from the content-addressed store, hash-verified

132}
133
134function handleFunDecl(node: SyntaxNode, ctx: ExtractorContext): boolean {
135 const clauses = node.namedChildren.filter((c) => c.type === 'function_clause');
136 const first = clauses[0];
137 if (!first) return true; // macro-templated clause (`?M(...) -> ...`) — no static name
138 const nameNode = getChildByField(first, 'name');
139 if (!nameNode) return true;
140 const name = atomText(nameNode, ctx.source);
141 if (!name) return true;
142 const arity = nodeArity(first);
143
144 // Continuation clause of the SAME function (same name AND arity): extend the
145 // existing node's span and attribute this clause's calls to it.
146 if (ctx.filePath === lastFnFile && name === lastFnName && arity === lastFnArity && lastFnId) {
147 for (let i = ctx.nodes.length - 1; i >= 0; i--) {
148 const n = ctx.nodes[i];
149 if (n && n.id === lastFnId) {
150 if (node.endPosition.row + 1 > n.endLine) n.endLine = node.endPosition.row + 1;
151 break;
152 }
153 }
154 ctx.pushScope(lastFnId);
155 for (const clause of clauses) ctx.visitFunctionBody(clause, lastFnId);
156 ctx.popScope();
157 return true;
158 }
159
160 const spec = precedingSpec(node, name, arity, ctx.source);
161 const exports = moduleExports(node, ctx.source, ctx.filePath);
162 const fn = ctx.createNode('function', name, node, {
163 docstring: getPrecedingDocstring(spec ?? node, ctx.source),
164 signature: spec
165 ? collapseWs(getNodeText(spec, ctx.source)).slice(0, 300)
166 : clauseHeader(first, ctx.source),
167 isExported: exports === 'all' || exports.has(`${name}/${arity}`) || exports.has(name),
168 });
169 if (!fn) return true;
170 // Arity is part of the function's identity — carry it on the qualified name
171 // (`mod::f/2`), the canonical Erlang spelling and the only persisted slot.
172 // The node NAME stays bare so name search and bare-name matching still work.
173 fn.qualifiedName = `${fn.qualifiedName}/${arity}`;
174 ctx.pushScope(fn.id);
175 // The whole clause is walked (not just the body) so record patterns in the
176 // arguments and guard calls contribute references too.
177 for (const clause of clauses) ctx.visitFunctionBody(clause, fn.id);
178 ctx.popScope();
179 lastFnFile = ctx.filePath;
180 lastFnName = name;
181 lastFnArity = arity;
182 lastFnId = fn.id;
183 return true;
184}
185
186function handleRecordDecl(node: SyntaxNode, ctx: ExtractorContext): boolean {
187 const nameNode = getChildByField(node, 'name');

Callers 1

erlang.tsFile · 0.85

Calls 14

getChildByFieldFunction · 0.90
getPrecedingDocstringFunction · 0.90
getNodeTextFunction · 0.90
atomTextFunction · 0.85
nodeArityFunction · 0.85
precedingSpecFunction · 0.85
moduleExportsFunction · 0.85
collapseWsFunction · 0.85
clauseHeaderFunction · 0.85
pushScopeMethod · 0.80
popScopeMethod · 0.80
hasMethod · 0.80

Tested by

no test coverage detected