Skip to content

Commit

Permalink
feat(ast): add AstKind::get_container_scope_id (#4450)
Browse files Browse the repository at this point in the history
Part of #4445
  • Loading branch information
DonIsaac committed Jul 25, 2024
1 parent 871b3d6 commit fd363d1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use oxc_span::Atom;
use oxc_syntax::scope::ScopeId;

use super::{ast::*, AstKind};

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit fd363d1

Please sign in to comment.