Skip to content

Commit

Permalink
refactor(traverse): simpler code for entering/exiting unconditional s…
Browse files Browse the repository at this point in the history
…copes (#4685)

Simplify generated code for visiting nodes which always have a scope.
  • Loading branch information
overlookmotel committed Aug 6, 2024
1 parent 83546d3 commit 54f9897
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 182 deletions.
33 changes: 21 additions & 12 deletions crates/oxc_traverse/scripts/lib/walk.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading

0 comments on commit 54f9897

Please sign in to comment.