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

Method extract_import

codegraph-kernel/src/ruby.rs:729–790  ·  view source on GitHub ↗

extractImport — every non-body `call` lands here (importTypes:['call']). Hook (languages/ruby.ts:123): FIRST identifier named child must read require/require_relative (a lowercase receiver is found first and declines; a constant receiver — `Kernel.require "x"` — reaches the method identifier and IS a require); argument_list → string → string_content → moduleName. Then the generic imports ref and e

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

Source from the content-addressed store, hash-verified

727 /// string_content → moduleName. Then the generic imports ref and
728 /// emitRubyRequireRefs' path ref.
729 fn extract_import(&mut self, node: Node<'t>) {
730 let ident = (0..node.named_child_count())
731 .filter_map(|i| node.named_child(i))
732 .find(|c| c.kind() == "identifier");
733 let Some(ident) = ident else { return };
734 let mname = self.text(ident);
735 if mname != "require" && mname != "require_relative" {
736 return;
737 }
738 let arg_list = (0..node.named_child_count())
739 .filter_map(|i| node.named_child(i))
740 .find(|c| c.kind() == "argument_list");
741 let Some(arg_list) = arg_list else { return };
742 let string = (0..arg_list.named_child_count())
743 .filter_map(|i| arg_list.named_child(i))
744 .find(|c| c.kind() == "string");
745 let Some(string) = string else { return };
746 let content = (0..string.named_child_count())
747 .filter_map(|i| string.named_child(i))
748 .find(|c| c.kind() == "string_content");
749 let Some(content) = content else { return };
750
751 // Interpolated paths take the FIRST string_content only — moduleName
752 // `interp/` (+ a garbage `interp/.rb` path ref) — deterministic quirk.
753 let module_name = self.text(content).to_string();
754 let import_text = self.text(node).trim().to_string();
755 if module_name.is_empty() {
756 return;
757 }
758 self.create_node(
759 "import",
760 &module_name,
761 node,
762 Extra { signature: Some(import_text), ..Extra::default() },
763 );
764 let parent = self.top_row();
765 let imports_kind = edge_kind_index("imports").unwrap();
766 self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
767
768 // emitRubyRequireRefs (3532): the file-path ref. Bare gem/stdlib
769 // requires (no `/`) emit nothing; paths get `.rb` appended.
770 let req = self.text(content).trim();
771 if req.is_empty() {
772 return;
773 }
774 let ref_path = if mname == "require_relative" {
775 let dir = match self.file_path.rfind('/') {
776 Some(i) => &self.file_path[..i],
777 None => "",
778 };
779 let joined = if dir.is_empty() { req.to_string() } else { format!("{dir}/{req}") };
780 posix_normalize(&joined)
781 } else {
782 req.to_string()
783 };
784 if !ref_path.contains('/') {
785 return;
786 }

Callers 1

visit_nodeMethod · 0.45

Calls 7

edge_kind_indexFunction · 0.85
posix_normalizeFunction · 0.85
textMethod · 0.65
unwrapMethod · 0.60
create_nodeMethod · 0.45
top_rowMethod · 0.45
push_ref_atMethod · 0.45

Tested by

no test coverage detected