Skip to content

Commit a72af10

Browse files
committed
[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.
1 parent 2f34836 commit a72af10

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

crates/ty_module_resolver/src/module_name.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ impl ModuleName {
8383
self.0.split('.')
8484
}
8585

86+
/// Returns the first component in this module name.
87+
///
88+
/// # Examples
89+
///
90+
/// ```
91+
/// use ty_module_resolver::ModuleName;
92+
///
93+
/// assert_eq!(ModuleName::new_static("foo.bar.baz").unwrap().first_component(), "foo");
94+
/// ```
95+
#[must_use]
96+
pub fn first_component(&self) -> &str {
97+
// OK because `Self::is_valid_name` guarantees that there is at least
98+
// one component in the module name.
99+
self.components()
100+
.next()
101+
.expect("at least one module component")
102+
}
103+
86104
/// The name of this module's immediate parent, if it has a parent.
87105
///
88106
/// # Examples

0 commit comments

Comments
 (0)