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

Method extract_function

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

Source from the content-addressed store, hash-verified

27 // --- extractFunction --------------------------------------------------------
28
29 pub(super) fn extract_function(&mut self, node: Node<'t>, name_override: Option<String>) {
30 // `Ctor.prototype.m = function () {…}` / `Ctor.m = function () {…}`: a
31 // method of the constructor (or class) the scope declares (#2966).
32 if name_override.is_none() && matches!(node.kind(), "function_expression" | "arrow_function" | "generator_function") {
33 if let Some(assignment) = node.parent().filter(|p| p.kind() == "assignment_expression") {
34 if let Some(member) = self.constructor_member(assignment, node) {
35 self.extract_constructor_member(node, assignment, member);
36 return;
37 }
38 }
39 }
40
41 let mut name = name_override
42 .clone()
43 .unwrap_or_else(|| self.extract_name(node));
44
45 // Arrow/function-expression values: resolve the name from the parent
46 // variable_declarator (`export const useAuth = () => {}`), or from a
47 // CommonJS export assignment (`exports.getItems = async () => {}`,
48 // #1675). Mirrors TreeSitterExtractor.extractFunction.
49 let mut common_js_export = false;
50 if name_override.is_none()
51 && name == "<anonymous>"
52 && matches!(node.kind(), "arrow_function" | "function_expression" | "generator_function")
53 {
54 if let Some(parent) = node.parent() {
55 if parent.kind() == "variable_declarator" {
56 if let Some(var_name) = parent.child_by_field_name("name") {
57 name = self.text(var_name).to_string();
58 }
59 } else if parent.kind() == "assignment_expression" {
60 if let Some(export_name) = self.common_js_export_name(parent, node) {
61 name = export_name;
62 common_js_export = true;
63 }
64 }
65 }
66 }
67 if name == "<anonymous>" {
68 // Still walk the body: module wrappers hold named inner functions
69 // and calls that would otherwise be lost (#528).
70 let body = body_of(node);
71 self.visit_parameter_defaults(node, body);
72 if let Some(body) = body {
73 self.visit_function_body(body);
74 }
75 return;
76 }
77
78 let exported_member = self.exported_member_fn == Some(node.id());
79 let extra = Extra {
80 docstring: crate::docstring::preceding_docstring(node, self.src),
81 signature: self.signature_of(node),
82 visibility: self.visibility_of(node),
83 is_exported: Some(common_js_export || exported_member || self.is_exported(node)),
84 is_async: Some(self.is_async(node)),
85 is_static: self.is_static(node),
86 ..Extra::default()

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
constructor_memberMethod · 0.80
common_js_export_nameMethod · 0.80
idMethod · 0.80
pushMethod · 0.80
textMethod · 0.65
extract_nameMethod · 0.45
visit_function_bodyMethod · 0.45
signature_ofMethod · 0.45

Tested by

no test coverage detected