Codegen: instance swaps throw at execute time — findPropKey is called on the INSTANCE (componentPropertyDefinitions undefined) instead of the component set
Summary
Instance swaps (INSTANCE.swaps) cannot work in Bridge 7.3.1: the generated code looks up the INSTANCE_SWAP property key on the instance node via findPropKey(instVar, slotName, "INSTANCE_SWAP"), but findPropKey reads componentPropertyDefinitions, which only exists on ComponentNode / ComponentSetNode. On an InstanceNode it is undefined, so Object.keys(undefined) throws and the whole build chunk fails at execute time.
Where
lib/compiler/codegen.ts, emitInstanceSwaps():
lines.push("var " + slotKeyVar + " = findPropKey(" + instVar + ", " + JSON.stringify(slotName) + ', "INSTANCE_SWAP");');
lines.push("if (" + slotKeyVar + ") " + instVar + ".setProperties({ [" + slotKeyVar + "]: " + swapCompVar + ".id });");
with the emitted helper:
function findPropKey(compSet, prefix, type) {
var defs = compSet.componentPropertyDefinitions; // undefined on an InstanceNode
return Object.keys(defs).find(function(k) { return k.split('#')[0] === prefix && defs[k].type === type; });
}
By contrast emitPropertyOverrides() correctly passes the component set variable (compSetVar) to findPropKey, which is why TEXT / BOOLEAN / VARIANT overrides work.
Evidence (Plugin API, read-only, Figma Desktop)
On a real component set (Actions/Button) whose set definitions include two INSTANCE_SWAP props:
{
"swapDefsOnSet": ["Left icon#11774:371", "Right icon#11774:447"],
"componentPropertyDefinitionsOnInstance": "undefined",
"objectKeysResult": "THROWS: Cannot convert undefined or null to object",
"instanceComponentPropertiesKeys": ["Style"]
}
So with a scene graph such as
{ "type": "INSTANCE", "name": "Cta", "component": "Actions/Button",
"variant": { "Variant": "Primary", "Size": "LG", "State": "Default" },
"properties": { "Label": "Continue", "Show Right Icon": true },
"swaps": { "Right icon": "arrow-right" } }
compile exits 0 (the swap target resolves through icons.json), and execution throws TypeError: Cannot convert undefined or null to object inside findPropKey when the build chunk reaches the swap line.
Proposal
- Pass the component-set variable to
findPropKeyfor swaps, exactly asemitPropertyOverridesdoes (compSetVaris already computed a few lines above). For standaloneCOMPONENTimports (no set), fall back toinstance.mainComponent's definitions, or toObject.keys(instance.componentProperties)filtered ontype === "INSTANCE_SWAP", which is available on instances and already carries the#idsuffix. - Add a unit test on
emitInstanceSwapsasserting the emitted lookup targets the set /componentProperties, not the instance.
Related: #24 (properties on exposed nested instances; unknown property keys are skipped silently — a swap slot that does not exist would also be skipped silently once the crash is fixed, so a RESOLVE_PROPERTY_NOT_FOUND diagnostic would help here too).
Happy to send a PR — the fix is a one-argument change plus a test.