diff --git a/packages/mermaid/src/Diagram.ts b/packages/mermaid/src/Diagram.ts index 0aa741994..d6c226cde 100644 --- a/packages/mermaid/src/Diagram.ts +++ b/packages/mermaid/src/Diagram.ts @@ -2,15 +2,17 @@ import * as configApi from './config'; import { log } from './logger'; import { getDiagram, registerDiagram } from './diagram-api/diagramAPI'; import { detectType, getDiagramLoader } from './diagram-api/detectType'; -import { isDetailedError } from './utils'; +import { isDetailedError, type DetailedError } from './utils'; + +export type ParseErrorFunction = (err: string | DetailedError, hash?: any) => void; + export class Diagram { type = 'graph'; parser; renderer; db; private detectTypeFailed = false; - // eslint-disable-next-line @typescript-eslint/ban-types - constructor(public txt: string, parseError?: Function) { + constructor(public txt: string, parseError?: ParseErrorFunction) { const cnf = configApi.getConfig(); this.txt = txt; try { @@ -37,8 +39,7 @@ export class Diagram { this.parse(this.txt, parseError); } - // eslint-disable-next-line @typescript-eslint/ban-types - parse(text: string, parseError?: Function): boolean { + parse(text: string, parseError?: ParseErrorFunction): boolean { if (this.detectTypeFailed) { return false; } @@ -53,23 +54,24 @@ export class Diagram { return false; } - // eslint-disable-next-line @typescript-eslint/ban-types - handleError(error: unknown, parseError?: Function) { + handleError(error: unknown, parseError?: ParseErrorFunction) { // Is this the correct way to access mermiad's parseError() // method ? (or global.mermaid.parseError()) ? - if (parseError) { - if (isDetailedError(error)) { - // handle case where error string and hash were - // wrapped in object like`const error = { str, hash };` - parseError(error.str, error.hash); - } else { - // assume it is just error string and pass it on - parseError(error); - } - } else { + + if (parseError === undefined) { // No mermaid.parseError() handler defined, so re-throw it throw error; } + + if (isDetailedError(error)) { + // Handle case where error string and hash were + // wrapped in object like`const error = { str, hash };` + parseError(error.str, error.hash); + return; + } + + // Otherwise, assume it is just an error string and pass it on + parseError(error as string); } getParser() { @@ -81,8 +83,10 @@ export class Diagram { } } -// eslint-disable-next-line @typescript-eslint/ban-types -export const getDiagramFromText = async (txt: string, parseError?: Function): Promise => { +export const getDiagramFromText = async ( + txt: string, + parseError?: ParseErrorFunction +): Promise => { const type = detectType(txt, configApi.getConfig()); try { // Trying to find the diagram diff --git a/packages/mermaid/src/__mocks__/mermaidAPI.ts b/packages/mermaid/src/__mocks__/mermaidAPI.ts index 08c5b7eea..3102095b9 100644 --- a/packages/mermaid/src/__mocks__/mermaidAPI.ts +++ b/packages/mermaid/src/__mocks__/mermaidAPI.ts @@ -6,7 +6,7 @@ import * as configApi from '../config'; import { vi } from 'vitest'; import { addDiagrams } from '../diagram-api/diagram-orchestration'; -import Diagram from '../Diagram'; +import Diagram, { type ParseErrorFunction } from '../Diagram'; // Normally, we could just do the following to get the original `parse()` // implementation, however, requireActual returns a promise and it's not documented how to use withing mock file. @@ -15,8 +15,7 @@ import Diagram from '../Diagram'; * @param text * @param parseError */ -// eslint-disable-next-line @typescript-eslint/ban-types -function parse(text: string, parseError?: Function): boolean { +function parse(text: string, parseError?: ParseErrorFunction): boolean { addDiagrams(); const diagram = new Diagram(text, parseError); return diagram.parse(text, parseError); diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts index ae6c62547..09842c426 100644 --- a/packages/mermaid/src/mermaid.ts +++ b/packages/mermaid/src/mermaid.ts @@ -8,6 +8,7 @@ import utils from './utils'; import { mermaidAPI } from './mermaidAPI'; import { addDetector } from './diagram-api/detectType'; import { isDetailedError } from './utils'; +import type { ParseErrorFunction } from './Diagram'; /** * ## init @@ -62,7 +63,7 @@ const init = async function ( log.warn(e.str); } if (mermaid.parseError) { - mermaid.parseError(e); + mermaid.parseError(e as string); } } }; @@ -212,8 +213,7 @@ const parse = (txt: string) => { const mermaid: { startOnLoad: boolean; diagrams: any; - // eslint-disable-next-line @typescript-eslint/ban-types - parseError?: Function; + parseError?: ParseErrorFunction; mermaidAPI: typeof mermaidAPI; parse: typeof parse; render: typeof mermaidAPI.render; diff --git a/packages/mermaid/src/mermaidAPI.ts b/packages/mermaid/src/mermaidAPI.ts index 7c967e5fd..7fe5064de 100644 --- a/packages/mermaid/src/mermaidAPI.ts +++ b/packages/mermaid/src/mermaidAPI.ts @@ -22,7 +22,7 @@ import classDb from './diagrams/class/classDb'; import flowDb from './diagrams/flowchart/flowDb'; import flowRenderer from './diagrams/flowchart/flowRenderer'; import ganttDb from './diagrams/gantt/ganttDb'; -import Diagram, { getDiagramFromText } from './Diagram'; +import Diagram, { getDiagramFromText, type ParseErrorFunction } from './Diagram'; import errorRenderer from './diagrams/error/errorRenderer'; import { attachFunctions } from './interactionDb'; import { log, setLogLevel } from './logger'; @@ -37,8 +37,7 @@ import { evaluate } from './diagrams/common/common'; * @param text * @param parseError */ -// eslint-disable-next-line @typescript-eslint/ban-types -function parse(text: string, parseError?: Function): boolean { +function parse(text: string, parseError?: ParseErrorFunction): boolean { addDiagrams(); const diagram = new Diagram(text, parseError); return diagram.parse(text, parseError);