AI-native design editor. Open-source Figma alternative.
fix(fig): ignore component property references the parameter map no longer carries (#1613)
Andrew Avseninde7b092eeff3
diff --git a/changes/1613/fixed.md b/changes/1613/fixed.mdnew file mode 100644index 000000000..c73032ada--- /dev/null+++ b/changes/1613/fixed.md@@ -0,0 +1 @@+- `.fig` import binds layers to component properties only through their parameter map, as Figma does, so legacy property references no longer hide or retext layers Figma draws.diff --git a/packages/fig/src/document/property-records.ts b/packages/fig/src/document/property-records.tsindex b90bed0c8..f4d5190ca 100644--- a/packages/fig/src/document/property-records.ts+++ b/packages/fig/src/document/property-records.ts@@ -20,20 +20,20 @@ function normalizeDefaults(node: NodeChange): void {}}+/**+ * Figma binds a layer to component properties through its parameter map alone: a legacy+ * `componentPropRefs` entry draws nothing, whether or not the map repeats it (live-verified), so+ * the references are the map's.+ */export function normalizeComponentPropertyRecords(node: NodeChange): void {normalizeDefaults(node)- const parameters = node.parameterConsumptionMap- const refs = structuredClone((node.componentPropRefs as ComponentPropRef[] | undefined) ?? [])- for (const entry of parameters?.entries ?? []) {+ const refs: ComponentPropRef[] = []+ for (const entry of node.parameterConsumptionMap?.entries ?? []) {const id = entry.variableData?.value?.propRefValue?.defIdif (entry.variableData?.dataType !== 'PROP_REF' || !id || !entry.variableField) continueif (!BOUND_FIELDS.has(entry.variableField)) continue- const ref = { defID: structuredClone(id), componentPropNodeField: entry.variableField }- const index = refs.findIndex(- (candidate) => candidate.componentPropNodeField === entry.variableField- )- if (index === -1) refs.push(ref)- else refs[index] = ref+ refs.push({ defID: structuredClone(id), componentPropNodeField: entry.variableField })}if (refs.length) node.componentPropRefs = refs+ else Reflect.deleteProperty(node, 'componentPropRefs')}diff --git a/packages/fig/tests/document/checkpoint.test.ts b/packages/fig/tests/document/checkpoint.test.tsindex 67682583e..2de07baf7 100644--- a/packages/fig/tests/document/checkpoint.test.ts+++ b/packages/fig/tests/document/checkpoint.test.ts@@ -1,5 +1,6 @@import { expect, test } from 'bun:test'+import { propertyBinding } from '#fig-tests/helpers/component-props'import { figArchiveOf } from '#fig-tests/helpers/fig-fixtures'import { guid } from '#fig-tests/helpers/guid'import { checkpointComponent, restoreComponentCheckpoint } from '#fig/document/component/checkpoint'@@ -43,7 +44,7 @@ test('checkpoint recovery keeps the effective fill of a swapped instance on a laparentIndex: { guid: guid(4), position: '!' },fillPaints: paint(1),symbolData: { symbolID: guid(2) },- componentPropRefs: [{ defID: guid(90), componentPropNodeField: 'OVERRIDDEN_SYMBOL_ID' }]+ ...propertyBinding(guid(90), 'OVERRIDDEN_SYMBOL_ID')},{guid: guid(6),diff --git a/tests/engine/io/fig/import/component-props.test.ts b/packages/fig/tests/document/component/properties.test.tssimilarity index 97%rename from tests/engine/io/fig/import/component-props.test.tsrename to packages/fig/tests/document/component/properties.test.tsindex e2e774e10..969bc1346 100644--- a/tests/engine/io/fig/import/component-props.test.ts+++ b/packages/fig/tests/document/component/properties.test.ts@@ -1,5 +1,7 @@import { describe, expect, test } from 'bun:test'+import { propertyBinding } from '#fig-tests/helpers/component-props'+import { materializeDocument } from '@open-pencil/fig'import type { SymbolData } from '@open-pencil/fig/instance-overrides'import type { NodeChange } from '@open-pencil/kiwi/fig/codec'@@ -32,7 +34,7 @@ function baseTextChange(): NodeChange {fontSize: 14,fontName: { family: 'Inter', style: 'Regular', postscript: '' },textData: { characters: 'Menu Item', lines: [{ lineType: 'PLAIN' }] },- componentPropRefs: [{ defID: textPropGuid, componentPropNodeField: 'TEXT_DATA' }]+ ...propertyBinding(textPropGuid, 'TEXT_DATA')}}@@ -138,7 +140,7 @@ describe('Figma component property import', () => {type: 'FRAME',name: 'Icon',visible: true,- componentPropRefs: [{ defID: visibilityPropGuid, componentPropNodeField: 'VISIBLE' }]+ ...propertyBinding(visibilityPropGuid, 'VISIBLE')},{guid: plainIconGuid,@@ -296,7 +298,7 @@ describe('Figma component property import', () => {symbolData: { symbolID: mailIconGuid },size: { x: 16, y: 16 },transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 },- componentPropRefs: [{ defID: iconPropGuid, componentPropNodeField: 'OVERRIDDEN_SYMBOL_ID' }]+ ...propertyBinding(iconPropGuid, 'OVERRIDDEN_SYMBOL_ID')},{guid: sourceInstanceGuid,diff --git a/packages/fig/tests/document/property-inheritance.test.ts b/packages/fig/tests/document/property-inheritance.test.tsindex e3c93aac0..57f099b41 100644--- a/packages/fig/tests/document/property-inheritance.test.ts+++ b/packages/fig/tests/document/property-inheritance.test.ts@@ -1,5 +1,6 @@import { expect, test } from 'bun:test'+import { propertyBinding } from '#fig-tests/helpers/component-props'import { guid } from '#fig-tests/helpers/guid'import { createDocumentReader } from '#fig/document/read'@@ -26,7 +27,7 @@ test('inherits parent definition semantics without replacing local property identype: 'INSTANCE',parentIndex: { guid: guid(2), position: '!' },symbolData: { symbolID: guid(4) },- componentPropRefs: [{ defID: guid(11), componentPropNodeField: 'OVERRIDDEN_SYMBOL_ID' }]+ ...propertyBinding(guid(11), 'OVERRIDDEN_SYMBOL_ID')},{ guid: guid(4), type: 'SYMBOL', name: 'Default' },{ guid: guid(5), type: 'SYMBOL', name: 'Replacement' },diff --git a/packages/fig/tests/document/property-records.test.ts b/packages/fig/tests/document/property-records.test.tsnew file mode 100644index 000000000..3f5db6ccf--- /dev/null+++ b/packages/fig/tests/document/property-records.test.ts@@ -0,0 +1,62 @@+import { describe, expect, test } from 'bun:test'++import { propertyBinding } from '#fig-tests/helpers/component-props'+import { guid } from '#fig-tests/helpers/guid'+import { createDocumentReader } from '#fig/document/read'++import type { NodeChange } from '@open-pencil/kiwi/fig/codec'++const SHOW_ICON = guid(10)++/** Whether the icon an instance hides through a boolean property still draws. */+function iconShownWhenHidden(binding: Partial<NodeChange>): boolean | undefined {+ const changes: NodeChange[] = [+ {+ guid: guid(1),+ type: 'SYMBOL',+ name: 'Card',+ componentPropDefs: [+ { id: SHOW_ICON, name: 'Show icon', type: 'BOOL', initialValue: { boolValue: true } }+ ]+ },+ {+ guid: guid(2),+ type: 'FRAME',+ name: 'Icon',+ parentIndex: { guid: guid(1), position: '!' },+ visible: true,+ ...binding+ },+ { guid: guid(3), type: 'CANVAS' },+ {+ guid: guid(4),+ type: 'INSTANCE',+ parentIndex: { guid: guid(3), position: '!' },+ symbolData: { symbolID: guid(1) },+ componentPropAssignments: [{ defID: SHOW_ICON, value: { boolValue: false } }]+ }+ ]+ const card = createDocumentReader(changes).readPage('1:3').children[0]+ return card.children[0].properties.visible+}++const LEGACY_REF = { componentPropRefs: [{ defID: SHOW_ICON, componentPropNodeField: 'VISIBLE' }] }++// Figma reads a layer's property bindings from its parameter map only (live-verified).+describe('component property references', () => {+ test('a property binding in the parameter map drives the layer', () => {+ expect(iconShownWhenHidden({ ...LEGACY_REF, ...propertyBinding(SHOW_ICON, 'VISIBLE') })).toBe(+ false+ )+ })++ test('a legacy reference the parameter map no longer carries is stale', () => {+ expect(iconShownWhenHidden({ ...LEGACY_REF, parameterConsumptionMap: { entries: [] } })).toBe(+ true+ )+ })++ test('a legacy reference without a parameter map is stale too', () => {+ expect(iconShownWhenHidden(LEGACY_REF)).toBe(true)+ })+})diff --git a/packages/fig/tests/helpers/component-props.ts b/packages/fig/tests/helpers/component-props.tsindex 78482ce50..4ff385a02 100644--- a/packages/fig/tests/helpers/component-props.ts+++ b/packages/fig/tests/helpers/component-props.ts@@ -1,5 +1,6 @@import type { ComponentPropDef, ComponentPropRef } from '#fig/instance-overrides/types'+import type { GUID } from '@open-pencil/kiwi/fig'import type { NodeChange } from '@open-pencil/kiwi/fig/codec'/** Export writes preferred values onto a definition; the interpreter never reads them. */@@ -17,3 +18,31 @@ export function componentPropDefsOf(record: NodeChange): ExportedComponentPropDeexport function componentPropRefsOf(record: NodeChange): ComponentPropRef[] | undefined {return record.componentPropRefs as ComponentPropRef[] | undefined}++const BOUND_FIELD_TYPES = {+ VISIBLE: 'BOOLEAN',+ TEXT_DATA: 'STRING',+ OVERRIDDEN_SYMBOL_ID: 'SYMBOL_ID',+ SLOT_CONTENT_ID: 'SLOT_CONTENT_ID'+} as const++/** A layer's binding to a component property, written as Figma does: in its parameter map. */+export function propertyBinding(+ defId: GUID,+ field: keyof typeof BOUND_FIELD_TYPES+): Pick<NodeChange, 'parameterConsumptionMap'> {+ return {+ parameterConsumptionMap: {+ entries: [+ {+ variableField: field,+ variableData: {+ value: { propRefValue: { defId } },+ dataType: 'PROP_REF',+ resolvedDataType: BOUND_FIELD_TYPES[field]+ }+ }+ ]+ }+ }+}diff --git a/tests/e2e/canvas/instance-scale-visual.spec.ts b/tests/e2e/canvas/instance-scale-visual.spec.tsindex 46cf56a88..70b784c8b 100644--- a/tests/e2e/canvas/instance-scale-visual.spec.ts+++ b/tests/e2e/canvas/instance-scale-visual.spec.ts@@ -251,7 +251,18 @@ test('a bound root swap renders its replacement paint rather than its old cache'size: { x: 40, y: 40 },fillPaints: paint(1, 0),symbolData: { symbolID: guid(2) },- componentPropRefs: [{ defID: guid(90), componentPropNodeField: 'OVERRIDDEN_SYMBOL_ID' }]+ parameterConsumptionMap: {+ entries: [+ {+ variableField: 'OVERRIDDEN_SYMBOL_ID',+ variableData: {+ value: { propRefValue: { defId: guid(90) } },+ dataType: 'PROP_REF',+ resolvedDataType: 'SYMBOL_ID'+ }+ }+ ]+ }},{guid: guid(6),diff --git a/tools/checks/test-homes/engine-baseline.txt b/tools/checks/test-homes/engine-baseline.txtindex ed118eedb..521ea86e5 100644--- a/tools/checks/test-homes/engine-baseline.txt+++ b/tools/checks/test-homes/engine-baseline.txt@@ -173,7 +173,6 @@ tests/engine/io/fig/heavy/fixtures.test.tstests/engine/io/fig/import/basic.test.tstests/engine/io/fig/import/boolean-operation.test.tstests/engine/io/fig/import/cancellation.test.ts-tests/engine/io/fig/import/component-props.test.tstests/engine/io/fig/import/derived-symbol-data.test.tstests/engine/io/fig/import/edge-cases.test.tstests/engine/io/fig/import/field-protection.test.ts