Add a utility method to detect top-level state (#4259)

This commit is contained in:
Charlie Marsh 2023-05-06 16:24:27 -04:00 committed by GitHub
parent 983bb31577
commit 539af34f58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View file

@ -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