feat(ast): add AstKind::get_container_scope_id (#4450)

Part of #4445
This commit is contained in:
DonIsaac 2024-07-25 00:51:00 +00:00
parent 871b3d6135
commit fd363d1c8b

View file

@ -1,4 +1,5 @@
use oxc_span::Atom;
use oxc_syntax::scope::ScopeId;
use super::{ast::*, AstKind};
@ -93,6 +94,34 @@ impl<'a> AstKind<'a> {
}
}
/// If this node is a container, get the [`ScopeId`] it creates.
///
/// Will always be none if semantic analysis has not been run.
pub fn get_container_scope_id(self) -> Option<ScopeId> {
match self {
Self::Program(p) => p.scope_id.get(),
Self::BlockStatement(b) => b.scope_id.get(),
Self::ForStatement(f) => f.scope_id.get(),
Self::ForInStatement(f) => f.scope_id.get(),
Self::ForOfStatement(f) => f.scope_id.get(),
Self::SwitchStatement(switch) => switch.scope_id.get(),
Self::CatchClause(catch) => catch.scope_id.get(),
Self::Function(f) => f.scope_id.get(),
Self::ArrowFunctionExpression(f) => f.scope_id.get(),
Self::Class(class) => class.scope_id.get(),
Self::StaticBlock(b) => b.scope_id.get(),
Self::TSEnumDeclaration(e) => e.scope_id.get(),
Self::TSConditionalType(e) => e.scope_id.get(),
Self::TSTypeAliasDeclaration(e) => e.scope_id.get(),
Self::TSInterfaceDeclaration(e) => e.scope_id.get(),
Self::TSMethodSignature(e) => e.scope_id.get(),
Self::TSConstructSignatureDeclaration(e) => e.scope_id.get(),
Self::TSModuleDeclaration(e) => e.scope_id.get(),
Self::TSMappedType(e) => e.scope_id.get(),
_ => None,
}
}
pub fn from_expression(e: &'a Expression<'a>) -> Self {
match e {
Expression::BooleanLiteral(e) => Self::BooleanLiteral(e),