Repository: mermaid-js/mermaid
File: packages/mermaid/src/diagrams/treemap/parser.ts (TypeScript)

Code before:
  const hierarchyNodes = buildHierarchy(items);

  // Add all nodes to the database
  const addNodesRecursively = (nodes: TreemapNode[], level: number) => {
    for (const node of nodes) {
      db.addNode(node, level);
      if (node.children && node.children.length > 0) {
        addNodesRecursively(node.children, level + 1);
      }
    }
  };

  addNodesRecursively(hierarchyNodes, 0);
};

/**
 * Gets the name of a treemap item
 * @param item - The treemap item
 * @returns The name of the item
 */
const getItemName = (item: { name?: string | number }): string => {
  return item.name ? String(item.name) : '';
};

export const parser: ParserDefinition = {
  parser: { yy: {} as TreemapDB },
  parse: async (text: string): Promise<void> => {
    try {
      // Use a generic parse that accepts any diagram type

      const parseFunc = parse as (diagramType: string, text: string) => Promise<TreemapAst>;
      const ast = await parseFunc('treemap', text);
      log.debug('Treemap AST:', ast);
      populate(ast, parser.parser?.yy as TreemapDB);
    } catch (error) {
      log.error('Error parsing treemap:', error);
      throw error;
    }
  },
};


Review comment (suggestion):
**nitpick**: I don't like using `as XXXXXX` unless we know with 100% certainty that the type is correct.

Is there any reason why we can't just make an empty `TreemapDB` for this?

```suggestion
  parser: { yy: new TreemapDB() },
```

Or if you really want to leave it with some default value, maybe

```suggestion
  // @ts-expect-error: Will be replaced with a real DB during init
  parser: { yy: undefined },
```

Code after:
  // Convert flat structure to hierarchical
  const hierarchyNodes = buildHierarchy(items);

  // Add all nodes to the database
  const addNodesRecursively = (nodes: TreemapNode[], level: number) => {
    for (const node of nodes) {
      db.addNode(node, level);
      if (node.children && node.children.length > 0) {
        addNodesRecursively(node.children, level + 1);
      }
    }
  };

  addNodesRecursively(hierarchyNodes, 0);
};

/**
 * Gets the name of a treemap item
 * @param item - The treemap item
 * @returns The name of the item
 */
const getItemName = (item: { name?: string | number }): string => {
  return item.name ? String(item.name) : '';
};

export const parser: ParserDefinition = {
  // @ts-expect-error - TreeMapDB is not assignable to DiagramDB
  parser: { yy: undefined },
  parse: async (text: string): Promise<void> => {
    try {
      // Use a generic parse that accepts any diagram type

      const parseFunc = parse as (diagramType: string, text: string) => Promise<TreemapAst>;
      const ast = await parseFunc('treemap', text);
      log.debug('Treemap AST:', ast);
      const db = parser.parser?.yy;
      if (!(db instanceof TreeMapDB)) {
        throw new Error(
          'parser.parser?.yy was not a TreemapDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.'
        );
      }
      populate(ast, db);
    } catch (error) {
      log.error('Error parsing treemap:', error);
      throw error;
    }
  },
};