Skip to content

Commit

Permalink
refactor: use scope way
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jul 12, 2024
1 parent 6457f24 commit 2eb37d5
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 37 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ pub struct YieldExpression<'a> {

/// Class Definitions
#[visited_node]
#[scope(flags(ScopeFlags::StrictMode | ScopeFlags::Class))]
#[scope(flags(ScopeFlags::StrictMode))]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/generated/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ pub mod walk {
if let Some(id) = &it.id {
visitor.visit_binding_identifier(id);
}
visitor.enter_scope(ScopeFlags::StrictMode | ScopeFlags::Class, &it.scope_id);
visitor.enter_scope(ScopeFlags::StrictMode, &it.scope_id);
if let Some(type_parameters) = &it.type_parameters {
visitor.visit_ts_type_parameter_declaration(type_parameters);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/generated/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3052,7 +3052,7 @@ pub mod walk_mut {
if let Some(id) = &mut it.id {
visitor.visit_binding_identifier(id);
}
visitor.enter_scope(ScopeFlags::StrictMode | ScopeFlags::Class, &it.scope_id);
visitor.enter_scope(ScopeFlags::StrictMode, &it.scope_id);
if let Some(type_parameters) = &mut it.type_parameters {
visitor.visit_ts_type_parameter_declaration(type_parameters);
}
Expand Down
5 changes: 1 addition & 4 deletions crates/oxc_semantic/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ impl<'a> Binder for Class<'a> {
SymbolFlags::ClassExcludes,
)
} else {
let symbol_id = builder.declare_symbol(ident.span, &ident.name, SymbolFlags::Class);
builder.symbols.set_declaration_symbol(symbol_id, builder.current_node_id);
symbol_id
builder.declare_symbol(ident.span, &ident.name, SymbolFlags::Class)
};
ident.symbol_id.set(Some(symbol_id));
}
Expand Down Expand Up @@ -156,7 +154,6 @@ impl<'a> Binder for Function<'a> {
// 5. Perform ! funcEnv.CreateImmutableBinding(name, false).
let symbol_id =
builder.declare_symbol(ident.span, &ident.name, SymbolFlags::Function);
builder.symbols.set_declaration_symbol(symbol_id, builder.current_node_id);
ident.symbol_id.set(Some(symbol_id));
}
}
Expand Down
27 changes: 14 additions & 13 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ impl<'a> SemanticBuilder<'a> {
}

fn resolve_references_for_current_scope(&mut self) {
let current_scope_flags = self.current_scope_flags();
// `iter_mut` to get mut references to 2 entries of `unresolved_references` simultaneously
let mut iter = self.unresolved_references.iter_mut();
let parent_refs = iter.nth(self.current_scope_depth - 1).unwrap();
Expand All @@ -392,18 +391,20 @@ impl<'a> SemanticBuilder<'a> {
// Try to resolve a reference.
// If unresolved, transfer it to parent scope's unresolved references.
if let Some(symbol_id) = bindings.get(&name).copied().or_else(|| {
self.symbols.get_symbol_id_from_declaration(self.current_node_id).and_then(
|symbol_id| {
let flag = self.symbols.get_flag(symbol_id);
if (flag.is_class() || flag.is_function())
&& self.symbols.get_name(symbol_id) == name
{
Some(symbol_id)
} else {
None
}
},
)
// If the current node is a class or function expression,
// try to get the id from the current node
// and check if it's the same name with the reference.
let id = match self.nodes.kind(self.current_node_id) {
AstKind::Function(func) if func.is_expression() => &func.id,
AstKind::Class(class) if class.is_expression() => &class.id,
_ => &None,
};
if let Some(id) = id {
if id.name == name {
return id.symbol_id.get();
}
}
None
}) {
for reference_id in &reference_ids {
self.symbols.references[*reference_id].set_symbol_id(symbol_id);
Expand Down
12 changes: 0 additions & 12 deletions crates/oxc_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub use oxc_syntax::{
scope::ScopeId,
symbol::{SymbolFlags, SymbolId},
};
use rustc_hash::FxHashMap;

#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -37,7 +35,6 @@ pub struct SymbolTable {
pub scope_ids: IndexVec<SymbolId, Option<ScopeId>>,
/// Pointer to the AST Node where this symbol is declared
pub declarations: IndexVec<SymbolId, AstNodeId>,
pub declaration_symbol: FxHashMap<AstNodeId, SymbolId>,
pub resolved_references: IndexVec<SymbolId, Vec<ReferenceId>>,
pub references: IndexVec<ReferenceId, Reference>,
pub redeclare_variables: IndexVec<SymbolId, Vec<Span>>,
Expand Down Expand Up @@ -66,10 +63,6 @@ impl SymbolTable {
.find_map(|(symbol, inner_span)| if inner_span == span { Some(symbol) } else { None })
}

pub fn get_symbol_id_from_declaration(&self, declaration: AstNodeId) -> Option<SymbolId> {
self.declaration_symbol.get(&declaration).copied()
}

pub fn get_symbol_id_from_name(&self, name: &str) -> Option<SymbolId> {
self.names.iter_enumerated().find_map(|(symbol, inner_name)| {
if inner_name.as_str() == name {
Expand Down Expand Up @@ -145,7 +138,6 @@ impl SymbolTable {
_ = self.spans.push(span);
_ = self.names.push(name);
_ = self.declarations.push(declaration);
_ = self.declarations.push(declaration);
_ = self.flags.push(flag);
_ = self.scope_ids.push(scope_id);
_ = self.resolved_references.push(vec![]);
Expand Down Expand Up @@ -189,10 +181,6 @@ impl SymbolTable {
.map(|reference_id| &self.references[*reference_id])
}

pub fn set_declaration_symbol(&mut self, symbol_id: SymbolId, declaration: AstNodeId) {
self.declaration_symbol.insert(declaration, symbol_id);
}

/// Determine whether evaluating the specific input `node` is a consequenceless reference. ie.
/// evaluating it won't result in potentially arbitrary code from being ran. The following are
/// allowed and determined not to cause side effects:
Expand Down
5 changes: 0 additions & 5 deletions crates/oxc_syntax/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ bitflags! {
const GetAccessor = 1 << 7;
const SetAccessor = 1 << 8;
const CatchClause = 1 << 9;
const Class = 1 << 10;
const Var = Self::Top.bits() | Self::Function.bits() | Self::ClassStaticBlock.bits() | Self::TsModuleBlock.bits();
const Modifiers = Self::Constructor.bits() | Self::GetAccessor.bits() | Self::SetAccessor.bits();
}
Expand Down Expand Up @@ -87,8 +86,4 @@ impl ScopeFlags {
pub fn is_catch_clause(&self) -> bool {
self.contains(Self::CatchClause)
}

pub fn is_class(&self) -> bool {
self.contains(Self::Class)
}
}

0 comments on commit 2eb37d5

Please sign in to comment.