mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:40:37 +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
|
@ -4808,7 +4808,7 @@ impl<'a> Checker<'a> {
|
||||||
let exports: Vec<(&str, TextRange)> = {
|
let exports: Vec<(&str, TextRange)> = {
|
||||||
self.semantic_model
|
self.semantic_model
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.bindings_for_name("__all__")
|
.get_all("__all__")
|
||||||
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
||||||
.filter_map(|binding| match &binding.kind {
|
.filter_map(|binding| match &binding.kind {
|
||||||
BindingKind::Export(Export { names }) => {
|
BindingKind::Export(Export { names }) => {
|
||||||
|
@ -5069,7 +5069,7 @@ impl<'a> Checker<'a> {
|
||||||
let exports: Option<Vec<&str>> = {
|
let exports: Option<Vec<&str>> = {
|
||||||
let global_scope = self.semantic_model.global_scope();
|
let global_scope = self.semantic_model.global_scope();
|
||||||
global_scope
|
global_scope
|
||||||
.bindings_for_name("__all__")
|
.get_all("__all__")
|
||||||
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
||||||
.filter_map(|binding| match &binding.kind {
|
.filter_map(|binding| match &binding.kind {
|
||||||
BindingKind::Export(Export { names }) => Some(names.iter().copied()),
|
BindingKind::Export(Export { names }) => Some(names.iter().copied()),
|
||||||
|
|
|
@ -156,7 +156,7 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, target: &Expr,
|
||||||
// used _after_ the loop.
|
// used _after_ the loop.
|
||||||
let scope = checker.semantic_model().scope();
|
let scope = checker.semantic_model().scope();
|
||||||
if scope
|
if scope
|
||||||
.bindings_for_name(name)
|
.get_all(name)
|
||||||
.map(|binding_id| checker.semantic_model().binding(binding_id))
|
.map(|binding_id| checker.semantic_model().binding(binding_id))
|
||||||
.all(|binding| !binding.is_used())
|
.all(|binding| !binding.is_used())
|
||||||
{
|
{
|
||||||
|
|
|
@ -90,24 +90,41 @@ impl<'a> Scope<'a> {
|
||||||
self.bindings.contains_key(name)
|
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> + '_ {
|
pub fn binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
|
||||||
self.bindings.values().copied()
|
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)> + '_ {
|
pub fn bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
|
||||||
self.bindings.iter().map(|(&name, &id)| (name, id))
|
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.
|
/// 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| {
|
std::iter::successors(self.bindings.get(name).copied(), |id| {
|
||||||
self.shadowed_bindings.get(id).copied()
|
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.
|
/// 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>) {
|
pub fn add_star_import(&mut self, import: StarImportation<'a>) {
|
||||||
self.star_imports.push(import);
|
self.star_imports.push(import);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue