mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
refactor(javascript): share literal definition graph edges (#16619)
This commit is contained in:
1 parent
47a778e96a
commit
e1241ad89a
1 file changed
+27
-46
@@ -3,6 +3,7 @@ import {
|
||||
semanticPropertyPointer,
|
||||
} from "../../domain/javascript/javascriptSemanticSlots.js";
|
||||
import { digestCanonicalValue } from "../../domain/canonicalDigest.js";
|
||||
import { semanticPrimitiveCandidates } from "../../domain/javascript/javascriptSemanticPrimitives.js";
|
||||
import type { JavaScriptSemanticBinding } from "../../domain/javascript/javascriptSemanticIr.js";
|
||||
import type {
|
||||
JavaScriptSemanticProperty,
|
||||
@@ -96,14 +97,7 @@ const projectInitializerCandidates = (
|
||||
};
|
||||
|
||||
/** Retain initializer descendants as candidates without asserting the unknown receiver still exists. */
|
||||
const projectUnknownInitializer = ({
|
||||
context,
|
||||
binding,
|
||||
initial,
|
||||
unknown,
|
||||
target,
|
||||
path,
|
||||
}: {
|
||||
const projectUnknownInitializer = (input: {
|
||||
context: SemanticFlowProjectionContext;
|
||||
binding: JavaScriptSemanticBinding;
|
||||
initial: JavaScriptSemanticValue;
|
||||
@@ -114,26 +108,15 @@ const projectUnknownInitializer = ({
|
||||
target: ReturnType<typeof semanticPropertySlot>;
|
||||
path: readonly string[];
|
||||
}): void => {
|
||||
const { context, binding, initial, unknown, target, path } = input;
|
||||
if (target === null) return;
|
||||
const values =
|
||||
initial.status === "literal"
|
||||
? [initial.value]
|
||||
: initial.status === "union"
|
||||
? initial.values
|
||||
: [];
|
||||
for (const value of values) {
|
||||
const literal = addLiteralNode(
|
||||
context,
|
||||
binding,
|
||||
for (const value of semanticPrimitiveCandidates(initial) ?? []) {
|
||||
const literal = addLiteralDefinition(
|
||||
input,
|
||||
value,
|
||||
`property:${semanticPropertyPointer(path)}`,
|
||||
"candidate",
|
||||
);
|
||||
addSemanticGraphRelation(context.state, {
|
||||
source: literal,
|
||||
target,
|
||||
relation: "defines",
|
||||
resolution: "candidate",
|
||||
});
|
||||
if (literal !== null)
|
||||
addSemanticGraphUnknown(context.state, {
|
||||
node_id: literal.node_id,
|
||||
@@ -193,24 +176,11 @@ const projectValue = (input: ValueProjectionInput): void => {
|
||||
const role =
|
||||
path.length === 0 ? "binding" : `property:${semanticPropertyPointer(path)}`;
|
||||
if (target === null) return;
|
||||
if (value.status === "literal") {
|
||||
const literal = addLiteralNode(context, binding, value.value, role);
|
||||
addSemanticGraphRelation(context.state, {
|
||||
source: literal,
|
||||
target,
|
||||
relation: "defines",
|
||||
resolution: "resolved",
|
||||
});
|
||||
} else if (value.status === "union")
|
||||
for (const primitive of value.values) {
|
||||
const literal = addLiteralNode(context, binding, primitive, role);
|
||||
addSemanticGraphRelation(context.state, {
|
||||
source: literal,
|
||||
target,
|
||||
relation: "defines",
|
||||
resolution: "candidate",
|
||||
});
|
||||
}
|
||||
if (value.status === "literal")
|
||||
addLiteralDefinition(input, value.value, role, "resolved");
|
||||
else if (value.status === "union")
|
||||
for (const primitive of value.values)
|
||||
addLiteralDefinition(input, primitive, role, "candidate");
|
||||
else if (value.status === "object" || value.status === "array") {
|
||||
const container = semanticContainer(value);
|
||||
for (const property of container.slots) {
|
||||
@@ -278,18 +248,22 @@ const projectValue = (input: ValueProjectionInput): void => {
|
||||
}
|
||||
};
|
||||
|
||||
const addLiteralNode = (
|
||||
context: SemanticFlowProjectionContext,
|
||||
binding: JavaScriptSemanticBinding,
|
||||
const addLiteralDefinition = (
|
||||
{
|
||||
context,
|
||||
binding,
|
||||
target,
|
||||
}: Pick<ValueProjectionInput, "context" | "binding" | "target">,
|
||||
value: string | number | boolean | null,
|
||||
role: string,
|
||||
resolution: "resolved" | "candidate",
|
||||
) => {
|
||||
const encodedValue = JSON.stringify(value);
|
||||
const valueDigest = `value-sha256:${digestCanonicalValue(value, "Semantic literal")}`;
|
||||
const valueKey =
|
||||
encodedValue.length <= valueDigest.length ? encodedValue : valueDigest;
|
||||
const stringLabel = "string literal";
|
||||
return retainSemanticGraphNode(context.state, context.file, {
|
||||
const literal = retainSemanticGraphNode(context.state, context.file, {
|
||||
kind: "literal",
|
||||
// Identity needs an exact value commitment, not another payload copy.
|
||||
// Keep the complete literal in properties for queries and Evidence export.
|
||||
@@ -303,6 +277,13 @@ const addLiteralNode = (
|
||||
context.bindingNodes.get(binding.bindingId)?.function_node_id ?? null,
|
||||
properties: { value },
|
||||
});
|
||||
addSemanticGraphRelation(context.state, {
|
||||
source: literal,
|
||||
target,
|
||||
relation: "defines",
|
||||
resolution,
|
||||
});
|
||||
return literal;
|
||||
};
|
||||
|
||||
/** Create one canonical slot for a root binding and exact static property path. */
|
||||
|
||||
Reference in new issue
Block a user