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

Method extract_variable

codegraph-kernel/src/tsjs/extractors.rs:795–965  ·  view source on GitHub ↗

Returns the initializers it walked under the symbol they declare — a constant's or a variable's (#693), a component wrapper's — whose fn-ref candidates that walk already took for the symbol (the caller's scan of the declaration skips them; it would take each again for the file). Mirrors TreeSitterExtractor.extractVariable.

(&mut self, node: Node<'t>)

Source from the content-addressed store, hash-verified

793 /// the declaration skips them; it would take each again for the file).
794 /// Mirrors TreeSitterExtractor.extractVariable.
795 pub(super) fn extract_variable(&mut self, node: Node<'t>) -> Option<HashSet<usize>> {
796 let mut walked: Option<HashSet<usize>> = None;
797 let is_const = self.is_const_decl(node);
798 let kind: &'static str = if is_const { "constant" } else { "variable" };
799 let docstring = crate::docstring::preceding_docstring(node, self.src);
800 let is_exported = self.is_exported(node); // `?? false` — always present
801
802 for child in node.named_children(&mut node.walk()) {
803 if child.kind() != "variable_declarator" {
804 continue;
805 }
806 let Some(name_node) = child.child_by_field_name("name") else { continue };
807 let value = child.child_by_field_name("value");
808
809 // Destructured patterns mint no node — except RTK Query generated
810 // hooks (`export const { useGetXQuery } = api`).
811 if matches!(name_node.kind(), "object_pattern" | "array_pattern") {
812 if name_node.kind() == "object_pattern"
813 && value.map(|v| v.kind() == "identifier").unwrap_or(false)
814 {
815 self.extract_rtk_hook_bindings(name_node, is_exported);
816 }
817 // The initializer (and any default in the pattern) still runs:
818 // walk the declarator as a function body walks it, with the
819 // enclosing scope on the stack (#2340). Mirrors
820 // TreeSitterExtractor.extractVariable.
821 self.visit_function_body(child);
822 continue;
823 }
824 let name = self.text(name_node).to_string();
825
826 // Arrow/function/generator values extract as functions, named by the declarator.
827 if let Some(v) = value {
828 if matches!(v.kind(), "arrow_function" | "function_expression" | "generator_function") {
829 self.extract_function(v, None);
830 continue;
831 }
832 }
833
834 let init_signature = value.map(|v| util::init_signature(self.text(v)));
835
836 // React HOC-wrapped components (#841), PascalCase-gated.
837 if let Some(v) = value {
838 if util::pascal_case().is_match(&name) {
839 if let Some(inner) = self.react_component_hoc(v) {
840 self.extract_react_component_node(
841 &name,
842 child,
843 inner,
844 Extra {
845 docstring: docstring.clone(),
846 signature: init_signature.clone(),
847 is_exported: Some(is_exported),
848 ..Extra::default()
849 },
850 );
851 if inner.is_none() {
852 walked.get_or_insert_with(HashSet::new).insert(v.id());

Callers 1

visit_nodeMethod · 0.45

Calls 15

preceding_docstringFunction · 0.85
init_signatureFunction · 0.85
is_vue_collection_nameFunction · 0.85
is_const_declMethod · 0.80
mapMethod · 0.80
react_component_hocMethod · 0.80
idMethod · 0.80
owned_object_valueMethod · 0.80

Tested by

no test coverage detected