Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.
TypeScript antigravity-skillsbusiness-knowledgeclaude-codeclaude-skillscodebase-analysiscodexcodex-skillsdeveloper-tools-ai-agentgemini-cli-skillskarpathy-llm-wikiknowledge-baseknowledge-graphmemoryopencode-skillspi-agentunderstandcodevibe-coding
fix(figma): carry token usage to the nearest structural ancestor
gruming07c36f6d68c9
diff --git a/understand-anything-plugin/packages/core/src/figma/__tests__/tokens.test.ts b/understand-anything-plugin/packages/core/src/figma/__tests__/tokens.test.tsindex 2c8fcf9f..37835264 100644--- a/understand-anything-plugin/packages/core/src/figma/__tests__/tokens.test.ts+++ b/understand-anything-plugin/packages/core/src/figma/__tests__/tokens.test.ts@@ -33,3 +33,34 @@ describe("extractTokens", () => {]));});});++describe("extractTokens — nested styled layers", () => {+ // The common real-world case: a screen's visible styling lives on a nested+ // TEXT/RECTANGLE leaf, so the style reference is on 11:0 — NOT on the screen+ // frame (10:0) that is the structural node.+ const nestedDoc: FigmaDocument = {+ name: "MyApp",+ document: {+ id: "0:0", name: "Document", type: "DOCUMENT", children: [+ { id: "1:0", name: "Home", type: "CANVAS", children: [+ { id: "10:0", name: "Home Screen", type: "FRAME", children: [+ { id: "11:0", name: "Title", type: "TEXT", styles: { text: "T_KEY" }, children: [] },+ ] },+ ] },+ ],+ },+ };+ const nestedStyles: FigmaStyles = { meta: { styles: [{ key: "T_KEY", name: "type/heading", style_type: "TEXT" }] } };+ const nestedStructural: GraphNode[] = [+ { id: "screen:10:0", type: "screen", name: "Home Screen", summary: "Home Screen", tags: ["screen"], complexity: "simple", figmaMeta: { fileKey: "ABC", nodeId: "10:0" } },+ ];++ it("attributes nested-layer token usage to the nearest structural ancestor (screen)", () => {+ const { nodes, edges } = extractTokens(nestedDoc, nestedStyles, nestedStructural, "ABC");+ const token = nodes.find((n) => n.type === "token");+ expect(token).toBeTruthy();+ expect(edges).toEqual(expect.arrayContaining([+ expect.objectContaining({ source: "screen:10:0", target: token!.id, type: "uses_token" }),+ ]));+ });+});diff --git a/understand-anything-plugin/packages/core/src/figma/parse/tokens.ts b/understand-anything-plugin/packages/core/src/figma/parse/tokens.tsindex 31d42539..0f38c09f 100644--- a/understand-anything-plugin/packages/core/src/figma/parse/tokens.ts+++ b/understand-anything-plugin/packages/core/src/figma/parse/tokens.ts@@ -39,8 +39,13 @@ export function extractTokens(}const usesSeen = new Set<string>();- function walk(n: FigmaNode) {- const consumerId = graphIdByFigmaId.get(n.id);+ function walk(n: FigmaNode, nearestConsumerId: string | undefined) {+ // Styles (fills/text/effect/grid) are usually applied to nested leaf+ // layers (TEXT/RECTANGLE/…), not to the shallow structural node itself.+ // Attribute a styled node's token usage to the nearest structural+ // ancestor (screen/component/componentSet/instance/page) so real consumer+ // relationships aren't dropped when the styled layer isn't itself a node.+ const consumerId = graphIdByFigmaId.get(n.id) ?? nearestConsumerId;if (consumerId && n.styles) {for (const styleKey of Object.values(n.styles)) {const tokenId = tokenByStyleKey.get(styleKey);@@ -53,9 +58,9 @@ export function extractTokens(}}}- for (const c of n.children ?? []) walk(c);+ for (const c of n.children ?? []) walk(c, consumerId);}- walk(doc.document);+ walk(doc.document, undefined);return { nodes, edges };}