fix(javascript): bound cyclic mutation captures by their paths (#6986)

This commit is contained in:
gensokyo-dev authored and GitHub committed 2026-10-24 10:11:27 +00:00
1 parent b22b0ef22a
commit dd02d8ff69
3 files changed
+158 -67

No files matched your search

@@ -94,6 +94,7 @@ interface PropertyPathCoverageNode {
string,
{
readonly key: JavaScriptSemanticPropertyPath[number];
readonly context: string | undefined;
readonly node: PropertyPathCoverageNode;
}
>;
@@ -109,13 +110,17 @@ const coverageNode = (): PropertyPathCoverageNode => ({
export class SemanticPropertyPathCoverage {
readonly #root = coverageNode();
/** Retain a path only when no previously retained prefix covers it. */
retain(path: JavaScriptSemanticPropertyPath): boolean {
if (this.covers(path)) return false;
/** Match optional per-slot receiver identities along the covering prefix. */
retain(
path: JavaScriptSemanticPropertyPath,
contexts?: readonly (string | undefined)[],
): boolean {
if (this.covers(path, contexts)) return false;
let node = this.#root;
for (const key of path) {
for (const [offset, key] of path.entries()) {
const context = contexts?.[offset];
if (key !== null && typeof key !== "object") {
const name = String(key);
const name = JSON.stringify([String(key), context]);
let child = node.exact.get(name);
if (child === undefined) {
child = coverageNode();
@@ -123,10 +128,10 @@ export class SemanticPropertyPathCoverage {
}
node = child;
} else {
const identity = JSON.stringify(key);
const identity = JSON.stringify([key, context]);
let selected = node.selectors.get(identity);
if (selected === undefined) {
selected = { key, node: coverageNode() };
selected = { key, context, node: coverageNode() };
node.selectors.set(identity, selected);
}
node = selected.node;
@@ -139,7 +144,10 @@ export class SemanticPropertyPathCoverage {
}
/** Query retained prefixes without changing the coverage index. */
covers(path: JavaScriptSemanticPropertyPath): boolean {
covers(
path: JavaScriptSemanticPropertyPath,
contexts?: readonly (string | undefined)[],
): boolean {
const pending = [{ node: this.#root, offset: 0 }];
while (pending.length > 0) {
const current = pending.pop();
@@ -147,13 +155,16 @@ export class SemanticPropertyPathCoverage {
if (current.node.terminal) return true;
const key = path[current.offset];
if (key === undefined) continue;
const context = contexts?.[current.offset];
if (key !== null && typeof key !== "object") {
const exact = current.node.exact.get(String(key));
const exact = current.node.exact.get(
JSON.stringify([String(key), context]),
);
if (exact !== undefined)
pending.push({ node: exact, offset: current.offset + 1 });
}
for (const selected of current.node.selectors.values()) {
if (keyCovers(selected.key, key))
if (selected.context === context && keyCovers(selected.key, key))
pending.push({ node: selected.node, offset: current.offset + 1 });
}
}