refactor(managed): share member signature matching rounds (#17403)

This commit is contained in:
Vincent Koc
2026-12-06 13:42:37 +08:00
committed by GitHub
parent e5d13c1eb0
commit 9312efa658
@@ -131,74 +131,47 @@ const keyField = (item: Field, declared: DeclaredName): Keyed<Field> => ({
structuralKey: null,
});
const matchMethods = (
left: readonly Keyed<Method>[],
right: readonly Keyed<Method>[],
const matchMembers = <Item extends Declared>(
left: readonly Keyed<Item>[],
right: readonly Keyed<Item>[],
exactBasis: "exact-il-signature" | "field-signature",
allowRemap: boolean,
) =>
matchByKeys({
left,
right,
rounds: [
{
basis: "exact-il-signature",
key: declaredAs(({ exactKey }) => exactKey),
},
{
basis: "exact-signature",
key: ({ signatureKey }) => signatureKey,
competitionKey: ({ signatureId }) => signatureId,
},
{
basis: "structural-method-shape",
// An unresolved owner has no declared shape match to prove.
key: declaredAs((keyed) =>
keyed.identityUnavailable ? null : keyed.structuralKey,
),
competitionKey: ({ structuralKey }) => structuralKey,
},
{
basis: "exact-il-signature",
key: ({ exactKey }) => (allowRemap ? exactKey : null),
remap: true,
},
{
basis: "structural-method-shape",
key: ({ structuralKey }) => (allowRemap ? structuralKey : null),
competitionKey: ({ structuralKey }) => structuralKey,
remap: true,
},
],
) => {
const structural = exactBasis === "exact-il-signature";
const rounds: MatchRound<Item>[] = [
{ basis: exactBasis, key: declaredAs(({ exactKey }) => exactKey) },
{
basis: "exact-signature",
key: ({ signatureKey }) => signatureKey,
competitionKey: ({ signatureId }) => signatureId,
},
];
if (structural)
rounds.push({
basis: "structural-method-shape",
// An unresolved owner has no declared shape match to prove.
key: declaredAs((keyed) =>
keyed.identityUnavailable ? null : keyed.structuralKey,
),
competitionKey: ({ structuralKey }) => structuralKey,
});
rounds.push({
basis: exactBasis,
key: ({ exactKey }) => (allowRemap ? exactKey : null),
remap: true,
});
if (structural)
rounds.push({
basis: "structural-method-shape",
key: ({ structuralKey }) => (allowRemap ? structuralKey : null),
competitionKey: ({ structuralKey }) => structuralKey,
remap: true,
});
return matchByKeys<Item>({ left, right, rounds });
};
const matchFields = (
left: readonly Keyed<Field>[],
right: readonly Keyed<Field>[],
allowRemap: boolean,
) =>
matchByKeys({
left,
right,
rounds: [
{
basis: "field-signature",
key: declaredAs(({ exactKey }) => exactKey),
},
{
basis: "exact-signature",
key: ({ signatureKey }) => signatureKey,
competitionKey: ({ signatureId }) => signatureId,
},
{
basis: "field-signature",
key: ({ exactKey }) => (allowRemap ? exactKey : null),
remap: true,
},
],
});
export type ManagedMethodMatches = ReturnType<typeof matchMethods>;
export type ManagedFieldMatches = ReturnType<typeof matchFields>;
export type ManagedMethodMatches = ReturnType<typeof matchMembers<Method>>;
export type ManagedFieldMatches = ReturnType<typeof matchMembers<Field>>;
interface MatchRound<Item> {
readonly basis: ConcreteMatchBasis;
@@ -502,8 +475,8 @@ export const keyMembers = (
right: ManagedMemberComparisonSide,
scope: ManagedComparisonScope,
): {
readonly methodMatches: ReturnType<typeof matchMethods>;
readonly fieldMatches: ReturnType<typeof matchFields>;
readonly methodMatches: ManagedMethodMatches;
readonly fieldMatches: ManagedFieldMatches;
} => {
const declared = (
inspection: ManagedMemberInspection,
@@ -530,9 +503,10 @@ export const keyMembers = (
right.result.methods,
matchable,
(a, b) =>
matchMethods(
matchMembers(
a,
b,
"exact-il-signature",
scope.left.inventoryComplete.method &&
scope.right.inventoryComplete.method,
),
@@ -544,9 +518,10 @@ export const keyMembers = (
right.result.fields,
matchable,
(a, b) =>
matchFields(
matchMembers(
a,
b,
"field-signature",
scope.left.inventoryComplete.field &&
scope.right.inventoryComplete.field,
),