fix(javascript): resolve electron findings through aliases of interop results (#8914)

* fix(javascript): resolve electron findings through aliases of interop results

* fix(javascript): resolve interop aliases iteratively and check target mutations

* fix(javascript): retain possible writes through uncertain aliases and guard positive reads

* perf(javascript): cache alias change status across semantic interop lookups
This commit is contained in:
highmachs
2026-11-05 18:31:52 +08:00
committed by GitHub
parent 281dbc8514
commit 7b6fc6f13d
3 changed files with 223 additions and 36 deletions
+151 -35
View File
@@ -30,6 +30,7 @@ import {
unwrapJavaScriptExpression,
} from "./javascriptAstValues.js";
import { semanticKnownArrayIterationAt } from "./javascriptSemanticReferenceSources.js";
import { semanticOpaqueBindingReadMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js";
// Match the complete Rolldown runtime operations, including its live getters.
// Names and formatting are irrelevant; lexical roles, operators and keys are not.
@@ -87,6 +88,11 @@ export const createSemanticModuleInterop = (
semanticKnownArrayIterationAt(array, state);
const helpers = new Map<string, InteropHelper | null>();
const origins = new Map<string, InteropOrigin>();
const aliasTargets = new Map<
string,
{ targetBinding: JavaScriptSemanticBindingState; node: t.Node }
>();
const aliasChangedCache = new Map<string, boolean>();
const calls = new WeakMap<t.Node, InteropOrigin>();
const intrinsicBindings = new Map<string, readonly string[] | null>();
const writes: {
@@ -517,40 +523,9 @@ export const createSemanticModuleInterop = (
return result;
};
const origin = (
const twoInitializerOrigin = (
binding: JavaScriptSemanticBindingState,
): InteropOrigin | undefined => {
const single = semanticStableInitializer(binding, state);
if (single !== undefined) {
if (
!single.projection.every(
(key): key is string => typeof key === "string",
)
)
return undefined;
const converted = callOrigin(single.node);
if (
converted?.origin.importedPath.length === 0 &&
single.projection[0] === "default" &&
!converted.defaultIsModule
)
return undefined;
const result =
converted === undefined
? undefined
: {
...converted,
origin: {
...converted.origin,
importedPath: [
...converted.origin.importedPath,
...single.projection,
],
},
};
if (result !== undefined) origins.set(binding.bindingId, result);
return result;
}
const [initial, converted] = binding.initializers;
if (
binding.definitions.length !== 2 ||
@@ -600,13 +575,126 @@ export const createSemanticModuleInterop = (
if (required === undefined || required.importedPath.length !== 0)
return undefined;
if (convertedCall === undefined) return undefined;
const result = {
return {
origin: required,
dependencies: convertedCall.dependencies,
defaultIsModule: convertedCall.defaultIsModule,
};
origins.set(binding.bindingId, result);
return result;
};
const origin = (
binding: JavaScriptSemanticBindingState,
): InteropOrigin | undefined => {
const cached = origins.get(binding.bindingId);
if (cached !== undefined) return cached;
let currentBinding: JavaScriptSemanticBindingState | undefined = binding;
const visited = new Set<string>();
const steps: {
binding: JavaScriptSemanticBindingState;
segments: readonly string[];
}[] = [];
let baseOrigin: InteropOrigin | undefined;
while (currentBinding !== undefined) {
const alreadyCached = origins.get(currentBinding.bindingId);
if (alreadyCached !== undefined) {
baseOrigin = alreadyCached;
break;
}
if (visited.has(currentBinding.bindingId)) return undefined;
visited.add(currentBinding.bindingId);
const single = semanticStableInitializer(currentBinding, state);
if (single !== undefined) {
if (
!single.projection.every(
(key): key is string => typeof key === "string",
)
)
return undefined;
const converted = callOrigin(single.node);
if (converted !== undefined) {
if (
converted.origin.importedPath.length === 0 &&
single.projection[0] === "default" &&
!converted.defaultIsModule
)
return undefined;
baseOrigin = {
...converted,
origin: {
...converted.origin,
importedPath: [
...converted.origin.importedPath,
...(single.projection as readonly string[]),
],
},
};
break;
}
const { root, members } = electronMemberPath(single.node);
if (
!t.isIdentifier(root) ||
!members.every((member): member is string => member !== null)
)
return undefined;
const targetBinding = resolveSemanticBindingState(
state,
root,
root.name,
);
if (targetBinding === undefined || targetBinding === currentBinding)
return undefined;
aliasTargets.set(currentBinding.bindingId, {
targetBinding,
node: root,
});
steps.push({
binding: currentBinding,
segments: [
...(members as readonly string[]),
...(single.projection as readonly string[]),
],
});
currentBinding = targetBinding;
continue;
}
const twoInit = twoInitializerOrigin(currentBinding);
if (twoInit !== undefined) {
baseOrigin = twoInit;
break;
}
return undefined;
}
if (baseOrigin === undefined) return undefined;
origins.set(currentBinding!.bindingId, baseOrigin);
let currentOrigin = baseOrigin;
for (let i = steps.length - 1; i >= 0; i--) {
const step = steps[i]!;
const firstSegment = step.segments[0];
if (
currentOrigin.origin.importedPath.length === 0 &&
firstSegment === "default" &&
!currentOrigin.defaultIsModule
)
return undefined;
currentOrigin = {
...currentOrigin,
origin: {
...currentOrigin.origin,
importedPath: [
...currentOrigin.origin.importedPath,
...step.segments,
],
},
};
origins.set(step.binding.bindingId, currentOrigin);
}
return currentOrigin;
};
const invalidatedOrigin = (record: InteropOrigin | undefined): boolean =>
record?.dependencies.some(({ path, capturedAt }) => {
@@ -667,6 +755,34 @@ export const createSemanticModuleInterop = (
invalidatedCalls.set(node, result);
return result;
},
aliasMayHaveChanged: (binding: JavaScriptSemanticBindingState): boolean => {
if (state.opaqueEffects.byScope.size === 0) return false;
const cached = aliasChangedCache.get(binding.bindingId);
if (cached !== undefined) return cached;
origin(binding);
let current: JavaScriptSemanticBindingState | undefined = binding;
const seen = new Set<string>();
let changed = false;
while (current !== undefined) {
if (seen.has(current.bindingId)) break;
seen.add(current.bindingId);
const next = aliasTargets.get(current.bindingId);
if (next === undefined) break;
if (
semanticOpaqueBindingReadMayHaveChanged(
next.targetBinding,
next.node,
state,
)
) {
changed = true;
break;
}
current = next.targetBinding;
}
aliasChangedCache.set(binding.bindingId, changed);
return changed;
},
};
};