Skip to content

Commit fb4b8a4

Browse files
0x-saurabhclaude
authored andcommitted
fix(javascript): classify member assignment targets as property writes
Member expressions that are the target of a destructuring assignment, a for...of or for...in head, or a delete expression were recorded as property reads because the object collector only recognized a direct assignment or update parent. The semantic graph then projected a false reads-property relation and no write occurrence for these forms. Classify a member from its owning write site instead: the collector records the written member leaves when it enters a write site, using the existing semanticWriteTargets walker. The set of write-site constructs now lives in one exported helper, semanticWriteSiteTarget, shared with the member-mutation analysis that already treated these forms as writes. Fixes #2567 Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 2895a18 commit fb4b8a4

5 files changed

Lines changed: 96 additions & 11 deletions

File tree

‎src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,3 +952,29 @@ it("projects and adopts more than the default per-file capacity when explicitly
952952
"exceed the offered tree capacity",
953953
);
954954
});
955+
956+
it("projects a destructuring assignment member target as a property write", () => {
957+
const graph = graphFor(`
958+
const source = { count: 1 };
959+
[source.count] = [2];
960+
`);
961+
const occurrences = graph.nodes.filter(
962+
({ properties }) => properties.operation_kind !== undefined,
963+
);
964+
expect(occurrences.map(({ label }) => label)).toEqual(["write:/count"]);
965+
const slot = graph.nodes.find(
966+
({ kind, properties }) =>
967+
kind === "property-slot" && properties.property_pointer === "/count",
968+
);
969+
expect(graph.relations).toContainEqual(
970+
expect.objectContaining({
971+
source_node_id: occurrences[0]?.node_id,
972+
target_node_id: slot?.node_id,
973+
relation: "writes-property",
974+
resolution: "resolved",
975+
}),
976+
);
977+
expect(graph.relations).not.toContainEqual(
978+
expect.objectContaining({ relation: "reads-property" }),
979+
);
980+
});

‎src/domain/javascript/javascriptAstValues.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ export const semanticObjectPatternKeys = (
8686
return key === null ? [] : [key];
8787
});
8888

89+
/**
90+
* The target a node writes, or null when the node is not a write site.
91+
*
92+
* Direct assignment, update, `delete`, and `for...of`/`for...in` heads are
93+
* the constructs that assign into their target. Collecting them in one place
94+
* keeps every consumer's notion of "what is written" consistent; the target
95+
* itself may be a binding, a member, or a destructuring pattern, which
96+
* {@link semanticWriteTargets} flattens to its written leaves.
97+
*/
98+
export const semanticWriteSiteTarget = (node: t.Node): t.Node | null => {
99+
if (t.isAssignmentExpression(node)) return node.left;
100+
if (t.isUpdateExpression(node)) return node.argument;
101+
if (t.isUnaryExpression(node, { operator: "delete" })) return node.argument;
102+
if (t.isForOfStatement(node) || t.isForInStatement(node)) return node.left;
103+
return null;
104+
};
105+
89106
/** Visit binding and member leaves in direct and destructuring write targets. */
90107
export function* semanticWriteTargets(
91108
root: t.Node,

‎src/domain/javascript/javascriptSemanticAnalysis.dataflow.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,34 @@ describe("JavaScript semantic analysis: read-modify-write", () => {
210210
]);
211211
});
212212

