diff --git a/src/domain/interfaceBuilderGraph.test.ts b/src/domain/interfaceBuilderGraph.test.ts index f28743322..d88547bb6 100644 --- a/src/domain/interfaceBuilderGraph.test.ts +++ b/src/domain/interfaceBuilderGraph.test.ts @@ -317,3 +317,110 @@ describe("keyed archive decoding and graph limits", () => { ); }); }); + +describe("AppKit keyed archive connectors", () => { + it("reads outlet and control connectors from NS-prefixed keys", () => { + const parsed = parseInterfaceBuilderRecords({ + $archiver: "NSKeyedArchiver", + $objects: [ + "$null", + { $class: { UID: 6 } }, + { $class: { UID: 7 }, identifier: "unrelated-identifier" }, + { + $class: { UID: 8 }, + NSSource: { UID: 1 }, + NSDestination: { UID: 2 }, + NSLabel: { UID: 5 }, + }, + { + $class: { UID: 9 }, + NSSource: { UID: 2 }, + NSDestination: { UID: 1 }, + NSLabel: { UID: 10 }, + }, + "o_button", + { + $classname: "NSCustomObject", + $classes: ["NSCustomObject", "NSObject"], + }, + { $classname: "NSButton", $classes: ["NSButton", "NSObject"] }, + { + $classname: "NSNibOutletConnector", + $classes: ["NSNibOutletConnector", "NSNibConnector", "NSObject"], + }, + { + $classname: "NSNibControlConnector", + $classes: ["NSNibControlConnector", "NSNibConnector", "NSObject"], + }, + "buttonClicked:", + ], + $top: {}, + }); + + expect(parsed.connections).toEqual([ + expect.objectContaining({ + kind: "outlet", + source_id: "1", + destination_id: "2", + label: "o_button", + }), + expect.objectContaining({ + kind: "action", + source_id: "2", + destination_id: "1", + label: "buttonClicked:", + }), + ]); + }); + + it("projects an AppKit action from the control through the selector to its target", () => { + const analysis = buildInterfaceBuilderAnalysis({ + targetSha256: hash, + toolVersion: "test", + documents: [ + { + relativePath: "MainMenu.nib", + archiveSha256: hash, + documentKind: "nib", + raw: { + $archiver: "NSKeyedArchiver", + $objects: [ + "$null", + { $class: { UID: 4 } }, + { $class: { UID: 5 } }, + { + $class: { UID: 6 }, + NSSource: { UID: 2 }, + NSDestination: { UID: 1 }, + NSLabel: "buttonClicked:", + }, + { $classname: "BuildDelegate", $classes: ["NSObject"] }, + { $classname: "NSButton", $classes: ["NSButton", "NSObject"] }, + { + $classname: "NSNibControlConnector", + $classes: ["NSNibControlConnector", "NSObject"], + }, + ], + $top: {}, + }, + }, + ], + limits: interfaceBuilderLimitsSchema.parse({}), + }); + const byId = new Map(analysis.graph.nodes.map((node) => [node.id, node])); + const action = analysis.graph.nodes.find(({ kind }) => kind === "action"); + const from = analysis.graph.edges + .filter( + ({ to, relation }) => relation === "target_action" && to === action?.id, + ) + .map(({ from: id }) => byId.get(id)?.name); + const to = analysis.graph.edges + .filter( + ({ from: id, relation }) => + relation === "target_action" && id === action?.id, + ) + .map(({ to: id }) => (id === null ? null : byId.get(id)?.kind)); + expect(from).toEqual(["NSButton"]); + expect(to).toEqual(expect.arrayContaining(["objc_selector", "unknown"])); + }); +}); diff --git a/src/domain/interfaceBuilderKeyedArchive.ts b/src/domain/interfaceBuilderKeyedArchive.ts index 24378a75c..e0ca876ae 100644 --- a/src/domain/interfaceBuilderKeyedArchive.ts +++ b/src/domain/interfaceBuilderKeyedArchive.ts @@ -216,8 +216,13 @@ const parseKeyedArchive = ( ) ) { const controlAction = /ControlConnector/iu.test(className); + // AppKit connectors (NSNibOutletConnector, NSNibControlConnector) + // archive NSSource as the outlet owner or sending control and + // NSDestination as the outlet value or action target, so they are read + // in that order without the inversion the unprefixed fields use. const source = firstObjectReference( controlAction ? fields.destination : fields.source, + archivedObjectReference(fields.NSSource), controlAction ? fields.to : fields.from, controlAction ? fields.target : fields.owner, ); @@ -229,11 +234,13 @@ const parseKeyedArchive = ( source_id: source, destination_id: firstObjectReference( controlAction ? fields.source : fields.destination, + archivedObjectReference(fields.NSDestination), controlAction ? fields.from : fields.to, controlAction ? fields.owner : fields.target, ), label: firstString( fields.label, + fields.NSLabel, fields.selector, fields.identifier, fields.action, @@ -285,6 +292,18 @@ export const firstString = (...values: unknown[]): string | null => { return null; }; +/** + * The node identity of a resolved archive reference: its authored object ID, + * otherwise its archive UID, which is how the referenced object's node is keyed. + */ +const archivedObjectReference = (value: unknown): string | null => { + const item = record(value); + if (typeof item.archiveUID !== "number") return null; + return ( + firstString(item.objectID, item["object-id"]) ?? String(item.archiveUID) + ); +}; + const firstObjectReference = (...values: unknown[]): string | null => { for (const value of values) { const direct = firstString(value);