Skip to content

Commit

Permalink
refactor(semantic)!: always create a scope for for statements
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 23, 2024
1 parent 3451304 commit 832017c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 163 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ pub struct WhileStatement<'a> {

/// For Statement
#[ast(visit)]
#[scope(if(self.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration)))]
#[scope]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
Expand Down Expand Up @@ -1383,7 +1383,7 @@ pub enum ForStatementInit<'a> {

/// For-In Statement
#[ast(visit)]
#[scope(if(self.left.is_lexical_declaration()))]
#[scope]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
Expand Down Expand Up @@ -1419,7 +1419,7 @@ pub enum ForStatementLeft<'a> {
}
/// For-Of Statement
#[ast(visit)]
#[scope(if(self.left.is_lexical_declaration()))]
#[scope]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
Expand Down
28 changes: 6 additions & 22 deletions crates/oxc_ast/src/generated/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3504,16 +3504,11 @@ pub mod walk {
pub fn walk_for_in_statement<'a, V: Visit<'a>>(visitor: &mut V, it: &ForInStatement<'a>) {
let kind = AstKind::ForInStatement(visitor.alloc(it));
visitor.enter_node(kind);
let scope_events_cond = it.left.is_lexical_declaration();
if scope_events_cond {
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
}
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
visitor.visit_for_statement_left(&it.left);
visitor.visit_expression(&it.right);
visitor.visit_statement(&it.body);
if scope_events_cond {
visitor.leave_scope();
}
visitor.leave_scope();
visitor.leave_node(kind);
}

Expand Down Expand Up @@ -3575,28 +3570,19 @@ pub mod walk {
pub fn walk_for_of_statement<'a, V: Visit<'a>>(visitor: &mut V, it: &ForOfStatement<'a>) {
let kind = AstKind::ForOfStatement(visitor.alloc(it));
visitor.enter_node(kind);
let scope_events_cond = it.left.is_lexical_declaration();
if scope_events_cond {
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
}
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
visitor.visit_for_statement_left(&it.left);
visitor.visit_expression(&it.right);
visitor.visit_statement(&it.body);
if scope_events_cond {
visitor.leave_scope();
}
visitor.leave_scope();
visitor.leave_node(kind);
}

#[inline]
pub fn walk_for_statement<'a, V: Visit<'a>>(visitor: &mut V, it: &ForStatement<'a>) {
let kind = AstKind::ForStatement(visitor.alloc(it));
visitor.enter_node(kind);
let scope_events_cond =
it.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration);
if scope_events_cond {
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
}
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
if let Some(init) = &it.init {
visitor.visit_for_statement_init(init);
}
Expand All @@ -3607,9 +3593,7 @@ pub mod walk {
visitor.visit_expression(update);
}
visitor.visit_statement(&it.body);
if scope_events_cond {
visitor.leave_scope();
}
visitor.leave_scope();
visitor.leave_node(kind);
}

Expand Down
28 changes: 6 additions & 22 deletions crates/oxc_ast/src/generated/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3692,16 +3692,11 @@ pub mod walk_mut {
) {
let kind = AstType::ForInStatement;
visitor.enter_node(kind);
let scope_events_cond = it.left.is_lexical_declaration();
if scope_events_cond {
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
}
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
visitor.visit_for_statement_left(&mut it.left);
visitor.visit_expression(&mut it.right);
visitor.visit_statement(&mut it.body);
if scope_events_cond {
visitor.leave_scope();
}
visitor.leave_scope();
visitor.leave_node(kind);
}

Expand Down Expand Up @@ -3772,28 +3767,19 @@ pub mod walk_mut {
) {
let kind = AstType::ForOfStatement;
visitor.enter_node(kind);
let scope_events_cond = it.left.is_lexical_declaration();
if scope_events_cond {
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
}
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
visitor.visit_for_statement_left(&mut it.left);
visitor.visit_expression(&mut it.right);
visitor.visit_statement(&mut it.body);
if scope_events_cond {
visitor.leave_scope();
}
visitor.leave_scope();
visitor.leave_node(kind);
}

#[inline]
pub fn walk_for_statement<'a, V: VisitMut<'a>>(visitor: &mut V, it: &mut ForStatement<'a>) {
let kind = AstType::ForStatement;
visitor.enter_node(kind);
let scope_events_cond =
it.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration);
if scope_events_cond {
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
}
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
if let Some(init) = &mut it.init {
visitor.visit_for_statement_init(init);
}
Expand All @@ -3804,9 +3790,7 @@ pub mod walk_mut {
visitor.visit_expression(update);
}
visitor.visit_statement(&mut it.body);
if scope_events_cond {
visitor.leave_scope();
}
visitor.leave_scope();
visitor.leave_node(kind);
}

