fix(javascript): classify assignment targets and retain compound-assignment reads (#537)

This commit is contained in:
morluto
2026-10-05 16:16:57 +08:00
committed by GitHub
parent 4c8dc39f43
commit 333d118c0f
4 changed files with 204 additions and 27 deletions
@@ -341,6 +341,48 @@ describe("JavaScript semantic analysis: structure 2", () => {
);
});
it("classifies destructuring and loop targets as writes, not reads", () => {
const ir = analyzeJavaScriptSemantics(`
let a, b, c;
({ a } = source);
[b] = list;
({ nested: { c } } = source);
for (a of list) {}
let d;
try { risky(); } catch ({ message }) { report(message); }
`);
const roles = (name: string): string[] =>
ir.references
.filter((reference) => reference.name === name)
.map(({ role }) => role);
expect(roles("a")).toEqual(["write", "write"]);
expect(roles("b")).toEqual(["write"]);
expect(roles("c")).toEqual(["write"]);
// A catch binding is a declaration, so its only later use is a read.
expect(roles("message")).toEqual(["read"]);
});
it("retains the read a compound assignment or update performs", () => {
const ir = analyzeJavaScriptSemantics(`
let total = 0;
total += 1;
total++;
total = 2;
const observed = total;
`);
const roles = ir.references
.filter(({ name }) => name === "total")
.map(({ role }) => role);
expect(roles).toEqual([
"read",
"write", // total += 1
"read",
"write", // total++
"write", // total = 2
"read", // const observed = total
]);
});
it("keeps function, class, and method identities separate from bindings", () => {
const ir = analyzeJavaScriptSemantics(`
class Service {
+2 -3
View File
@@ -6,6 +6,7 @@ import type {
} from "./javascriptSemanticIr.js";
import {
semanticCallableIdForNode,
semanticReadsBeforeWrite,
semanticStaticPropertyName,
} from "./javascriptSemanticProjection.js";
import {
@@ -79,9 +80,7 @@ const collectMember = (
const write =
(t.isAssignmentExpression(parent) && parent.left === node) ||
(t.isUpdateExpression(parent) && parent.argument === node);
const readsBeforeWrite =
(t.isAssignmentExpression(parent) && parent.operator !== "=" && write) ||
(t.isUpdateExpression(parent) && write);
const readsBeforeWrite = write && semanticReadsBeforeWrite(node, parent);
if (readsBeforeWrite)
addObjectOperation(
{
+137 -18
View File
@@ -98,25 +98,37 @@ export const collectSemanticReferences = (
const output: JavaScriptSemanticReference[] = [];
const seen = new Set<string>();
traverseJavaScriptAst(program, {
enter: (node, parent) => {
enter: (node, parent, ancestors) => {
if (!t.isIdentifier(node) || parent === null) return;
const role = semanticReferenceRole(node, parent);
const role = semanticIdentifierRole(node, parent, ancestors);
if (role === null) return;
const key = `${String(node.start)}:${role}:${node.name}`;
if (seen.has(key)) return;
seen.add(key);
const binding = resolveSemanticBindingState(state, node, node.name);
const blocked =
binding === undefined &&
semanticResolutionBlocked(state, node, node.name);
output.push({
name: node.name,
role,
location: range(node),
bindingId: binding?.bindingId ?? null,
resolution:
binding !== undefined ? "resolved" : blocked ? "unknown" : "unbound",
});
// A compound assignment or update reads before it writes; emit both so
// reference consumers see the read they actually execute.
const roles =
role === "write" && semanticReadsBeforeWrite(node, parent)
? (["read", "write"] as const)
: ([role] as const);
for (const resolvedRole of roles) {
const key = `${String(node.start)}:${resolvedRole}:${node.name}`;
if (seen.has(key)) continue;
seen.add(key);
const binding = resolveSemanticBindingState(state, node, node.name);
const blocked =
binding === undefined &&
semanticResolutionBlocked(state, node, node.name);
output.push({
name: node.name,
role: resolvedRole,
location: range(node),
bindingId: binding?.bindingId ?? null,
resolution:
binding !== undefined
? "resolved"
: blocked
? "unknown"
: "unbound",
});
}
},
});
return output;
@@ -395,6 +407,111 @@ const declarationCallableId = (
: semanticCallableIdForNode(declarator.init);
};
/**
* True when writing `node` also reads it first: a compound assignment
* (`+=`, `||=`, …) or an update (`++`/`--`). A plain `=` writes only.
*
* One predicate serves both member operations and identifier references so the
* two can never disagree about what counts as a read-modify-write.
*/
export const semanticReadsBeforeWrite = (
node: t.Node,
parent: t.Node,
): boolean =>
(t.isAssignmentExpression(parent) &&
parent.left === node &&
parent.operator !== "=") ||
(t.isUpdateExpression(parent) && parent.argument === node);
/**
* Role of one identifier occurrence, given the node's ancestry.
*
* A binding inside a destructuring pattern has an ObjectProperty/ArrayPattern
* as its direct parent, so the immediate parent cannot tell a declaration
* (`const {a} = o`, `catch ({message})`) from an assignment target
* (`({a} = o)`, `for (a of list)`). Walking the ancestry to the nearest
* pattern-owning construct is what makes the distinction possible.
*/
export const semanticIdentifierRole = (
node: t.Identifier,
parent: t.Node,
ancestors: readonly t.Node[],
): JavaScriptSemanticReference["role"] | null => {
const patternAncestor = enclosingPatternAncestor(node, parent, ancestors);
if (patternAncestor !== null) {
if (isPatternOwnerDeclaration(patternAncestor)) return null;
return "write";
}
return semanticReferenceRole(node, parent);
};
/**
* The nearest ancestor that owns this identifier as a *bound* pattern slot, or
* null when the identifier is not being bound. A binding inside a pattern has
* an ObjectProperty/ArrayPattern parent, so the immediate parent cannot say
* whether the identifier declares a binding or is assigned to.
*
* Only the bound side counts: `{a = compute()}` binds `a` but merely *reads*
* `compute`, so the default-value side must not be mistaken for a binding.
*/
const enclosingPatternAncestor = (
node: t.Identifier,
parent: t.Node,
ancestors: readonly t.Node[],
): t.Node | null => {
const chain = [...ancestors, parent];
for (let index = chain.length - 1; index >= 0; index -= 1) {
const ancestor = chain[index];
if (ancestor === undefined || !isPatternNode(ancestor)) continue;
if (!bindsIdentifier(ancestor, parent, node)) continue;
// Found the binding slot; now find what owns the pattern.
for (let owner = index - 1; owner >= 0; owner -= 1) {
const candidate = chain[owner];
if (candidate === undefined) continue;
if (
t.isAssignmentExpression(candidate) ||
t.isForInStatement(candidate) ||
t.isForOfStatement(candidate)
)
return candidate;
if (isPatternOwnerDeclaration(candidate)) return candidate;
}
return null;
}
return null;
};
/** True when `parent` places `node` in the bound slot of `pattern`. */
const bindsIdentifier = (
pattern: t.Node,
parent: t.Node,
node: t.Identifier,
): boolean => {
// The identifier sits directly in the pattern: `[b]`, `({b})`, `{b = 1}`.
if (parent === pattern) return patternContains(pattern, node);
if (t.isObjectPattern(pattern))
return (
t.isObjectProperty(parent) &&
(parent.value === node || (parent.shorthand && parent.key === node))
);
if (t.isArrayPattern(pattern))
return pattern.elements.some((element) => element === parent);
if (t.isAssignmentPattern(pattern)) return pattern.left === parent;
return t.isRestElement(pattern) && pattern.argument === parent;
};
const isPatternNode = (node: t.Node): boolean =>
t.isObjectPattern(node) ||
t.isArrayPattern(node) ||
t.isAssignmentPattern(node) ||
t.isRestElement(node);
const isPatternOwnerDeclaration = (node: t.Node): boolean =>
(t.isVariableDeclarator(node) && t.isPattern(node.id)) ||
(t.isFunction(node) &&
node.params.some((value): value is t.Pattern => t.isPattern(value))) ||
(t.isCatchClause(node) && node.param !== null && t.isPattern(node.param));
/** Classify one identifier occurrence consistently with semantic references. */
export const semanticReferenceRole = (
node: t.Identifier,
@@ -404,7 +521,9 @@ export const semanticReferenceRole = (
return null;
if (
(t.isAssignmentExpression(parent) && parent.left === node) ||
t.isUpdateExpression(parent)
t.isUpdateExpression(parent) ||
((t.isForInStatement(parent) || t.isForOfStatement(parent)) &&
parent.left === node)
)
return "write";
if (t.isExportSpecifier(parent) && parent.local === node) return "export";
+23 -6
View File
@@ -1,19 +1,36 @@
import * as t from "@babel/types";
/** Visit every Babel AST node in deterministic source-tree order. */
/**
* Visit every Babel AST node in deterministic source-tree order.
*
* `ancestors` runs outermost-first and ends with the direct parent. Callers
* need it whenever meaning depends on where a node sits, not just what its
* immediate parent is — a binding inside `catch ({message})` or `({a} = o)`
* has an ObjectProperty parent, so the parent alone cannot say whether the
* identifier declares a binding or is assigned to.
*/
export const traverseJavaScriptAst = (
root: t.Node,
visitor: {
readonly enter: (node: t.Node, parent: t.Node | null) => void;
readonly enter: (
node: t.Node,
parent: t.Node | null,
ancestors: readonly t.Node[],
) => void;
readonly exit?: (node: t.Node, parent: t.Node | null) => void;
},
): void => {
const visit = (node: t.Node, parent: t.Node | null): void => {
visitor.enter(node, parent);
for (const child of childNodes(node)) visit(child, node);
const visit = (
node: t.Node,
parent: t.Node | null,
ancestors: readonly t.Node[],
): void => {
visitor.enter(node, parent, ancestors);
for (const child of childNodes(node))
visit(child, node, [...ancestors, node]);
visitor.exit?.(node, parent);
};
visit(root, null);
visit(root, null, []);
};
const childNodes = (node: t.Node): t.Node[] => {