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

Method extract_function

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

Source from the content-addressed store, hash-verified

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