Expand Down
28 changes: 6 additions & 22 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
fn visit_for_statement(&mut self, stmt: &ForStatement<'a>) {
let kind = AstKind::ForStatement(self.alloc(stmt));
self.enter_node(kind);
let is_lexical_declaration =
stmt.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration);
if is_lexical_declaration {
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
}
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
if let Some(init) = &stmt.init {
self.visit_for_statement_init(init);
}
Expand Down Expand Up @@ -1007,19 +1003,14 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
});
/* cfg */

if is_lexical_declaration {
self.leave_scope();
}
self.leave_scope();
self.leave_node(kind);
}

fn visit_for_in_statement(&mut self, stmt: &ForInStatement<'a>) {
let kind = AstKind::ForInStatement(self.alloc(stmt));
self.enter_node(kind);
let is_lexical_declaration = stmt.left.is_lexical_declaration();
if is_lexical_declaration {
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
}
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);

self.visit_for_statement_left(&stmt.left);

Expand Down Expand Up @@ -1071,19 +1062,14 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
});
/* cfg */

if is_lexical_declaration {
self.leave_scope();
}
self.leave_scope();
self.leave_node(kind);
}

fn visit_for_of_statement(&mut self, stmt: &ForOfStatement<'a>) {
let kind = AstKind::ForOfStatement(self.alloc(stmt));
self.enter_node(kind);
let is_lexical_declaration = stmt.left.is_lexical_declaration();
if is_lexical_declaration {
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
}
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);

self.visit_for_statement_left(&stmt.left);

Expand Down Expand Up @@ -1134,9 +1120,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
});
/* cfg */

if is_lexical_declaration {
self.leave_scope();
}
self.leave_scope();
self.leave_node(kind);
}

Expand Down
18 changes: 3 additions & 15 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,27 +539,15 @@ impl<'a> Visit<'a> for ChildScopeCollector {
}

fn visit_for_statement(&mut self, stmt: &ForStatement<'a>) {
if let Some(scope_id) = stmt.scope_id.get() {
self.scope_ids.push(scope_id);
} else {
walk::walk_for_statement(self, stmt);
}
self.scope_ids.push(stmt.scope_id.get().unwrap());
}

fn visit_for_in_statement(&mut self, stmt: &ForInStatement<'a>) {
if let Some(scope_id) = stmt.scope_id.get() {
self.scope_ids.push(scope_id);
} else {
walk::walk_for_in_statement(self, stmt);
}
self.scope_ids.push(stmt.scope_id.get().unwrap());
}

fn visit_for_of_statement(&mut self, stmt: &ForOfStatement<'a>) {
if let Some(scope_id) = stmt.scope_id.get() {
self.scope_ids.push(scope_id);
} else {
walk::walk_for_of_statement(self, stmt);
}
self.scope_ids.push(stmt.scope_id.get().unwrap());
}

fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
Expand Down
57 changes: 24 additions & 33 deletions crates/oxc_traverse/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,14 +1617,13 @@ pub(crate) unsafe fn walk_for_statement<'a, Tr: Traverse<'a>>(
ctx: &mut TraverseCtx<'a>,
) {
traverser.enter_for_statement(&mut *node, ctx);
let mut previous_scope_id = None;
if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_FOR_STATEMENT_SCOPE_ID)
as *mut Cell<Option<ScopeId>>))
.get()
{
previous_scope_id = Some(ctx.current_scope_id());
ctx.set_current_scope_id(scope_id);
}
let previous_scope_id = ctx.current_scope_id();
ctx.set_current_scope_id(
(*((node as *mut u8).add(ancestor::OFFSET_FOR_STATEMENT_SCOPE_ID)
as *mut Cell<Option<ScopeId>>))
.get()
.unwrap(),
);
ctx.push_stack(Ancestor::ForStatementInit(ancestor::ForStatementWithoutInit(node)));
if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FOR_STATEMENT_INIT)
as *mut Option<ForStatementInit>)
Expand All @@ -1650,9 +1649,7 @@ pub(crate) unsafe fn walk_for_statement<'a, Tr: Traverse<'a>>(
ctx,
);
ctx.pop_stack();
if let Some(previous_scope_id) = previous_scope_id {
ctx.set_current_scope_id(previous_scope_id);
}
ctx.set_current_scope_id(previous_scope_id);
traverser.exit_for_statement(&mut *node, ctx);
}

Expand Down Expand Up @@ -1723,14 +1720,13 @@ pub(crate) unsafe fn walk_for_in_statement<'a, Tr: Traverse<'a>>(
ctx: &mut TraverseCtx<'a>,
) {
traverser.enter_for_in_statement(&mut *node, ctx);
let mut previous_scope_id = None;
if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_FOR_IN_STATEMENT_SCOPE_ID)
as *mut Cell<Option<ScopeId>>))
.get()
{
previous_scope_id = Some(ctx.current_scope_id());
ctx.set_current_scope_id(scope_id);
}
let previous_scope_id = ctx.current_scope_id();
ctx.set_current_scope_id(
(*((node as *mut u8).add(ancestor::OFFSET_FOR_IN_STATEMENT_SCOPE_ID)
as *mut Cell<Option<ScopeId>>))
.get()
.unwrap(),
);
ctx.push_stack(Ancestor::ForInStatementLeft(ancestor::ForInStatementWithoutLeft(node)));
walk_for_statement_left(
traverser,
Expand All @@ -1750,9 +1746,7 @@ pub(crate) unsafe fn walk_for_in_statement<'a, Tr: Traverse<'a>>(
ctx,
);
ctx.pop_stack();
if let Some(previous_scope_id) = previous_scope_id {
ctx.set_current_scope_id(previous_scope_id);
}
ctx.set_current_scope_id(previous_scope_id);
traverser.exit_for_in_statement(&mut *node, ctx);
}

Expand Down Expand Up @@ -1792,14 +1786,13 @@ pub(crate) unsafe fn walk_for_of_statement<'a, Tr: Traverse<'a>>(
ctx: &mut TraverseCtx<'a>,
) {
traverser.enter_for_of_statement(&mut *node, ctx);
let mut previous_scope_id = None;
if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_FOR_OF_STATEMENT_SCOPE_ID)
as *mut Cell<Option<ScopeId>>))
.get()
{
previous_scope_id = Some(ctx.current_scope_id());
ctx.set_current_scope_id(scope_id);
}
let previous_scope_id = ctx.current_scope_id();
ctx.set_current_scope_id(
(*((node as *mut u8).add(ancestor::OFFSET_FOR_OF_STATEMENT_SCOPE_ID)
as *mut Cell<Option<ScopeId>>))
.get()
.unwrap(),
);
ctx.push_stack(Ancestor::ForOfStatementLeft(ancestor::ForOfStatementWithoutLeft(node)));
walk_for_statement_left(
traverser,
Expand All @@ -1819,9 +1812,7 @@ pub(crate) unsafe fn walk_for_of_statement<'a, Tr: Traverse<'a>>(
ctx,
);
ctx.pop_stack();
if let Some(previous_scope_id) = previous_scope_id {
ctx.set_current_scope_id(previous_scope_id);
}
ctx.set_current_scope_id(previous_scope_id);
traverser.exit_for_of_statement(&mut *node, ctx);
}

Expand Down
Loading

0 comments on commit 832017c

Please sign in to comment.