From 2fc321596fbb12add353ac74b06e4aa0b7a22043 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 17 Oct 2025 18:38:19 -0700 Subject: [PATCH] refactor(scope): simplify Scope class, remove pending state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove pendingIdentifiers and isInParams from constructor - Fix has() method null coalescing bug - Simplify add(), push(), pop() methods - Remove withPendingIdentifiers, withIsInParams, clearPending methods - Simplify hash() to only hash vars and parent (not pending state) - Make pop() return this instead of creating new Scope when no parent This creates a pure, hashable Scope class that only tracks variable scope chain. Temporary state (pending identifiers) will be moved to ScopeContext wrapper in next task. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/parser/scopeTracker.ts | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/src/parser/scopeTracker.ts b/src/parser/scopeTracker.ts index 3ba0044..fa42dd2 100644 --- a/src/parser/scopeTracker.ts +++ b/src/parser/scopeTracker.ts @@ -4,39 +4,25 @@ import * as terms from './shrimp.terms' export class Scope { constructor( public parent: Scope | null, - public vars: Set, - public pendingIdentifiers: string[] = [], - public isInParams: boolean = false + public vars: Set ) {} has(name: string): boolean { - return this.vars.has(name) ?? this.parent?.has(name) + return this.vars.has(name) || (this.parent?.has(name) ?? false) } add(...names: string[]): Scope { const newVars = new Set(this.vars) - names.forEach((name) => newVars.add(name)) - return new Scope(this.parent, newVars, [], this.isInParams) + names.forEach(name => newVars.add(name)) + return new Scope(this.parent, newVars) } push(): Scope { - return new Scope(this, new Set(), [], false) + return new Scope(this, new Set()) } pop(): Scope { - return this.parent ?? new Scope(null, new Set(), [], false) - } - - withPendingIdentifiers(ids: string[]): Scope { - return new Scope(this.parent, this.vars, ids, this.isInParams) - } - - withIsInParams(value: boolean): Scope { - return new Scope(this.parent, this.vars, this.pendingIdentifiers, value) - } - - clearPending(): Scope { - return new Scope(this.parent, this.vars, [], this.isInParams) + return this.parent ?? this } hash(): number { @@ -51,10 +37,6 @@ export class Scope { h = (h << 5) - h + this.parent.hash() h |= 0 } - // Include pendingIdentifiers and isInParams in hash - h = (h << 5) - h + this.pendingIdentifiers.length - h = (h << 5) - h + (this.isInParams ? 1 : 0) - h |= 0 return h } }