213+
it.each([
214+
["destructuring assignment", "[source.count] = [2];"],
215+
["destructuring assignment with default", "[source.count = 2] = [];"],
216+
[
217+
"nested destructuring assignment",
218+
"({ value: { count: source.count } } = {});",
219+
],
220+
["rest destructuring assignment", "[...source.items] = [];"],
221+
["for-of head", "for (source.count of [4]) {}"],
222+
["for-in head", "for (source.count in { key: true }) {}"],
223+
["delete", "delete source.count;"],
224+
])("records a member target in %s as a write", (_name, statement) => {
225+
const ir = analyzeJavaScriptSemantics(`
226+
const source = { count: 1, items: [] };
227+
${statement}
228+
`);
229+
expect(ir.objectOperations.map(({ kind }) => kind)).toEqual(["write"]);
230+
});
231+
232+
it("keeps a member in a destructuring default value a read", () => {
233+
const ir = analyzeJavaScriptSemantics(`
234+
const source = { count: 1 };
235+
let value;
236+
[value = source.count] = [];
237+
`);
238+
expect(ir.objectOperations.map(({ kind }) => kind)).toEqual(["read"]);
239+
});
240+
213241
it("does not model a compound binding write as a competing replacement", () => {
214242
const ir = analyzeJavaScriptSemantics(`
215243
let count = 1;

‎src/domain/javascript/javascriptSemanticMemberMutations.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
3535
import { traverseJavaScriptAstSteps } from "./javascriptSemanticTraversal.js";
3636
import {
3737
semanticStaticPropertyKey,
38+
semanticWriteSiteTarget,
3839
semanticWriteTargets,
3940
unwrapJavaScriptExpression,
4041
} from "./javascriptAstValues.js";
@@ -471,12 +472,8 @@ export function* collectSemanticMemberMutationsSteps(
471472
originAt: source.mutation,
472473
iterationOnly: source.kind === "iteration",
473474
});
474-
if (t.isAssignmentExpression(node)) markTarget(node.left, node);
475-
else if (t.isUpdateExpression(node)) markTarget(node.argument, node);
476-
else if (t.isUnaryExpression(node, { operator: "delete" }))
477-
markTarget(node.argument, node);
478-
else if (t.isForOfStatement(node) || t.isForInStatement(node))
479-
markTarget(node.left, node);
475+
const writeTarget = semanticWriteSiteTarget(node);
476+
if (writeTarget !== null) markTarget(writeTarget, node);
480477
else if (
481478
t.isCallExpression(node) ||
482479
t.isOptionalCallExpression(node) ||

‎src/domain/javascript/javascriptSemanticObjects.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
} from "./javascriptSemanticProjection.js";
1111
import {
1212
semanticStaticPropertyKey,
13+
semanticWriteSiteTarget,
14+
semanticWriteTargets,
1315
unwrapJavaScriptExpression,
1416
} from "./javascriptAstValues.js";
1517
import {
@@ -28,13 +30,25 @@ export const collectJavaScriptSemanticObjects = (
2830
const output: JavaScriptSemanticObjectOperation[] = [];
2931
const callableIds = new Set(callables.map(({ callableId }) => callableId));
3032
const callableStack: string[] = [];
31-
const context: ObjectCollectionContext = { state, output };
33+
const context: ObjectCollectionContext = {
34+
state,
35+
output,
36+
writeTargets: new Set(),
37+
};
3238
traverseJavaScriptAst(program, {
3339
enter: (node, parent) => {
3440
const callableId = semanticCallableIdForNode(node);
3541
if (callableId !== null && callableIds.has(callableId))
3642
callableStack.push(callableId);
3743
const ownerCallableId = callableStack.at(-1) ?? null;
44+
// A write site is entered before its target members, so recording the
45+
// written leaves here lets a member nested in a destructuring pattern
46+
// (`[o.a] = v`), a loop head (`for (o.a of v)`), or `delete o.a` be
47+
// classified from its owner instead of from its immediate parent.
48+
const writeTarget = semanticWriteSiteTarget(node);
49+
if (writeTarget !== null)
50+
for (const leaf of semanticWriteTargets(writeTarget))
51+
if (!t.isIdentifier(leaf)) context.writeTargets.add(leaf);
3852
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node))
3953
collectMember(node, parent, ownerCallableId, context);
4054
else if (
@@ -73,6 +87,8 @@ export const collectJavaScriptSemanticObjects = (
7387
interface ObjectCollectionContext {
7488
readonly state: JavaScriptSemanticAnalysisState;
7589
readonly output: JavaScriptSemanticObjectOperation[];
90+
/** Member expressions an entered write site assigns into, not yet visited. */
91+
readonly writeTargets: Set<t.Node>;
7692
}
7793

7894
const collectMember = (
@@ -90,10 +106,11 @@ const collectMember = (
90106
)
91107
return;
92108
const identity = expressionObjectIdentity(node, state);
93-
const write =
94-
(t.isAssignmentExpression(parent) && parent.left === node) ||
95-
(t.isUpdateExpression(parent) && parent.argument === node);
96-
const readsBeforeWrite = write && semanticReadsBeforeWrite(node, parent);
109+
// Each member is visited once, so consuming its entry keeps the set bounded
110+
// to the write targets not yet reached.
111+
const write = context.writeTargets.delete(node);
112+
const readsBeforeWrite =
113+
write && parent !== null && semanticReadsBeforeWrite(node, parent);
97114
if (readsBeforeWrite)
98115
addObjectOperation(
99116
{

0 commit comments

Comments
 (0)