mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 05:20:49 -05:00
[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:
committed by
Andrew Gallant
parent
2f34836318
commit
a72af10cf8
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user