refactor(javascript): share unresolved bundler node construction (#14441)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-11-25 08:13:58 +00:00
1 parent b3ae717188
commit 17a1d033fe
4 files changed
+52 -80

No files matched your search

@@ -5,9 +5,8 @@ import { compositeKey } from "../../domain/unicodeCodePointOrder.js";
import {
addAstContainsEdge,
artifactLocalIdentity,
chunkLookupKey,
bundlerLookupKey,
javascriptAnalysisCoverage,
moduleLookupKey,
selfReferenceOmissions,
type JavaScriptArtifactGraphContext,
type JavaScriptArtifactGraphCoverage,
@@ -46,7 +45,7 @@ export const addJavaScriptBundlerNodes = (
});
for (const key of registration.chunk_keys)
context.chunkNodes.set(
chunkLookupKey(file.path, registration.runtime, key),
bundlerLookupKey(file.path, registration.runtime, key),
chunk,
);
records.push({ file, registration, chunk, coverage });
@@ -160,7 +159,7 @@ const addBundlerModuleNodes = (
],
});
context.moduleNodes.set(
moduleLookupKey(file.path, registration.runtime, moduleValue.module_key),
bundlerLookupKey(file.path, registration.runtime, moduleValue.module_key),
module,
);
addAstContainsEdge(context, {
@@ -194,10 +193,10 @@ const addBundlerRuntimeEdges = (
const { file, registration, chunk, coverage } = record;
for (const moduleKey of registration.entry_module_keys) {
const resolved = context.moduleNodes.get(
moduleLookupKey(file.path, registration.runtime, moduleKey),
bundlerLookupKey(file.path, registration.runtime, moduleKey),
);
const target =
resolved ?? unresolvedBundlerModuleNode(context, record, moduleKey);
resolved ?? unresolvedBundlerNode(context, record, "module", moduleKey);
context.accumulator.addEdge({
source_node_id: chunk.node_id,
target_node_id: target.node_id,
@@ -220,14 +219,14 @@ const addBundlerRuntimeEdges = (
});
}
for (const chunkKey of registration.async_chunk_keys) {
const lookup = chunkLookupKey(file.path, registration.runtime, chunkKey);
const lookup = bundlerLookupKey(file.path, registration.runtime, chunkKey);
const resolved = context.chunkNodes.get(lookup);
if (resolved?.node_id === chunk.node_id) {
selfAsyncChunks++;
continue;
}
const target =
resolved ?? unresolvedBundlerChunkNode(context, record, chunkKey);
resolved ?? unresolvedBundlerNode(context, record, "chunk", chunkKey);
context.accumulator.addEdge({
source_node_id: chunk.node_id,
target_node_id: target.node_id,
@@ -252,70 +251,39 @@ const addBundlerRuntimeEdges = (
return selfAsyncChunks;
};
const unresolvedBundlerModuleNode = (
const unresolvedBundlerNode = (
context: JavaScriptArtifactGraphContext,
record: BundlerGraphRecord,
moduleKey: string,
): ApplicationNode =>
context.accumulator.addNode({
kind: "javascript-module",
kind: "module" | "chunk",
key: string,
): ApplicationNode => {
const reference = kind === "module" ? "entry-module" : "async-chunk";
return context.accumulator.addNode({
kind: `javascript-${kind}`,
identity: artifactLocalIdentity(
record.file.sha256,
`${record.registration.runtime}:unresolved-entry-module`,
moduleKey,
`${record.registration.runtime}:unresolved-${reference}`,
key,
),
observations: [
{
label: moduleKey,
label: key,
properties: {
semantic_role: "bundler-module-reference",
semantic_role: `bundler-${kind}-reference`,
bundler: record.registration.bundler,
runtime: record.registration.runtime,
module_key: moduleKey,
[`${kind}_key`]: key,
resolution_status: "not-found",
},
evidence: staticInferenceEvidence({
sha256: record.file.sha256,
path: record.file.path,
range: record.registration.location,
operation: "retain-unresolved-bundler-entry-module",
coverage: record.coverage,
confidence: "low",
}),
},
],
});
const unresolvedBundlerChunkNode = (
context: JavaScriptArtifactGraphContext,
record: BundlerGraphRecord,
chunkKey: string,
): ApplicationNode =>
context.accumulator.addNode({
kind: "javascript-chunk",
identity: artifactLocalIdentity(
record.file.sha256,
`${record.registration.runtime}:unresolved-async-chunk`,
chunkKey,
),
observations: [
{
label: chunkKey,
properties: {
semantic_role: "bundler-chunk-reference",
bundler: record.registration.bundler,
runtime: record.registration.runtime,
chunk_key: chunkKey,
resolution_status: "not-found",
},
evidence: staticInferenceEvidence({
sha256: record.file.sha256,
path: record.file.path,
range: record.registration.location,
operation: "retain-unresolved-bundler-async-chunk",
operation: `retain-unresolved-bundler-${reference}`,
coverage: record.coverage,
confidence: "low",
}),
},
],
});
};
@@ -522,19 +522,12 @@ export const selfReferenceOmissions = (
`${String(count)} ${count === 1 ? noun[0] : noun[1]} resolved back to ${owner} itself and ${count === 1 ? "was" : "were"} omitted; application graph edges cannot be self-referential.`,
];
/** Deterministic key for one recovered module inside a bundle asset. */
export const moduleLookupKey = (
/** Key for a module or chunk inside one asset and bundler runtime. */
export const bundlerLookupKey = (
path: string,
runtime: string,
moduleKey: string,
): string => compositeKey([path, runtime, moduleKey]);
/** Deterministic key for one recovered bundler chunk inside a bundle asset. */
export const chunkLookupKey = (
path: string,
runtime: string,
chunkKey: string,
): string => compositeKey([path, runtime, chunkKey]);
key: string,
): string => compositeKey([path, runtime, key]);
/** Locate the recovered module that owns a static finding. */
export const sourceNodeFor = (
@@ -547,4 +540,4 @@ export const sourceNodeFor = (
? context.sourceModuleNodes.get(path)
: runtime === undefined
? undefined
: context.moduleNodes.get(moduleLookupKey(path, runtime, moduleKey));
: context.moduleNodes.get(bundlerLookupKey(path, runtime, moduleKey));
@@ -13,11 +13,10 @@ import {
applicationContainerIdentity,
addApplicationRootCandidate,
artifactLocalIdentity,
chunkLookupKey,
bundlerLookupKey,
createElectronRoleNode,
javascriptAnalysisCoverage,
linkElectronRoleToAsset,
moduleLookupKey,
selfReferenceOmissions,
sourceNodeFor,
type JavaScriptArtifactGraphContext,
@@ -774,7 +773,7 @@ const resolveReference = (
reference.loader === "commonjs-loader"
? undefined
: context.moduleNodes.get(
moduleLookupKey(sourcePath, reference.module_runtime, specifier),
bundlerLookupKey(sourcePath, reference.module_runtime, specifier),
);
if (module !== undefined) {
const file = context.filesByPath.get(sourcePath);
@@ -979,7 +978,7 @@ const resolveBundlerChunkReference = (
if (registrations === undefined) return null;
const candidates = registrations.flatMap(({ runtime }) => {
const node = context.chunkNodes.get(
chunkLookupKey(sourcePath, runtime, key),
bundlerLookupKey(sourcePath, runtime, key),
);
return node === undefined ? [] : [node];
});