Skip to content

Commit aaf57ee

Browse files
authored
Reset keyToScope and lastScope in ScopeInfoBuilder.build() so a reused builder does not leak state (#7)
* Reset keyToScope and lastScope in ScopeInfoBuilder.build() * Add test for builder state reset across build() calls
1 parent fd7221e commit aaf57ee

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/builder/builder.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,20 @@ describe("ScopeInfoBuilder", () => {
368368
assertStrictEquals(info.ranges[0].originalScope, info.scopes[0]);
369369
});
370370
});
371+
372+
describe("build", () => {
373+
it("resets accumulated state so a reused builder does not leak across builds", () => {
374+
builder.startScope(0, 0, { key: 0 }).endScope(10, 0);
375+
builder.startRange(0, 0, { scopeKey: 0 }).endRange(0, 10);
376+
builder.build();
377+
378+
// Key 0 is not registered in this second build, so the range must not
379+
// resolve to a scope carried over from the previous build.
380+
const info = builder.startRange(0, 0, { scopeKey: 0 }).endRange(0, 10)
381+
.build();
382+
383+
assertStrictEquals(info.ranges[0].originalScope, undefined);
384+
assertStrictEquals(builder.lastScope(), null);
385+
});
386+
});
371387
});

src/builder/builder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ export class ScopeInfoBuilder {
228228
this.#scopes = [];
229229
this.#ranges = [];
230230
this.#knownScopes.clear();
231+
this.#keyToScope.clear();
232+
this.#lastScope = null;
231233

232234
return info;
233235
}

0 commit comments

Comments
 (0)