Skip to content

Commit

Permalink
refactor: another way
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jul 11, 2024
1 parent 586f5d9 commit ca61d3d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 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 @@ -1300,7 +1300,7 @@ pub struct TryStatement<'a> {
}

#[visited_node]
#[scope(flags(ScopeFlags::CatchClause))]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
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 @@ -3690,7 +3690,7 @@ pub mod walk {

#[inline]
pub fn walk_catch_clause<'a, V: Visit<'a>>(visitor: &mut V, it: &CatchClause<'a>) {
visitor.enter_scope(ScopeFlags::CatchClause, &it.scope_id);
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
let kind = AstKind::CatchClause(visitor.alloc(it));
visitor.enter_node(kind);
if let Some(param) = &it.param {
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 @@ -3895,7 +3895,7 @@ pub mod walk_mut {

#[inline]
pub fn walk_catch_clause<'a, V: VisitMut<'a>>(visitor: &mut V, it: &mut CatchClause<'a>) {
visitor.enter_scope(ScopeFlags::CatchClause, &it.scope_id);
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
let kind = AstType::CatchClause;
visitor.enter_node(kind);
if let Some(param) = &mut it.param {
Expand Down
25 changes: 14 additions & 11 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,7 @@ impl<'a> SemanticBuilder<'a> {
excludes: SymbolFlags,
report_error: bool,
) -> Option<SymbolId> {
let symbol_id = self.scope.get_binding(scope_id, name).or_else(|| {
self.scope.get_parent_id(scope_id).and_then(|pid| {
let flags = self.scope.get_flags(pid);
// It is a Syntax Error if any element of the BoundNames of CatchParameter also occurs in the LexicallyDeclaredNames of Block.
if flags.is_catch_clause() {
self.scope.get_binding(pid, name)
} else {
None
}
})
})?;
let symbol_id = self.scope.get_binding(scope_id, name)?;
if report_error && self.symbols.get_flag(symbol_id).intersects(excludes) {
let symbol_span = self.symbols.get_span(symbol_id);
self.error(redeclaration(name, symbol_span, span));
Expand Down Expand Up @@ -1823,6 +1813,19 @@ impl<'a> SemanticBuilder<'a> {
AstKind::YieldExpression(_) => {
self.set_function_node_flag(NodeFlags::HasYield);
}
AstKind::BlockStatement(_) => {
if matches!(
self.nodes.parent_kind(self.current_node_id),
Some(AstKind::CatchClause(_))
) {
// Clone the `CatchClause` bindings and add them to the current scope.
// to make it easier to check redeclare errors.
if let Some(parent_scope_id) = self.scope.get_parent_id(self.current_scope_id) {
let bindings = self.scope.get_bindings(parent_scope_id).clone();
self.scope.get_bindings_mut(self.current_scope_id).extend(bindings);
}
}
}
_ => {}
}
}
Expand Down
7 changes: 1 addition & 6 deletions crates/oxc_syntax/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ bitflags! {
const Constructor = 1 << 6;
const GetAccessor = 1 << 7;
const SetAccessor = 1 << 8;
const CatchClause = 1 << 9;
const Var = Self::Top.bits() | Self::Function.bits() | Self::ClassStaticBlock.bits() | Self::TsModuleBlock.bits() | Self::CatchClause.bits();
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 @@ -82,8 +81,4 @@ impl ScopeFlags {
pub fn is_set_or_get_accessor(&self) -> bool {
self.intersects(Self::SetAccessor | Self::GetAccessor)
}

pub fn is_catch_clause(&self) -> bool {
self.contains(Self::CatchClause)
}
}

0 comments on commit ca61d3d

Please sign in to comment.