From 54f98973d1493e2bf555c3d7686f8107c949a1b5 Mon Sep 17 00:00:00 2001
From: overlookmotel <557937+overlookmotel@users.noreply.github.com>
Date: Tue, 6 Aug 2024 13:03:02 +0000
Subject: [PATCH] refactor(traverse): simpler code for entering/exiting
unconditional scopes (#4685)
Simplify generated code for visiting nodes which always have a scope.
---
crates/oxc_traverse/scripts/lib/walk.mjs | 33 ++-
crates/oxc_traverse/src/walk.rs | 288 ++++++++++-------------
2 files changed, 139 insertions(+), 182 deletions(-)
diff --git a/crates/oxc_traverse/scripts/lib/walk.mjs b/crates/oxc_traverse/scripts/lib/walk.mjs
index 6bb9af1a4bd80..657bfb76b496f 100644
--- a/crates/oxc_traverse/scripts/lib/walk.mjs
+++ b/crates/oxc_traverse/scripts/lib/walk.mjs
@@ -78,19 +78,28 @@ function generateWalkForStruct(type, types) {
// but we don't take that into account.
// Visitor should not do that though, so maybe it's OK.
// In final version, we should not make `scope_id` fields `Cell`s to prevent this.
- enterScopeCode = `
- let mut previous_scope_id = None;
- if let Some(scope_id) = (*(${makeFieldCode(scopeIdField)})).get() {
- previous_scope_id = Some(ctx.current_scope_id());
- ctx.set_current_scope_id(scope_id);
- }
- `;
+ if (scopeArgs.if) {
+ enterScopeCode = `
+ let mut previous_scope_id = None;
+ if let Some(scope_id) = (*(${makeFieldCode(scopeIdField)})).get() {
+ previous_scope_id = Some(ctx.current_scope_id());
+ ctx.set_current_scope_id(scope_id);
+ }
+ `;
- exitScopeCode = `
- if let Some(previous_scope_id) = previous_scope_id {
- ctx.set_current_scope_id(previous_scope_id);
- }
- `;
+ exitScopeCode = `
+ if let Some(previous_scope_id) = previous_scope_id {
+ ctx.set_current_scope_id(previous_scope_id);
+ }
+ `;
+ } else {
+ enterScopeCode = `
+ let previous_scope_id = ctx.current_scope_id();
+ ctx.set_current_scope_id((*(${makeFieldCode(scopeIdField)})).get().unwrap());
+ `;
+
+ exitScopeCode = `ctx.set_current_scope_id(previous_scope_id);`;
+ }
}
const fieldsCodes = visitedFields.map((field, index) => {
diff --git a/crates/oxc_traverse/src/walk.rs b/crates/oxc_traverse/src/walk.rs
index e15eaca9f7673..482ca3710dbec 100644
--- a/crates/oxc_traverse/src/walk.rs
+++ b/crates/oxc_traverse/src/walk.rs
@@ -29,14 +29,12 @@ pub(crate) unsafe fn walk_program<'a, Tr: Traverse<'a>>(
ctx: &mut TraverseCtx<'a>,
) {
traverser.enter_program(&mut *node, ctx);
- let mut previous_scope_id = None;
- if let Some(scope_id) = (*((node as *mut u8).add(ancestor::OFFSET_PROGRAM_SCOPE_ID)
- as *mut Cell