AI-native design editor. Open-source Figma alternative.
feat(core): name the node type createInstance and detachInstance return (#918)
Danila Poyarkov105032153e13
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ac68704c2..5587e7e28 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -60,6 +60,7 @@### Changed+- `createInstance()` and `detachInstance()` in the plugin API return the node type they built, so reading `componentProperties`, `setProperties`, or `isExposedInstance` from a new instance no longer needs a cast; `FigmaInstanceNode` is exported alongside the other node types.- Name the tool and list every invalid argument with where it is when AI chat, the CLI, or WebMCP calls a tool wrongly, as MCP clients already saw, as in `Invalid arguments for create_shape:` followed by `× Invalid type: Expected ("FRAME" | …) but received "CIRCLE"` and `→ at type`. Design JSX component properties and gradient stops report their problems the same way. Previously only the first problem was named, without the tool or the argument.- HTML and Tailwind JSX export write variable-bound colors, spacing, radii, borders, sizes, type sizes, and opacity as the tokens they come from, such as `var(--color-primary)` or `bg-primary`, and put layers set to another mode in it with an attribute such as `data-theme="dark"`. Values CSS would not resolve as the canvas draws them stay literal, and standalone HTML includes the stylesheet for the tokens it uses.- Clicking selects layers as in Figma: inside a top-level frame or section, a click selects the frame's direct child rather than the deepest layer, the empty part of a top-level frame or section that holds layers selects nothing, and once a layer is selected, clicks reach its siblings and cousins. Double-click goes one level deeper and ⌘-click (Ctrl-click on Windows and Linux) selects the deepest layer. A marquee started inside a top-level frame or section selects that container's layers, and from the page it selects a frame or section that holds layers only when fully enclosed.diff --git a/packages/core/src/figma-api/compatibility.ts b/packages/core/src/figma-api/compatibility.tsindex 5f84be431..9096784db 100644--- a/packages/core/src/figma-api/compatibility.ts+++ b/packages/core/src/figma-api/compatibility.ts@@ -1,7 +1,12 @@/// <reference types="@figma/plugin-typings" />import type { FigmaAPI } from './index'-import type { FigmaComponentNode, FigmaComponentSetNode, FigmaSlotNode } from './node-types'+import type {+ FigmaComponentNode,+ FigmaComponentSetNode,+ FigmaInstanceNode,+ FigmaSlotNode+} from './node-types'import type { FigmaNodeProxy } from './proxy'type Expect<T extends true> = T@@ -64,7 +69,7 @@ const _componentSetPropertyMethodsMatch: ComponentSetPropertyMethodsMatch = truetype InstancePropertySurfaceMatch = Expect<Extends<Pick<- FigmaNodeProxy & InstanceNode,+ FigmaInstanceNode,| 'componentProperties'| 'componentPropertyReferences'| 'setProperties'diff --git a/packages/core/src/figma-api/index.ts b/packages/core/src/figma-api/index.tsindex 58ae71951..1b2f53b12 100644--- a/packages/core/src/figma-api/index.ts+++ b/packages/core/src/figma-api/index.ts@@ -61,6 +61,7 @@ export type {FigmaEllipseNode,FigmaFrameNode,FigmaGroupNode,+ FigmaInstanceNode,FigmaLineNode,FigmaPolygonNode,FigmaRectangleNode,diff --git a/packages/core/src/figma-api/node-types.ts b/packages/core/src/figma-api/node-types.tsindex 5dba43d08..2a098eb84 100644--- a/packages/core/src/figma-api/node-types.ts+++ b/packages/core/src/figma-api/node-types.ts@@ -12,6 +12,7 @@ export type FigmaPolygonNode = FigmaNodeProxy & PolygonNodeexport type FigmaStarNode = FigmaNodeProxy & StarNodeexport type FigmaComponentNode = FigmaNodeProxy & ComponentNodeexport type FigmaComponentSetNode = FigmaNodeProxy & ComponentSetNode+export type FigmaInstanceNode = FigmaNodeProxy & InstanceNodeexport type FigmaSlotNode = FigmaNodeProxy & SlotNodeexport type FigmaSectionNode = FigmaNodeProxy & SectionNodeexport type FigmaGroupNode = FigmaNodeProxy & GroupNodediff --git a/packages/core/src/figma-api/proxy.ts b/packages/core/src/figma-api/proxy.tsindex 562750c26..eb296ceac 100644--- a/packages/core/src/figma-api/proxy.ts+++ b/packages/core/src/figma-api/proxy.ts@@ -32,6 +32,7 @@ import {import { installVisualNodeProxyAccessors } from './accessors/visual'import { installComponentPropertyAccessors } from './components'import type { FigmaFontName } from './fonts'+import type { FigmaFrameNode, FigmaInstanceNode } from './node-types'import { getPageBackgrounds, setPageBackgrounds } from './page-backgrounds'import * as PluginData from './plugin-data'import { nodeProxyToJSON } from './serialization'@@ -237,22 +238,24 @@ export class FigmaNodeProxy {return this[INTERNAL_API].wrapNode(comp.id)}- createInstance(): FigmaNodeProxy {+ createInstance(): FigmaInstanceNode {const n = this._raw()if (n.type !== 'COMPONENT') throw new Error('createInstance() can only be called on components')const pageId = this[INTERNAL_API].currentPageIdconst inst = this[INTERNAL_GRAPH].createInstance(n.id, pageId)if (!inst) throw new Error('Failed to create instance')- return this[INTERNAL_API].wrapNode(inst.id)+ // `wrapNode` cannot know the node's type; this one just built an instance.+ return this[INTERNAL_API].wrapNode(inst.id) as FigmaInstanceNode}/** Turns this instance into a frame that keeps its current content, like Figma's. */- detachInstance(): FigmaNodeProxy {+ detachInstance(): FigmaFrameNode {const n = this._raw()if (n.type !== 'INSTANCE') throw new Error('detachInstance() can only be called on instances')assertNodeEditable(this[INTERNAL_GRAPH], this[INTERNAL_ID])this[INTERNAL_GRAPH].detachInstance(n.id)- return this[INTERNAL_API].wrapNode(n.id)+ // The node is a frame once detached, which `wrapNode` has no way to tell.+ return this[INTERNAL_API].wrapNode(n.id) as FigmaFrameNode}/** Points this instance at another component, as Figma's swapComponent does. */diff --git a/packages/core/tests/figma-api/slots.test.ts b/packages/core/tests/figma-api/slots.test.tsindex 8673d6daf..25ccb161d 100644--- a/packages/core/tests/figma-api/slots.test.ts+++ b/packages/core/tests/figma-api/slots.test.ts@@ -6,10 +6,6 @@ import { SceneGraph } from '@open-pencil/scene-graph'/** Figma's typings only expose the slot members on `SlotNode`, so narrow before reading them. */const isSlot = (node: FigmaNodeProxy): node is FigmaSlotNode => node.type === 'SLOT'-/** `createInstance()` is typed as a bare proxy; the instance surface lives on `InstanceNode`. */-const isInstance = (node: FigmaNodeProxy): node is FigmaNodeProxy & InstanceNode =>- node.type === 'INSTANCE'-// Defaults, names, read-back values, and limit reports were recorded by running the same// script against a component in live Figma through figma-use.@@ -78,7 +74,6 @@ describe('slots', () => {component.appendChild(Object.assign(api.createFrame(), { name: 'Locked' }))component.createSlot().appendChild(Object.assign(api.createRectangle(), { name: 'Default' }))const instance = component.createInstance()- if (!isInstance(instance)) throw new Error('Missing instance')const locked = instance.children.find((child) => child.name === 'Locked')const slot = instance.children.find(isSlot)if (!locked || !slot) throw new Error('Missing instance layers')diff --git a/tests/engine/figma/api/components.test.ts b/tests/engine/figma/api/components.test.tsindex feca30100..625d14e6a 100644--- a/tests/engine/figma/api/components.test.ts+++ b/tests/engine/figma/api/components.test.ts@@ -4,7 +4,7 @@ import { hasInstanceOverride } from '@open-pencil/scene-graph'import { expectDefined } from '#tests/helpers/assert'-import { asInstanceNode, asTextNode, createAPI } from './helpers'+import { asTextNode, createAPI } from './helpers'describe('components', () => {test('exposes component property accessors and applies instance properties', () => {@@ -13,7 +13,7 @@ describe('components', () => {component.name = 'Card'component.appendChild(Object.assign(api.createText(), { name: 'Label', characters: 'Default' }))const propertyName = component.addComponentProperty('Label', 'TEXT', 'Default')- const instance = asInstanceNode(component.createInstance())+ const instance = component.createInstance()expect(component.componentPropertyDefinitions[propertyName]?.defaultValue).toBe('Default')expect(instance.componentProperties[propertyName]?.value).toBe('Default')@@ -34,7 +34,7 @@ describe('components', () => {const visibleName = component.addComponentProperty('Visible', 'BOOLEAN', true)label.componentPropertyReferences = { characters: textName }badge.componentPropertyReferences = { visible: visibleName }- const instance = asInstanceNode(component.createInstance())+ const instance = component.createInstance()expect(component.componentPropertyReferences).toEqual({})expect(instance.componentProperties[visibleName]?.value).toBe(true)@@ -43,7 +43,7 @@ describe('components', () => {expect(instance.componentProperties[visibleName]?.value).toBe(false)const nested = api.createComponent()- const slot = asInstanceNode(nested.createInstance())+ const slot = nested.createInstance()slot.componentPropertyReferences = { mainComponent: textName }expect(slot.isExposedInstance).toBe(true)slot.isExposedInstance = falsediff --git a/tests/engine/figma/api/helpers.ts b/tests/engine/figma/api/helpers.tsindex 8ff4ce0c8..0c08b5174 100644--- a/tests/engine/figma/api/helpers.ts+++ b/tests/engine/figma/api/helpers.ts@@ -6,23 +6,14 @@ export function createAPI(): FigmaAPI {return new FigmaAPI(new SceneGraph())}-/**- * Reads a proxy through Figma's `InstanceNode` surface. The proxy installs the instance accessors- * on its prototype but only the node types in `figma-api/node-types` spell them out, so a proxy- * handed back by `createInstance()` or `wrapNode()` needs the node type naming them.- */-export function asInstanceNode(node: FigmaNodeProxy): FigmaNodeProxy & InstanceNode {- return node as FigmaNodeProxy & InstanceNode-}--/** The text counterpart of {@link asInstanceNode}. */+/** Reads a proxy through Figma's `TextNode` surface, which `wrapNode()` cannot name. */export function asTextNode(node: FigmaNodeProxy): FigmaNodeProxy & TextNode {return node as FigmaNodeProxy & TextNode}/**- * The component counterpart of {@link asInstanceNode}, which also names the node a rejection path- * is handed when the runtime guard, not the type, is what the test exercises.+ * Names a proxy a component, including where a rejection path is handed one and the runtime+ * guard, not the type, is what the test exercises.*/export function asComponentNode(node: FigmaNodeProxy): FigmaNodeProxy & ComponentNode {return node as FigmaNodeProxy & ComponentNode