refactor(javascript): share Electron graph edge projection (#15479)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 14:17:07 +00:00
1 parent 43e69a3984
commit 3e565f4bf6
1 file changed
+38 -50
@@ -40,21 +40,33 @@ export interface ElectronGraphEdgeInput {
export const ELECTRON_CANDIDATE_LIMITATION =
"The Electron module loader is unknown, so this relationship is only a candidate.";
type ElectronEvidenceInput = Parameters<typeof astObservationEvidence>[0] &
Pick<Parameters<typeof staticInferenceEvidence>[0], "confidence">;
const electronRelationshipEvidence = (
input: ElectronEvidenceInput,
loaderAuthority: ElectronGraphEdgeInput["loaderAuthority"],
kind: "observation" | "inference",
) => {
if (loaderAuthority === "candidate")
return staticInferenceEvidence({
...input,
confidence: "low",
limitations: [
...(input.limitations ?? []),
ELECTRON_CANDIDATE_LIMITATION,
],
});
return kind === "observation"
? astObservationEvidence(input)
: staticInferenceEvidence(input);
};
/** Preserve syntax with uncertainty when its Electron receiver is unproven. */
export const electronFindingEvidence = (
input: Parameters<typeof astObservationEvidence>[0],
loaderAuthority: "candidate" | undefined,
) =>
loaderAuthority === "candidate"
? staticInferenceEvidence({
...input,
confidence: "low",
limitations: [
...(input.limitations ?? []),
ELECTRON_CANDIDATE_LIMITATION,
],
})
: astObservationEvidence(input);
loaderAuthority: ElectronGraphEdgeInput["loaderAuthority"],
) => electronRelationshipEvidence(input, loaderAuthority, "observation");
/** Resolve the file or recovered bundle module that owns one finding. */
export const electronFindingSourceNode = (
@@ -81,10 +93,10 @@ export const electronObservationIdentity = (
scope,
});
/** Add a direct AST syntax relationship. */
export const addElectronAstEdge = (
const addElectronEdge = (
context: JavaScriptArtifactGraphContext,
input: ElectronGraphEdgeInput,
kind: "observation" | "inference",
): void => {
context.accumulator.addEdge({
source_node_id: input.source.node_id,
@@ -96,58 +108,34 @@ export const addElectronAstEdge = (
? { loader_authority: "candidate" }
: {}),
},
evidence: electronFindingEvidence(
evidence: electronRelationshipEvidence(
{
sha256: input.file.sha256,
path: input.file.path,
range: input.range,
operation: input.operation,
coverage: input.coverage,
...(kind === "inference" && input.confidence !== undefined
? { confidence: input.confidence }
: {}),
...(input.limitations === undefined
? {}
: { limitations: input.limitations }),
},
input.loaderAuthority,
kind,
),
});
};
/** Add a direct AST syntax relationship. */
export const addElectronAstEdge = (
context: JavaScriptArtifactGraphContext,
input: ElectronGraphEdgeInput,
): void => addElectronEdge(context, input, "observation");
/** Add an Electron relationship that static syntax suggests but cannot prove. */
export const addElectronInferenceEdge = (
context: JavaScriptArtifactGraphContext,
input: ElectronGraphEdgeInput,
): void => {
context.accumulator.addEdge({
source_node_id: input.source.node_id,
target_node_id: input.target.node_id,
relation: input.relation,
properties: {
...input.properties,
...(input.loaderAuthority === "candidate"
? { loader_authority: "candidate" }
: {}),
},
evidence: staticInferenceEvidence({
sha256: input.file.sha256,
path: input.file.path,
range: input.range,
operation: input.operation,
coverage: input.coverage,
...(input.loaderAuthority === "candidate"
? { confidence: "low" as const }
: input.confidence === undefined
? {}
: { confidence: input.confidence }),
...(input.loaderAuthority === "candidate"
? {
limitations: [
...(input.limitations ?? []),
ELECTRON_CANDIDATE_LIMITATION,
],
}
: input.limitations === undefined
? {}
: { limitations: input.limitations }),
}),
});
};
): void => addElectronEdge(context, input, "inference");