AI-native design editor. Open-source Figma alternative.
fix: validate component property reference scopes
Danila Poyarkov0a6ce6e72d0b
diff --git a/packages/core/src/design-jsx/component-properties.ts b/packages/core/src/design-jsx/component-properties.tsindex 0a0d01c4b..905395a8f 100644--- a/packages/core/src/design-jsx/component-properties.ts+++ b/packages/core/src/design-jsx/component-properties.ts@@ -24,7 +24,8 @@ const referenceSchema = v.object({/** Accept the native graph contracts, without introducing a second property model. */export function componentMetadata(props: Record<string, unknown>,- type: SceneNode['type']+ type: SceneNode['type'],+ definitions?: readonly ComponentPropertyDefinition[]): Partial<SceneNode> {const result: Partial<SceneNode> = {}if (props.properties !== undefined && type !== 'INSTANCE') {@@ -42,12 +43,40 @@ export function componentMetadata(throw new Error('TEXT properties require a text node')if (reference.field === 'INSTANCE_SWAP' && type !== 'INSTANCE')throw new Error('INSTANCE_SWAP properties require an instance')+ if (definitions) {+ const definition = definitions.find((item) => item.id === reference.propertyId)+ if (!definition)+ throw new Error(`Unknown component property reference: ${reference.propertyId}`)+ const expectedField = definition.type === 'BOOLEAN' ? 'VISIBLE' : definition.type+ if (reference.field !== expectedField)+ throw new Error(`Component property ${definition.id} cannot bind ${reference.field}`)+ }}result.componentPropertyReferences = references}return result}+/** Match the nearest component scope, including definitions inherited from its set. */+export function componentPropertyScope(+ graph: SceneGraph,+ parentId: string+): readonly ComponentPropertyDefinition[] | undefined {+ let parent = graph.getNode(parentId)+ while (parent) {+ if (parent.type === 'INSTANCE') return componentPropertyDefinitions(graph, parent)+ if (parent.type === 'COMPONENT_SET') return parent.componentPropertyDefinitions+ if (parent.type === 'COMPONENT') {+ const set = parent.parentId ? graph.getNode(parent.parentId) : undefined+ return set?.type === 'COMPONENT_SET'+ ? [...set.componentPropertyDefinitions, ...parent.componentPropertyDefinitions]+ : parent.componentPropertyDefinitions+ }+ parent = parent.parentId ? graph.getNode(parent.parentId) : undefined+ }+ return undefined+}+export function assignComponentProperties(graph: SceneGraph,instance: SceneNode,diff --git a/packages/core/src/design-jsx/renderer.ts b/packages/core/src/design-jsx/renderer.tsindex 33f1c983b..927e51365 100644--- a/packages/core/src/design-jsx/renderer.ts+++ b/packages/core/src/design-jsx/renderer.ts@@ -16,7 +16,11 @@ import type { IconData } from '#core/icons/types'import { computeAllLayouts } from '#core/layout'import { randomHex } from '#core/random'-import { assignComponentProperties, componentMetadata } from './component-properties'+import {+ assignComponentProperties,+ componentMetadata,+ componentPropertyScope+} from './component-properties'import { applySizeOverrides, propsToOverrides } from './props-overrides'import { prepareScalarBindings } from './scalar-bindings'import { isTreeNode } from './tree'@@ -428,7 +432,7 @@ async function renderInstanceNode(}const overrides = {...propsToOverrides(props, false, parentLayout),- ...componentMetadata(props, 'INSTANCE')+ ...componentMetadata(props, 'INSTANCE', componentPropertyScope(graph, parentId))}const instance =graph.createInstance(component.id, parentId, overrides) ?? graph.createNode('FRAME', parentId)@@ -490,7 +494,7 @@ async function renderArtworkNode(tree: TreeNode,parentId: string): Promise<SceneNode> {- const metadata = componentMetadata(tree.props, 'VECTOR')+ const metadata = componentMetadata(tree.props, 'VECTOR', componentPropertyScope(graph, parentId))const node =tree.type === 'icon'? await renderIconNode(graph, tree, parentId)@@ -513,7 +517,7 @@ async function renderNode(graph: SceneGraph, tree: TreeNode, parentId: string):const { props, bindings } = preparePropsForRender(graph, tree.props, isText, parentId)const overrides = {...propsToOverrides(props, isText, parentLayout),- ...componentMetadata(props, nodeType)+ ...componentMetadata(props, nodeType, componentPropertyScope(graph, parentId))}if (isText) {diff --git a/tests/engine/render/jsx/component-property-references.test.ts b/tests/engine/render/jsx/component-property-references.test.tsnew file mode 100644index 000000000..4a1e6e25a--- /dev/null+++ b/tests/engine/render/jsx/component-property-references.test.ts@@ -0,0 +1,65 @@+import { expect, test } from 'bun:test'++import { Component, ComponentSet, Frame, Text, renderTree } from '@open-pencil/core/design-jsx'+import type { ComponentPropertyDefinition } from '@open-pencil/scene-graph'++import { makeSceneGraph } from '#tests/helpers/scene'++const definitions: ComponentPropertyDefinition[] = [+ { id: 'caption', name: 'Caption', type: 'TEXT', defaultValue: 'Hello' },+ { id: 'visible', name: 'Visible', type: 'BOOLEAN', defaultValue: 'true' }+]++for (const reference of [+ { propertyId: 'missing', field: 'TEXT' },+ { propertyId: 'visible', field: 'TEXT' },+ { propertyId: 'caption', field: 'VISIBLE' }+] as const) {+ test(`rejects invalid scoped reference ${reference.propertyId}/${reference.field}`, async () => {+ await expect(+ renderTree(+ makeSceneGraph(),+ Component({+ properties: definitions,+ children: Frame({ children: Text({ children: 'Hello', propertyRefs: [reference] }) })+ })+ )+ ).rejects.toThrow(/component property/i)+ })+}++test('accepts references inherited from the owning component set', async () => {+ const result = await renderTree(+ makeSceneGraph(),+ ComponentSet({+ properties: definitions,+ children: Component({+ name: 'Kind=Default',+ children: Frame({+ children: Text({+ children: 'Hello',+ propertyRefs: [{ propertyId: 'caption', field: 'TEXT' }]+ })+ })+ })+ })+ )+ expect(result.type).toBe('COMPONENT_SET')+})++test('does not resolve a nested component reference against the outer component', async () => {+ await expect(+ renderTree(+ makeSceneGraph(),+ Component({+ properties: definitions,+ children: Component({+ children: Text({+ children: 'Hello',+ propertyRefs: [{ propertyId: 'caption', field: 'TEXT' }]+ })+ })+ })+ )+ ).rejects.toThrow('Unknown component property reference: caption')+})