mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 00:20:38 +00:00
Add a utility method to detect top-level state (#4259)
This commit is contained in:
parent
983bb31577
commit
539af34f58
2 changed files with 10 additions and 1 deletions
|
@ -398,6 +398,15 @@ impl<'a> Context<'a> {
|
|||
self.stmts.ancestor_ids(node_id).map(|id| self.stmts[id])
|
||||
}
|
||||
|
||||
/// Return `true` if the context is at the top level of the module (i.e., in the module scope,
|
||||
/// and not nested within any statements).
|
||||
pub fn at_top_level(&self) -> bool {
|
||||
self.scope_id.is_global()
|
||||
&& self
|
||||
.stmt_id
|
||||
.map_or(true, |stmt_id| self.stmts.parent_id(stmt_id).is_none())
|
||||
}
|
||||
|
||||
/// Returns `true` if the context is in an exception handler.
|
||||
pub const fn in_exception_handler(&self) -> bool {
|
||||
self.in_exception_handler
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue