mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
92 lines
3.6 KiB
TypeScript
92 lines
3.6 KiB
TypeScript
import type { JavaScriptSemanticGraphNode } from "../../domain/javascript/javascriptSemanticGraphSchemas.js";
|
|
import type { JavaScriptSemanticResourceOperation } from "../../domain/javascript/javascriptSemanticIr.js";
|
|
import {
|
|
retainSemanticGraphNode,
|
|
addSemanticGraphRelation,
|
|
addSemanticGraphUnknown,
|
|
} from "./JavaScriptSemanticGraphConstruction.js";
|
|
import {
|
|
builtinSemanticNode,
|
|
builtinSemanticRelation,
|
|
unknownSemanticEvidence,
|
|
} from "./JavaScriptSemanticGraphEvidence.js";
|
|
import type { SemanticFlowProjectionContext } from "./JavaScriptSemanticGraphFlowProjection.js";
|
|
|
|
/** Project built-in resource acquisition and exact local release handles. */
|
|
export const projectSemanticResources = (
|
|
context: SemanticFlowProjectionContext,
|
|
): void => {
|
|
const resourceNodes = new Map(
|
|
context.ir.resourceOperations.flatMap((operation) => {
|
|
if (operation.kind !== "acquire") return [];
|
|
const node = addResourceNode(context, operation);
|
|
return node === null ? [] : [[operation.resourceId, node] as const];
|
|
}),
|
|
);
|
|
for (const operation of context.ir.resourceOperations)
|
|
if (operation.kind === "acquire") {
|
|
const callSite = context.callSiteAt(operation.location);
|
|
const resource = resourceNodes.get(operation.resourceId);
|
|
addSemanticGraphRelation(context.state, {
|
|
source: callSite,
|
|
target: resource,
|
|
relation: "acquires",
|
|
...builtinSemanticRelation(context.file, operation),
|
|
});
|
|
if (operation.resolution === "partial")
|
|
addSemanticGraphUnknown(context.state, {
|
|
node_id: callSite?.node_id ?? null,
|
|
family: "resource-lifecycle",
|
|
relation_kinds: ["acquires"],
|
|
reason: "ambiguous-target",
|
|
detail: `Static ${operation.method} resource acquisition is uncertain.`,
|
|
candidate_node_ids: resource === undefined ? [] : [resource.node_id],
|
|
evidence: unknownSemanticEvidence(context.file, operation.location),
|
|
});
|
|
} else projectRelease(context, operation, resourceNodes);
|
|
};
|
|
|
|
const addResourceNode = (
|
|
context: SemanticFlowProjectionContext,
|
|
operation: JavaScriptSemanticResourceOperation,
|
|
): JavaScriptSemanticGraphNode | null =>
|
|
retainSemanticGraphNode(context.state, context.file, {
|
|
kind: "resource",
|
|
roleKey: operation.resourceId,
|
|
location: operation.location,
|
|
label: operation.method,
|
|
functionNodeId: context.callableNodeId(operation.ownerCallableId),
|
|
...builtinSemanticNode(context.file, operation, {
|
|
method: operation.method,
|
|
}),
|
|
});
|
|
|
|
const projectRelease = (
|
|
context: SemanticFlowProjectionContext,
|
|
operation: JavaScriptSemanticResourceOperation,
|
|
resourceNodes: ReadonlyMap<string, JavaScriptSemanticGraphNode>,
|
|
): void => {
|
|
const callSite = context.callSiteAt(operation.location);
|
|
const resources = operation.linkedResourceIds.flatMap((identifier) => {
|
|
const resource = resourceNodes.get(identifier);
|
|
return resource === undefined ? [] : [resource];
|
|
});
|
|
for (const resource of resources)
|
|
addSemanticGraphRelation(context.state, {
|
|
source: callSite,
|
|
target: resource,
|
|
relation: "releases",
|
|
...builtinSemanticRelation(context.file, operation),
|
|
});
|
|
if (operation.resolution === "complete") return;
|
|
addSemanticGraphUnknown(context.state, {
|
|
node_id: callSite?.node_id ?? null,
|
|
family: "resource-lifecycle",
|
|
relation_kinds: ["releases"],
|
|
reason: "ambiguous-target",
|
|
detail: `Static ${operation.method} resource ownership is ambiguous.`,
|
|
candidate_node_ids: resources.map(({ node_id: identifier }) => identifier),
|
|
evidence: unknownSemanticEvidence(context.file, operation.location),
|
|
});
|
|
};
|