From 953d288957bf234927c4eca8ff7ce94cf540d172 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Wed, 30 Oct 2024 18:52:09 +0900 Subject: [PATCH] refactor(types): fix kanbanRenderer types Add the appropriate type checks to ensure that we're only ever calling: - `insertCluster()` with `ClusterNode` - `insertNode()` with a `NonClusterNode` Fixes: 7401cb8f6aec0c7dccae820824eb2bb3e6a3e12d --- .../mermaid/src/diagrams/kanban/kanbanRenderer.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/kanban/kanbanRenderer.ts b/packages/mermaid/src/diagrams/kanban/kanbanRenderer.ts index ba28eae7ba..dc57808a9c 100644 --- a/packages/mermaid/src/diagrams/kanban/kanbanRenderer.ts +++ b/packages/mermaid/src/diagrams/kanban/kanbanRenderer.ts @@ -7,6 +7,7 @@ import type { KanbanDB } from './kanbanTypes.js'; import defaultConfig from '../../defaultConfig.js'; import { insertCluster } from '../../rendering-util/rendering-elements/clusters.js'; import { insertNode, positionNode } from '../../rendering-util/rendering-elements/nodes.js'; +import type { ClusterNode } from '../../rendering-util/types.js'; export const draw: DrawDefinition = async (text, id, _version, diagObj) => { log.debug('Rendering kanban diagram\n' + text); @@ -26,7 +27,10 @@ export const draw: DrawDefinition = async (text, id, _version, diagObj) => { sectionsElem.attr('class', 'sections'); const nodesElem = svg.append('g'); nodesElem.attr('class', 'items'); - const sections = data4Layout.nodes.filter((node) => node.isGroup); + const sections = data4Layout.nodes.filter( + // TODO: TypeScript 5.5 will infer this predicate automatically + (node): node is typeof node & ClusterNode => node.isGroup + ); let cnt = 0; // TODO set padding const padding = 10; @@ -60,6 +64,11 @@ export const draw: DrawDefinition = async (text, id, _version, diagObj) => { let y = top; const sectionItems = data4Layout.nodes.filter((node) => node.parentId === section.id); for (const item of sectionItems) { + if (item.isGroup) { + // Kanban diagrams should not have groups within groups + // this should never happen + throw new Error('Groups within groups are not allowed in Kanban diagrams'); + } item.x = section.x; item.width = WIDTH - 1.5 * padding; const nodeEl = await insertNode(nodesElem, item, { config: conf });