mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-10 05:39:12 +00:00
Improve names and documentation on scope API (#5095)
## Summary Just minor improvements to improve consistency of method names and availability.
This commit is contained in:
parent
86ff1febea
commit
65dbfd2556
3 changed files with 24 additions and 7 deletions
|
@ -90,24 +90,41 @@ impl<'a> Scope<'a> {
|
|||
self.bindings.contains_key(name)
|
||||
}
|
||||
|
||||
/// Returns the ids of all bindings defined in this scope.
|
||||
/// Returns the IDs of all bindings defined in this scope.
|
||||
pub fn binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
|
||||
self.bindings.values().copied()
|
||||
}
|
||||
|
||||
/// Returns a tuple of the name and id of all bindings defined in this scope.
|
||||
/// Returns a tuple of the name and ID of all bindings defined in this scope.
|
||||
pub fn bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
|
||||
self.bindings.iter().map(|(&name, &id)| (name, id))
|
||||
}
|
||||
|
||||
/// Returns an iterator over all [bindings](BindingId) bound to the given name, including
|
||||
/// Like [`Scope::get`], but returns all bindings with the given name, including
|
||||
/// those that were shadowed by later bindings.
|
||||
pub fn bindings_for_name(&self, name: &str) -> impl Iterator<Item = BindingId> + '_ {
|
||||
pub fn get_all(&self, name: &str) -> impl Iterator<Item = BindingId> + '_ {
|
||||
std::iter::successors(self.bindings.get(name).copied(), |id| {
|
||||
self.shadowed_bindings.get(id).copied()
|
||||
})
|
||||
}
|
||||
|
||||
/// Like [`Scope::binding_ids`], but returns all bindings that were added to the scope,
|
||||
/// including those that were shadowed by later bindings.
|
||||
pub fn all_binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
|
||||
self.bindings.values().copied().flat_map(|id| {
|
||||
std::iter::successors(Some(id), |id| self.shadowed_bindings.get(id).copied())
|
||||
})
|
||||
}
|
||||
|
||||
/// Like [`Scope::bindings`], but returns all bindings added to the scope, including those that
|
||||
/// were shadowed by later bindings.
|
||||
pub fn all_bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
|
||||
self.bindings.iter().flat_map(|(&name, &id)| {
|
||||
std::iter::successors(Some(id), |id| self.shadowed_bindings.get(id).copied())
|
||||
.map(move |id| (name, id))
|
||||
})
|
||||
}
|
||||
|
||||
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
|
||||
pub fn add_star_import(&mut self, import: StarImportation<'a>) {
|
||||
self.star_imports.push(import);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue