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 262b724
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
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

0 comments on commit 262b724

Please sign in to comment.