From b0569bc8e788bb8c4d7f07334b2b5e70a8202e8d Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 6 Jan 2024 12:51:44 +0800 Subject: [PATCH] feat(semantic): add current_scope_flags function in SemanticBuilder (#1906) --- crates/oxc_semantic/src/binder.rs | 7 +++---- crates/oxc_semantic/src/builder.rs | 6 +++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index a306c4467..8986d547a 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -41,7 +41,7 @@ impl<'a> Binder for VariableDeclarator<'a> { // Logic for scope hoisting `var` let mut var_scope_ids = vec![]; - if !builder.scope.get_flags(current_scope_id).is_var() { + if !builder.current_scope_flags().is_var() { for scope_id in builder.scope.ancestors(current_scope_id).skip(1) { if builder.scope.get_flags(scope_id).is_var() { var_scope_ids.push(scope_id); @@ -106,8 +106,7 @@ impl<'a> Binder for Function<'a> { fn bind(&self, builder: &mut SemanticBuilder) { let current_scope_id = builder.current_scope_id; if let Some(ident) = &self.id { - let flags = builder.scope.get_flags(current_scope_id); - if !flags.is_strict_mode() + if !builder.current_scope_flags().is_strict_mode() && matches!( builder.nodes.parent_kind(builder.current_node_id), Some(AstKind::IfStatement(_)) @@ -158,7 +157,7 @@ impl<'a> Binder for Function<'a> { } // bind scope flags: Constructor | GetAccessor | SetAccessor - debug_assert!(builder.scope.get_flags(current_scope_id).contains(ScopeFlags::Function)); + debug_assert!(builder.current_scope_flags().contains(ScopeFlags::Function)); if let Some(kind) = builder.nodes.parent_kind(builder.current_node_id) { match kind { AstKind::MethodDefinition(def) => { diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 1d74437f4..0e3965aaf 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -219,8 +219,12 @@ impl<'a> SemanticBuilder<'a> { } } + pub fn current_scope_flags(&self) -> ScopeFlags { + self.scope.get_flags(self.current_scope_id) + } + pub fn strict_mode(&self) -> bool { - self.scope.get_flags(self.current_scope_id).is_strict_mode() + self.current_scope_flags().is_strict_mode() || self.current_node_flags.contains(NodeFlags::Class) }