diff --git a/packages/mermaid/src/diagrams/treemap/parser.ts b/packages/mermaid/src/diagrams/treemap/parser.ts index ad9d748d8..64a5855db 100644 --- a/packages/mermaid/src/diagrams/treemap/parser.ts +++ b/packages/mermaid/src/diagrams/treemap/parser.ts @@ -4,6 +4,7 @@ import { log } from '../../logger.js'; import { populateCommonDb } from '../common/populateCommonDb.js'; import type { TreemapNode, TreemapAst, TreemapDB } from './types.js'; import { buildHierarchy } from './utils.js'; +import { TreeMapDB } from './db.js'; /** * Populates the database with data from the Treemap AST @@ -83,7 +84,7 @@ const getItemName = (item: { name?: string | number }): string => { }; export const parser: ParserDefinition = { - parser: { yy: {} as TreemapDB }, + parser: { yy: new TreeMapDB() }, parse: async (text: string): Promise => { try { // Use a generic parse that accepts any diagram type @@ -91,7 +92,13 @@ export const parser: ParserDefinition = { const parseFunc = parse as (diagramType: string, text: string) => Promise; const ast = await parseFunc('treemap', text); log.debug('Treemap AST:', ast); - populate(ast, parser.parser?.yy as TreemapDB); + 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;