diff --git a/src/Diagram.ts b/src/Diagram.ts index 7e6d24db6..4f1765899 100644 --- a/src/Diagram.ts +++ b/src/Diagram.ts @@ -2,6 +2,7 @@ import * as configApi from './config'; import { log } from './logger'; import { getDiagram } from './diagram-api/diagramAPI'; import { detectType } from './diagram-api/detectType'; +import { isDetailedError } from './utils'; export class Diagram { type = 'graph'; parser; @@ -42,11 +43,9 @@ export class Diagram { // Is this the correct way to access mermiad's parseError() // method ? (or global.mermaid.parseError()) ? if (parseError) { - // @ts-ignore - if (error.str != undefined) { + if (isDetailedError(error)) { // handle case where error string and hash were // wrapped in object like`const error = { str, hash };` - // @ts-ignore parseError(error.str, error.hash); } else { // assume it is just error string and pass it on diff --git a/src/config.ts b/src/config.ts index 20843ccbc..9eb688bb4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -13,11 +13,11 @@ let currentConfig: MermaidConfig = assignWithDepth({}, defaultConfig); export const updateCurrentConfig = (siteCfg: MermaidConfig, _directives: any[]) => { // start with config being the siteConfig - let cfg = assignWithDepth({}, siteCfg); + let cfg: MermaidConfig = assignWithDepth({}, siteCfg); // let sCfg = assignWithDepth(defaultConfig, siteConfigDelta); // Join directives - let sumOfDirectives = {}; + let sumOfDirectives: MermaidConfig = {}; for (let i = 0; i < _directives.length; i++) { const d = _directives[i]; sanitize(d); @@ -28,16 +28,15 @@ export const updateCurrentConfig = (siteCfg: MermaidConfig, _directives: any[]) cfg = assignWithDepth(cfg, sumOfDirectives); - // @ts-ignore - if (sumOfDirectives.theme && theme[sumOfDirectives.theme]) { + if (sumOfDirectives.theme && sumOfDirectives.theme in theme) { const tmpConfigFromInitialize = assignWithDepth({}, configFromInitialize); const themeVariables = assignWithDepth( tmpConfigFromInitialize.themeVariables || {}, - // @ts-ignore sumOfDirectives.themeVariables ); - // @ts-ignore - cfg.themeVariables = theme[cfg.theme].getThemeVariables(themeVariables); + if (cfg.theme && cfg.theme in theme) { + cfg.themeVariables = theme[cfg.theme as keyof typeof theme].getThemeVariables(themeVariables); + } } currentConfig = cfg; diff --git a/src/diagrams/common/common.ts b/src/diagrams/common/common.ts index 7421f48a2..057481768 100644 --- a/src/diagrams/common/common.ts +++ b/src/diagrams/common/common.ts @@ -147,7 +147,7 @@ export const evaluate = (val: string | boolean): boolean => export const parseGenericTypes = function (text: string): string { let cleanedText = text; - if (text.indexOf('~') != -1) { + if (text.indexOf('~') !== -1) { cleanedText = cleanedText.replace('~', '<'); cleanedText = cleanedText.replace('~', '>'); diff --git a/src/diagrams/git/gitGraphDetector.ts b/src/diagrams/git/gitGraphDetector.ts index f5a660fc1..1c0a015e7 100644 --- a/src/diagrams/git/gitGraphDetector.ts +++ b/src/diagrams/git/gitGraphDetector.ts @@ -1,5 +1,5 @@ import type { DiagramDetector } from '../../diagram-api/detectType'; export const gitGraphDetector: DiagramDetector = (txt) => { - return txt.match(/^\s*gitGraph/) != null; + return txt.match(/^\s*gitGraph/) !== null; }; diff --git a/src/diagrams/mindmap/mindamapDetector.ts b/src/diagrams/mindmap/mindamapDetector.ts index 18cf67312..42d6309ee 100644 --- a/src/diagrams/mindmap/mindamapDetector.ts +++ b/src/diagrams/mindmap/mindamapDetector.ts @@ -1,5 +1,5 @@ import { DiagramDetector } from '../../diagram-api/detectType'; export const mindmapDetector: DiagramDetector = (txt) => { - return txt.match(/^\s*mindmap/) != null; + return txt.match(/^\s*mindmap/) !== null; }; diff --git a/src/errorRenderer.ts b/src/errorRenderer.ts index 1f2b61e2e..de0b31886 100644 --- a/src/errorRenderer.ts +++ b/src/errorRenderer.ts @@ -1,6 +1,7 @@ /** Created by knut on 14-12-11. */ import { select } from 'd3'; import { log } from './logger'; +import { getErrorMessage } from './utils'; let conf = {}; @@ -89,8 +90,7 @@ export const draw = (id: string, mermaidVersion: string) => { svg.attr('viewBox', '768 0 512 512'); } catch (e) { log.error('Error while rendering info diagram'); - // @ts-ignore - log.error(e.message); + log.error(getErrorMessage(e)); } }; diff --git a/src/mermaid.ts b/src/mermaid.ts index 95cf68c27..a46103f11 100644 --- a/src/mermaid.ts +++ b/src/mermaid.ts @@ -6,6 +6,7 @@ import { MermaidConfig } from './config.type'; import { log } from './logger'; import utils from './utils'; import { mermaidAPI } from './mermaidAPI'; +import { isDetailedError } from './utils'; /** * ## init @@ -39,10 +40,10 @@ const init = function ( initThrowsErrors(config, nodes, callback); } catch (e) { log.warn('Syntax Error rendering'); - // @ts-ignore - log.warn(e.str); + if (isDetailedError(e)) { + log.warn(e.str); + } if (mermaid.parseError) { - // @ts-ignore mermaid.parseError(e); } } @@ -56,6 +57,7 @@ const initThrowsErrors = function ( const conf = mermaidAPI.getConfig(); // console.log('Starting rendering diagrams (init) - mermaid.init', conf); if (config) { + // This is a legacy way of setting config. It is not documented and should be removed in the future. // @ts-ignore mermaid.sequenceConfig = config; } diff --git a/src/utils.ts b/src/utils.ts index 30ab57ad8..27c644ff0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -933,6 +933,20 @@ export const sanitizeCss = (str) => { return str; }; +export interface DetailedError { + str: string; + hash: any; +} + +export function isDetailedError(error: unknown): error is DetailedError { + return 'str' in error; +} + +export function getErrorMessage(error: unknown): string { + if (error instanceof Error) return error.message; + return String(error); +} + export default { assignWithDepth, wrapLabel,