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

Method extract_import

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

700 /// string_content → moduleName. Then the generic imports ref and
701 /// emitRubyRequireRefs' path ref.
702 fn extract_import(&mut self, node: Node<'t>) {
703 let ident = (0..node.named_child_count())
704 .filter_map(|i| node.named_child(i))
705 .find(|c| c.kind() == "identifier");
706 let Some(ident) = ident else { return };
707 let mname = self.text(ident);
708 if mname != "require" && mname != "require_relative" {
709 return;
710 }
711 let arg_list = (0..node.named_child_count())
712 .filter_map(|i| node.named_child(i))
713 .find(|c| c.kind() == "argument_list");
714 let Some(arg_list) = arg_list else { return };
715 let string = (0..arg_list.named_child_count())
716 .filter_map(|i| arg_list.named_child(i))
717 .find(|c| c.kind() == "string");
718 let Some(string) = string else { return };
719 let content = (0..string.named_child_count())
720 .filter_map(|i| string.named_child(i))
721 .find(|c| c.kind() == "string_content");
722 let Some(content) = content else { return };
723
724 // Interpolated paths take the FIRST string_content only — moduleName
725 // `interp/` (+ a garbage `interp/.rb` path ref) — deterministic quirk.
726 let module_name = self.text(content).to_string();
727 let import_text = self.text(node).trim().to_string();
728 if module_name.is_empty() {
729 return;
730 }
731 self.create_node(
732 "import",
733 &module_name,
734 node,
735 Extra { signature: Some(import_text), ..Extra::default() },
736 );
737 let parent = self.top_row();
738 let imports_kind = edge_kind_index("imports").unwrap();
739 self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
740
741 // emitRubyRequireRefs (3532): the file-path ref. Bare gem/stdlib
742 // requires (no `/`) emit nothing; paths get `.rb` appended.
743 let req = self.text(content).trim();
744 if req.is_empty() {
745 return;
746 }
747 let ref_path = if mname == "require_relative" {
748 let dir = match self.file_path.rfind('/') {
749 Some(i) => &self.file_path[..i],
750 None => "",
751 };
752 let joined = if dir.is_empty() { req.to_string() } else { format!("{dir}/{req}") };
753 posix_normalize(&joined)
754 } else {
755 req.to_string()
756 };
757 if !ref_path.contains('/') {
758 return;
759 }

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