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

Method extract_variable

codegraph-kernel/src/go.rs:681–764  ·  view source on GitHub ↗

extractVariable's Go branch: var/const specs + short_var_declaration. Returns the initializers it walked under their specs' symbols, for the declaration scan to skip.

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

Source from the content-addressed store, hash-verified

679 /// Returns the initializers it walked under their specs' symbols, for the
680 /// declaration scan to skip.
681 fn extract_variable(&mut self, node: Node<'t>) -> Vec<usize> {
682 let mut walked = Vec::new();
683 let docstring = self.docstring_of(node);
684 let is_const_decl = node.kind() == "const_declaration";
685
686 // A `var ( … )` group wraps its specs in a `var_spec_list`; a
687 // `const ( … )` group holds them directly.
688 let mut specs: Vec<Node<'t>> = Vec::new();
689 for child in node.named_children(&mut node.walk()) {
690 if child.kind() == "var_spec_list" {
691 specs.extend((0..child.named_child_count()).filter_map(|j| child.named_child(j)));
692 } else {
693 specs.push(child);
694 }
695 }
696 for spec in specs {
697 if !matches!(spec.kind(), "var_spec" | "const_spec") {
698 continue;
699 }
700 let mut var_row: Option<u32> = None;
701 if let Some(name_node) = spec.named_child(0) {
702 if name_node.kind() == "identifier" {
703 let name = self.text(name_node).to_string();
704 let value_node = if spec.named_child_count() > 1 {
705 spec.named_child(spec.named_child_count() - 1)
706 } else {
707 None
708 };
709 let signature = value_node.map(|v| util::init_signature(self.text(v)));
710 // A member of a `const ( … )` or `var ( … )` group has its
711 // own doc comment beside it in the parentheses; one without
712 // keeps the group's.
713 let doc = self.docstring_of(spec).or_else(|| docstring.clone());
714 // Exported by its own name's case, read from the spec (the
715 // declaration around it has no name).
716 let is_exported = Some(self.is_exported(spec));
717 var_row = self.create_node(
718 if is_const_decl { "constant" } else { "variable" },
719 &name,
720 spec,
721 Extra { docstring: doc, signature, is_exported, ..Extra::default() },
722 );
723 }
724 }
725 // Walk the initializer ATTRIBUTED to the declared symbol (#693).
726 if let Some(value_field) = spec.child_by_field_name("value") {
727 if let Some(row) = var_row {
728 let name = self.nodes_meta[row as usize].name.clone();
729 self.stack.push(Scope { row, kind: "variable", name });
730 self.visit_function_body(value_field);
731 self.stack.pop();
732 walked.push(value_field.id());
733 } else {
734 self.visit_function_body(value_field);
735 }
736 }
737 }
738

Callers 1

visit_nodeMethod · 0.45

Calls 11

init_signatureFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
idMethod · 0.80
textMethod · 0.65
docstring_ofMethod · 0.45
walkMethod · 0.45
is_exportedMethod · 0.45
create_nodeMethod · 0.45
visit_function_bodyMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected