diff --git a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts index 3b8f39eb88..add5bfd95f 100644 --- a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts @@ -2160,9 +2160,51 @@ it.each([ "candidate", ], [ - "proof stops at the inner close", + "next outer slot after a native inner close", 'const pairs=[[0],"next"];const [[used],selected]=pairs;pairs[1]="later";', ["1"], + "resolved", + ], + [ + "second nested array after a native inner close", + 'const pairs=[[0],["next"]];const [[used],[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "resolved", + ], + [ + "second nested array after an empty inner pattern", + 'const pairs=[[0],["next"]];const [[],[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "resolved", + ], + [ + "second nested array after a later-installed close hook", + 'const pairs=[[0],["next"]];const [[used],[selected]]=pairs;Object.getPrototypeOf([].values()).return=()=>({});pairs[1][0]="later";', + ["1", "0"], + "resolved", + ], + [ + "second nested array after a possible inherited close hook", + 'const pairs=[[0],["next"]];Object.prototype.return=()=>({});const [[used],[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "candidate", + ], + [ + "second nested array after an effectful default", + 'const pairs=[[undefined],["next"]];const [[used=(pairs[1][0]="default")],[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "candidate", + ], + [ + "second nested array after an unproved hole", + 'const pairs=[[,0],["next"]];const [[,used],[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], + "candidate", + ], + [ + "second nested array after an assignment target", + 'const pairs=[[0],["next"]];let used,selected;[[used],[selected]]=pairs;pairs[1][0]="later";', + ["1", "0"], "candidate", ], [ diff --git a/src/domain/javascript/javascriptSemanticObjects.ts b/src/domain/javascript/javascriptSemanticObjects.ts index 03c93ff62b..48d465c408 100644 --- a/src/domain/javascript/javascriptSemanticObjects.ts +++ b/src/domain/javascript/javascriptSemanticObjects.ts @@ -19,6 +19,7 @@ import { import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; import { range } from "./javascriptStaticAnalysisHelpers.js"; import { createSemanticReadPresenceQuery } from "./javascriptSemanticReadPresence.js"; +import { semanticArrayPatternCanResumeAfterClose } from "./javascriptSemanticReferenceSources.js"; /** Recover bounded static property reads, writes, spreads, and destructuring. */ export const collectJavaScriptSemanticObjects = ( @@ -157,12 +158,16 @@ const collectDestructuring = ( let ownSlot = sourcePresence?.read; // An unsupported property still occupies its place in the pattern's // evaluation order, even when it cannot produce a graph operation. - const pending: ({ - readonly pattern: t.Node | undefined; - readonly path: readonly string[]; - readonly plainObjectPath?: boolean; - readonly lookup?: "own" | "unproven"; - } | null)[] = [{ pattern: root, path: [], plainObjectPath: !rootArray }]; + const pending: ( + | { + readonly pattern: t.Node | undefined; + readonly path: readonly string[]; + readonly plainObjectPath?: boolean; + readonly lookup?: "own" | "unproven"; + } + | { readonly resume: NonNullable } + | null + )[] = [{ pattern: root, path: [], plainObjectPath: !rootArray }]; while (pending.length > 0) { const next = pending.pop(); if (next === undefined) break; @@ -170,6 +175,10 @@ const collectDestructuring = ( ownSlot = undefined; continue; } + if ("resume" in next) { + if (ownSlot !== undefined) ownSlot = next.resume; + continue; + } const { path } = next; const propertyPath = source.path === null ? null : [...source.path, ...path]; @@ -220,9 +229,10 @@ const collectDestructuring = ( } if (t.isArrayPattern(pattern)) { if (pattern !== root) { + const parentRead = ownSlot; ownSlot = - ownSlot !== undefined && - ownSlot === sourcePresence?.read && + parentRead !== undefined && + parentRead === sourcePresence?.read && (!rootArray || path.length === 1) && t.isArrayExpression(selected) && propertyPath !== null @@ -233,9 +243,20 @@ const collectDestructuring = ( pattern, ) : undefined; - // IteratorClose can run after these elements and before the next - // sibling. Reaching this boundary ends the current presence proof. - if (ownSlot !== undefined) pending.push(null); + // Resume only after all inner bindings retained their proof and the + // native close cannot run a hook. Assignment targets can still throw. + if (ownSlot !== undefined) + pending.push( + parentRead !== undefined && + rootArray && + semanticArrayPatternCanResumeAfterClose( + pattern, + state.parentsByNode.get(root), + state, + ) + ? { resume: parentRead } + : null, + ); } for (let index = pattern.elements.length - 1; index >= 0; index -= 1) { const element = pattern.elements[index]; diff --git a/src/domain/javascript/javascriptSemanticReferenceSources.ts b/src/domain/javascript/javascriptSemanticReferenceSources.ts index 923217fb05..a87867b2a8 100644 --- a/src/domain/javascript/javascriptSemanticReferenceSources.ts +++ b/src/domain/javascript/javascriptSemanticReferenceSources.ts @@ -82,6 +82,36 @@ const arrayIteratorReturnPaths = [ ]), ] as const; +/** Whether a native array iterator can observe a possible close hook here. */ +const semanticArrayIteratorMayHaveReturnAt = ( + pattern: t.ArrayPattern, + state: JavaScriptSemanticAnalysisState, +): boolean => + arrayIteratorReturnPaths.some((path) => + ambientPathMayObserveAt(state, pattern, path, "contents"), + ); + +const isLexicalPatternDeclaration = ( + owner: t.Node | undefined, + state: JavaScriptSemanticAnalysisState, +): owner is t.VariableDeclarator => { + if (!t.isVariableDeclarator(owner)) return false; + const declaration = state.parentsByNode.get(owner); + return ( + t.isVariableDeclaration(declaration) && + (declaration.kind === "const" || declaration.kind === "let") + ); +}; + +/** A proved lexical inner read can resume its outer iterator after a hook-free close. */ +export const semanticArrayPatternCanResumeAfterClose = ( + pattern: t.ArrayPattern, + owner: t.Node | undefined, + state: JavaScriptSemanticAnalysisState, +): boolean => + isLexicalPatternDeclaration(owner, state) && + !semanticArrayIteratorMayHaveReturnAt(pattern, state); + /** References a possible native close hook can still obtain by advancing its iterator. */ export const semanticArrayPatternCloseSelection = ( pattern: t.ArrayPattern, @@ -89,22 +119,11 @@ export const semanticArrayPatternCloseSelection = ( array: Extract, state: JavaScriptSemanticAnalysisState, ): JavaScriptSemanticPropertySelection | undefined => { - if ( - !arrayIteratorReturnPaths.some((path) => - ambientPathMayObserveAt(state, pattern, path, "contents"), - ) - ) - return undefined; - const declaration = - owner === undefined ? undefined : state.parentsByNode.get(owner); + if (!semanticArrayIteratorMayHaveReturnAt(pattern, state)) return undefined; const last = pattern.elements.at(-1); const hasRest = t.isRestElement(last); let consumed = 0; - if ( - t.isVariableDeclarator(owner) && - t.isVariableDeclaration(declaration) && - (declaration.kind === "const" || declaration.kind === "let") - ) { + if (isLexicalPatternDeclaration(owner, state)) { for (const [index, element] of pattern.elements.entries()) { // A lexical rest drains the iterator only after earlier pattern work // reaches it. A preceding default or nested binding can throw first. diff --git a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts index cad6c0d103..da4e603b3f 100644 --- a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts +++ b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts @@ -50,6 +50,7 @@ const expectedDependencies = [ ["arrayObject", ["0", "mode"]], ["nestedElement", ["items", "0"]], ["innerElement", ["0", "0"]], + ["siblingInnerElement", ["1", "0"]], ["spreadElement", ["1"]], ["aliasedPrototypeElement", ["0", "0"], "candidate"], ["selected", ["0"], "candidate"], @@ -72,6 +73,7 @@ const tracedBindings = [ "arrayObject", "nestedElement", "innerElement", + "siblingInnerElement", "spreadElement", "aliasedPrototypeElement", "selected", @@ -124,9 +126,10 @@ cliTest( const nestedSource = { items: ["nested"] }; const { items: [nestedElement] } = nestedSource; nestedSource.items[0] = "later"; - const nestedPairs = [["read-time"]]; - const [[innerElement]] = nestedPairs; + const nestedPairs = [["read-time"], ["sibling"]]; + const [[innerElement], [siblingInnerElement]] = nestedPairs; nestedPairs[0][0] = "later"; + nestedPairs[1][0] = "later"; const spreadSource = [...["prefix"], "read-time"]; const [,spreadElement] = spreadSource; spreadSource[1] = "later";