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

Method extract_import

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

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

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