refactor(environment): classify learned identifier pairs once (#15828)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 07:58:29 +00:00
1 parent 6555557989
commit 74fa22cc71
1 file changed
+23 -33
@@ -297,22 +297,6 @@ interface LearnContext {
readonly identity: boolean;
}
/** Two values under one key as a pair of ids, when they are: an identifier field, or outside one only a string the state holds as an id, since a counter that equals an id is not one. */
const idPair = (
key: string,
value: JsonValue,
other: JsonValue | undefined,
heldIds: ReadonlySet<string>,
): readonly [string | number, string | number] | undefined =>
isScalarId(value) &&
isScalarId(other) &&
(ID_FIELD.test(key) ||
(typeof value === "string" &&
typeof other === "string" &&
heldIds.has(other)))
? [value, other]
: undefined;
const childPointer = (at: string, key: string | number): string =>
`${at}/${escapeJsonPointerToken(String(key))}`;
@@ -353,23 +337,29 @@ const learnAt = (
for (const [key, value] of Object.entries(live)) {
if (!Object.hasOwn(env, key)) continue;
const other = env[key];
const pair = idPair(key, value, other, context.heldIds);
if (pair !== undefined) {
const stateReferenceField =
!ID_FIELD.test(key) &&
typeof value === "string" &&
typeof other === "string" &&
context.heldIds.has(other);
context.map.learn(
...pair,
own && IDENTITY_FIELD.test(key) ? context.scope : undefined,
stateReferenceField ? key : undefined,
);
} else
learnAt(value, other, context, {
at: childPointer(place.at, key),
own: false,
});
// Outside identifier fields, only strings held as ids can teach a
// mapping. A numeric counter that equals an id must stay literal.
if (isScalarId(value) && isScalarId(other)) {
const identifierField = ID_FIELD.test(key);
if (
identifierField ||
(typeof value === "string" &&
typeof other === "string" &&
context.heldIds.has(other))
) {
context.map.learn(
value,
other,
own && IDENTITY_FIELD.test(key) ? context.scope : undefined,
identifierField ? undefined : key,
);
continue;
}
}
learnAt(value, other, context, {
at: childPointer(place.at, key),
own: false,
});
}
};