[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.
This commit is contained in:
Andrew Gallant
2026-01-14 12:48:59 -05:00
committed by Andrew Gallant
parent 2f34836318
commit a72af10cf8

View File

@@ -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