From a72af10cf8fa49f79eb5233bc0bf552faa153c2b Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 14 Jan 2026 12:48:59 -0500 Subject: [PATCH] [ty] Add `ModuleName::top` The invariants established by the constructors for `ModuleName` guarantee that this is always available. It's useful for determining the "top level" module for where a symbol lives. --- crates/ty_module_resolver/src/module_name.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ty_module_resolver/src/module_name.rs b/crates/ty_module_resolver/src/module_name.rs index bd7b1966a0..d63576079b 100644 --- a/crates/ty_module_resolver/src/module_name.rs +++ b/crates/ty_module_resolver/src/module_name.rs @@ -83,6 +83,24 @@ impl ModuleName { self.0.split('.') } + /// Returns the first component in this module name. + /// + /// # Examples + /// + /// ``` + /// use ty_module_resolver::ModuleName; + /// + /// assert_eq!(ModuleName::new_static("foo.bar.baz").unwrap().first_component(), "foo"); + /// ``` + #[must_use] + pub fn first_component(&self) -> &str { + // OK because `Self::is_valid_name` guarantees that there is at least + // one component in the module name. + self.components() + .next() + .expect("at least one module component") + } + /// The name of this module's immediate parent, if it has a parent. /// /// # Examples