fix(javascript): retain stable declarations and bound intrinsic writes

Keep repeated variable declarations with one unconditional initializer while
rejecting assignments, conditional initialization and multiple values.
Normalize known intrinsic calls, aliases and bound argument prefixes at the
semantic owner, retaining opaque targets without tainting unused bindings or
fresh local objects. Cover both regressions through source and CLI/MCP tests.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
N0zoM1z0
2026-10-11 16:25:15 +00:00
co-authored by factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Kaoru0822
parent 3d1d5b56b2
commit 2a9fdde5db
10 changed files with 239 additions and 37 deletions
@@ -42,6 +42,14 @@ export const electronMemberPath = (
return { root, members: members.toReversed() };
};
/** A known intrinsic callable and its examined positional arguments. */
export interface JavaScriptIntrinsicInvocation {
readonly path: readonly string[];
readonly arguments:
| readonly t.CallExpression["arguments"][number][]
| undefined;
}
/** Index member and binding writes through caller-proven lexical identities. */
export function* collectJavaScriptIdentityWritesSteps(
program: t.Program,
@@ -49,7 +57,9 @@ export function* collectJavaScriptIdentityWritesSteps(
root: t.Node,
kind: "binding" | "member" | "opaque-call",
) => readonly string[] | undefined,
intrinsic: (node: t.Node) => readonly string[] | undefined,
intrinsicInvocation: (
node: t.CallExpression | t.OptionalCallExpression,
) => JavaScriptIntrinsicInvocation | undefined,
freshObject: (node: t.Node) => boolean,
retain: (node: t.Node, path: readonly (string | null)[]) => void,
): Generator<void> {
@@ -69,39 +79,16 @@ export function* collectJavaScriptIdentityWritesSteps(
yield* traverseJavaScriptAstSteps(program, {
enter: (node) => {
if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) {
let callee = intrinsic(node.callee);
let arguments_ = node.arguments;
let applied: t.Node | undefined;
let usesApply = false;
if (callee?.join(".") === "intrinsic.Reflect.apply") {
usesApply = true;
const callable = arguments_[0];
callee = t.isNode(callable) ? intrinsic(callable) : undefined;
applied = arguments_[2];
}
if (callee?.at(-1) === "call") {
callee = callee.slice(0, -1);
arguments_ = arguments_.slice(1);
} else if (callee?.at(-1) === "apply") {
usesApply = true;
callee = callee.slice(0, -1);
applied = arguments_[1];
}
if (usesApply)
arguments_ =
t.isArrayExpression(applied) &&
applied.elements.every((element) => t.isExpression(element))
? applied.elements.filter((element): element is t.Expression =>
t.isExpression(element),
)
: [];
const invocation = intrinsicInvocation(node);
const callee = invocation?.path;
const arguments_ = invocation?.arguments;
const method = callee?.at(-1);
const owner = callee?.length === 3 ? callee[1] : undefined;
if (
(owner === "Object" && objectMutators.has(method ?? "")) ||
(owner === "Reflect" && reflectMutators.has(method ?? ""))
) {
const target = arguments_[0];
const target = arguments_?.[0];
// A known mutator with an opaque argument list can overwrite any
// captured intrinsic. Do not turn an unexamined target into proof.
if (target === undefined || t.isSpreadElement(target)) {
@@ -113,7 +100,7 @@ export function* collectJavaScriptIdentityWritesSteps(
const exported = origin(root, "member");
if (exported !== undefined) {
const key = keyedMutators.has(method ?? "")
? [stringValue(arguments_[1]) ?? null]
? [stringValue(arguments_?.[1]) ?? null]
: [];
retain(node, [...exported, ...members, ...key]);
} else if (!freshObject(target)) {