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

Method extractFunction

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

* Extract a function

(node: SyntaxNode, nameOverride?: string)

Source from the content-addressed store, hash-verified

2329 * Extract a function
2330 */
2331 private extractFunction(node: SyntaxNode, nameOverride?: string): void {
2332 if (!this.extractor) return;
2333
2334 // If the language provides getReceiverType and this function has a receiver
2335 // (e.g., Rust function_item inside an impl block), extract as method instead
2336 if (this.extractor.getReceiverType?.(node, this.source)) {
2337 this.extractMethod(node);
2338 return;
2339 }
2340
2341 // nameOverride is supplied only for explicitly-named anonymous functions the
2342 // caller resolved itself (e.g. arrow values of exported-const object members
2343 // — SvelteKit actions). Inline-object arrows reached by the general walker
2344 // get no override, so they still fall through to the <anonymous> skip below.
2345 let name = nameOverride ?? extractName(node, this.source, this.extractor);
2346 // A CommonJS export assignment names the function it holds — see below.
2347 let commonJsExport = false;
2348 // For arrow functions and function expressions assigned to variables,
2349 // resolve the name from the parent variable_declarator.
2350 // e.g. `export const useAuth = () => { ... }` — the arrow_function node
2351 // has no `name` field; the name lives on the variable_declarator.
2352 if (
2353 !nameOverride &&
2354 name === '<anonymous>' &&
2355 (node.type === 'arrow_function' || node.type === 'function_expression' || node.type === 'generator_function')
2356 ) {
2357 const parent = node.parent;
2358 if (parent?.type === 'variable_declarator') {
2359 const varName = getChildByField(parent, 'name');
2360 if (varName) {
2361 name = getNodeText(varName, this.source);
2362 }
2363 } else if (parent?.type === 'assignment_expression') {
2364 // `exports.getItems = async (req, res) => {…}` / `module.exports.x =
2365 // function () {…}` — the CommonJS controller style. The function is
2366 // anonymous only syntactically: the export property is the name every
2367 // `router.get('/items', getItems)` resolves. Without a node the handler
2368 // is invisible to callers/impact and its calls attribute to the file
2369 // (#1675). Same treatment `const X = () => {}` already gets.
2370 const exportName = this.commonJsExportName(parent, node);
2371 if (exportName) {
2372 name = exportName;
2373 commonJsExport = true;
2374 }
2375 }
2376 }
2377 if (name === '<anonymous>') {
2378 // Don't emit a node for the anonymous wrapper itself, but still visit its
2379 // body: AMD/RequireJS and CommonJS module wrappers (`define([], function(){…})`,
2380 // `(function(){…})()`) hold named inner functions and calls that would
2381 // otherwise be lost — the dispatcher set skipChildren, so nothing else
2382 // descends into this subtree. (#528)
2383 const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
2384 ?? getChildByField(node, this.extractor.bodyField);
2385 this.visitParameterDefaults(node, body);
2386 if (body) {
2387 this.visitFunctionBody(body, '');
2388 }

Callers 8

dispatchNodeMethod · 0.95
extractMethodMethod · 0.95
extractRtkEndpointsMethod · 0.95
extractPiniaSetupBodyMethod · 0.95
extractVariableMethod · 0.95

Calls 12

extractMethodMethod · 0.95
commonJsExportNameMethod · 0.95
visitFunctionBodyMethod · 0.95
docstringForMethod · 0.95
createNodeMethod · 0.95
extractDecoratorsForMethod · 0.95
getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
extractNameFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected