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

Method extract_function

codegraph-kernel/src/tsjs/extractors.rs:18–79  ·  view source on GitHub ↗
(&mut self, node: Node<'t>, name_override: Option<String>)

Source from the content-addressed store, hash-verified

16 // --- extractFunction --------------------------------------------------------
17
18 pub(super) fn extract_function(&mut self, node: Node<'t>, name_override: Option<String>) {
19 let mut name = name_override
20 .clone()
21 .unwrap_or_else(|| self.extract_name(node));
22
23 // Arrow/function-expression values: resolve the name from the parent
24 // variable_declarator (`export const useAuth = () => {}`), or from a
25 // CommonJS export assignment (`exports.getItems = async () => {}`,
26 // #1675). Mirrors TreeSitterExtractor.extractFunction.
27 let mut common_js_export = false;
28 if name_override.is_none()
29 && name == "<anonymous>"
30 && matches!(node.kind(), "arrow_function" | "function_expression" | "generator_function")
31 {
32 if let Some(parent) = node.parent() {
33 if parent.kind() == "variable_declarator" {
34 if let Some(var_name) = parent.child_by_field_name("name") {
35 name = self.text(var_name).to_string();
36 }
37 } else if parent.kind() == "assignment_expression" {
38 if let Some(export_name) = self.common_js_export_name(parent, node) {
39 name = export_name;
40 common_js_export = true;
41 }
42 }
43 }
44 }
45 if name == "<anonymous>" {
46 // Still walk the body: module wrappers hold named inner functions
47 // and calls that would otherwise be lost (#528).
48 let body = body_of(node);
49 self.visit_parameter_defaults(node, body);
50 if let Some(body) = body {
51 self.visit_function_body(body);
52 }
53 return;
54 }
55
56 let extra = Extra {
57 docstring: crate::docstring::preceding_docstring(node, self.src),
58 signature: self.signature_of(node),
59 visibility: self.visibility_of(node),
60 is_exported: Some(common_js_export || self.is_exported(node)),
61 is_async: Some(self.is_async(node)),
62 is_static: self.is_static(node),
63 ..Extra::default()
64 };
65 let Some(row) = self.create_node("function", &name, node, extra) else {
66 return;
67 };
68
69 self.extract_type_annotations(node, row);
70 self.extract_decorators_for(node, row);
71
72 self.stack.push(Scope { row, kind: "function", name });
73 let body = body_of(node);
74 self.visit_parameter_defaults(node, body);
75 if let Some(body) = body {

Callers 8

visit_nodeMethod · 0.45
extract_methodMethod · 0.45
extract_variableMethod · 0.45
extract_rtk_endpointsMethod · 0.45

Calls 15

body_ofFunction · 0.85
preceding_docstringFunction · 0.85
common_js_export_nameMethod · 0.80
pushMethod · 0.80
textMethod · 0.65
extract_nameMethod · 0.45
visit_function_bodyMethod · 0.45
signature_ofMethod · 0.45
visibility_ofMethod · 0.45
is_exportedMethod · 0.45
is_asyncMethod · 0.45

Tested by

no test coverage detected