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

Method extract_import

codegraph-kernel/src/ruby.rs:751–812  ·  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

749 /// string_content → moduleName. Then the generic imports ref and
750 /// emitRubyRequireRefs' path ref.
751 fn extract_import(&mut self, node: Node<'t>) {
752 let ident = (0..node.named_child_count())
753 .filter_map(|i| node.named_child(i))
754 .find(|c| c.kind() == "identifier");
755 let Some(ident) = ident else { return };
756 let mname = self.text(ident);
757 if mname != "require" && mname != "require_relative" {
758 return;
759 }
760 let arg_list = (0..node.named_child_count())
761 .filter_map(|i| node.named_child(i))
762 .find(|c| c.kind() == "argument_list");
763 let Some(arg_list) = arg_list else { return };
764 let string = (0..arg_list.named_child_count())
765 .filter_map(|i| arg_list.named_child(i))
766 .find(|c| c.kind() == "string");
767 let Some(string) = string else { return };
768 let content = (0..string.named_child_count())
769 .filter_map(|i| string.named_child(i))
770 .find(|c| c.kind() == "string_content");
771 let Some(content) = content else { return };
772
773 // Interpolated paths take the FIRST string_content only — moduleName
774 // `interp/` (+ a garbage `interp/.rb` path ref) — deterministic quirk.
775 let module_name = self.text(content).to_string();
776 let import_text = self.text(node).trim().to_string();
777 if module_name.is_empty() {
778 return;
779 }
780 self.create_node(
781 "import",
782 &module_name,
783 node,
784 Extra { signature: Some(import_text), ..Extra::default() },
785 );
786 let parent = self.top_row();
787 let imports_kind = edge_kind_index("imports").unwrap();
788 self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
789
790 // emitRubyRequireRefs (3532): the file-path ref. Bare gem/stdlib
791 // requires (no `/`) emit nothing; paths get `.rb` appended.
792 let req = self.text(content).trim();
793 if req.is_empty() {
794 return;
795 }
796 let ref_path = if mname == "require_relative" {
797 let dir = match self.file_path.rfind('/') {
798 Some(i) => &self.file_path[..i],
799 None => "",
800 };
801 let joined = if dir.is_empty() { req.to_string() } else { format!("{dir}/{req}") };
802 posix_normalize(&joined)
803 } else {
804 req.to_string()
805 };
806 if !ref_path.contains('/') {
807 return;
808 }

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