feat: make parseError function more type-safe

This commit is contained in:
Basti Ortiz
2022-10-12 00:21:52 +08:00
parent 9660b0e9fb
commit 15f31f9d95

View File

@@ -3,14 +3,16 @@ import { log } from './logger';
import { getDiagram, registerDiagram } from './diagram-api/diagramAPI'; import { getDiagram, registerDiagram } from './diagram-api/diagramAPI';
import { detectType, getDiagramLoader } from './diagram-api/detectType'; import { detectType, getDiagramLoader } from './diagram-api/detectType';
import { isDetailedError } from './utils'; import { isDetailedError } from './utils';
export type ParseErrorFunction = (str: string, hash?: any) => void;
export class Diagram { export class Diagram {
type = 'graph'; type = 'graph';
parser; parser;
renderer; renderer;
db; db;
private detectTypeFailed = false; private detectTypeFailed = false;
// eslint-disable-next-line @typescript-eslint/ban-types constructor(public txt: string, parseError?: ParseErrorFunction) {
constructor(public txt: string, parseError?: Function) {
const cnf = configApi.getConfig(); const cnf = configApi.getConfig();
this.txt = txt; this.txt = txt;
try { try {
@@ -37,8 +39,7 @@ export class Diagram {
this.parse(this.txt, parseError); this.parse(this.txt, parseError);
} }
// eslint-disable-next-line @typescript-eslint/ban-types parse(text: string, parseError?: ParseErrorFunction): boolean {
parse(text: string, parseError?: Function): boolean {
if (this.detectTypeFailed) { if (this.detectTypeFailed) {
return false; return false;
} }
@@ -53,23 +54,24 @@ export class Diagram {
return false; return false;
} }
// eslint-disable-next-line @typescript-eslint/ban-types handleError(error: unknown, parseError?: ParseErrorFunction) {
handleError(error: unknown, parseError?: Function) {
// Is this the correct way to access mermiad's parseError() // Is this the correct way to access mermiad's parseError()
// method ? (or global.mermaid.parseError()) ? // method ? (or global.mermaid.parseError()) ?
if (parseError) {
if (isDetailedError(error)) { if (parseError === undefined) {
// 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 {
// No mermaid.parseError() handler defined, so re-throw it // No mermaid.parseError() handler defined, so re-throw it
throw error; 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() { getParser() {
@@ -81,8 +83,10 @@ export class Diagram {
} }
} }
// eslint-disable-next-line @typescript-eslint/ban-types export const getDiagramFromText = async (
export const getDiagramFromText = async (txt: string, parseError?: Function): Promise<Diagram> => { txt: string,
parseError?: ParseErrorFunction
): Promise<Diagram> => {
const type = detectType(txt, configApi.getConfig()); const type = detectType(txt, configApi.getConfig());
try { try {
// Trying to find the diagram // Trying to find the diagram