Skip to content

Commit

Permalink
Merge pull request #3393 from mermaid-js/sidv/typescript
Browse files Browse the repository at this point in the history
Introduce stricter typescript linting
  • Loading branch information
knsv committed Sep 9, 2022
2 parents d78adc6 + 5a1e3ed commit f63acea
Show file tree
Hide file tree
Showing 25 changed files with 131 additions and 93 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
@@ -1,3 +1,5 @@
dist/**
.github/**
docs/Setup.md
cypress.config.js
cypress/plugins/index.js
11 changes: 11 additions & 0 deletions .eslintrc.json
Expand Up @@ -15,6 +15,7 @@
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jsdoc/recommended",
"plugin:json/recommended",
"plugin:markdown/recommended",
Expand All @@ -36,6 +37,16 @@
"jsdoc/require-returns": "off",
"jsdoc/require-returns-description": "off",
"cypress/no-async-tests": "off",
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-expect-error": "allow-with-description",
"ts-ignore": "allow-with-description",
"ts-nocheck": "allow-with-description",
"ts-check": "allow-with-description",
"minimumDescriptionLength": 10
}
],
"json/*": ["error", "allowComments"],
"no-empty": ["error", { "allowEmptyCatch": true }]
},
Expand Down
4 changes: 3 additions & 1 deletion demos/index.html
Expand Up @@ -1083,7 +1083,9 @@ <h1 id="link-clicked">Anchor for "link-clicked" test</h1>
<script>
const testLineEndings = (test, input) => {
try {
mermaid.render(test, input, () => {});
mermaid.render(test, input, () => {
//no-op
});
} catch (err) {
console.error('Error in %s:\n\n%s', test, err);
}
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
Expand Down
2 changes: 2 additions & 0 deletions src/Diagram.ts
Expand Up @@ -8,6 +8,7 @@ export class Diagram {
parser;
renderer;
db;
// eslint-disable-next-line @typescript-eslint/ban-types
constructor(public txt: string, parseError?: Function) {
const cnf = configApi.getConfig();
this.txt = txt;
Expand All @@ -33,6 +34,7 @@ export class Diagram {
this.parse(this.txt, parseError);
}

// eslint-disable-next-line @typescript-eslint/ban-types
parse(text: string, parseError?: Function): boolean {
try {
text = text + '\n';
Expand Down
1 change: 1 addition & 0 deletions src/__mocks__/mermaidAPI.ts
Expand Up @@ -19,6 +19,7 @@ let hasLoadedDiagrams = false;
* @param text
* @param parseError
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function parse(text: string, parseError?: Function): boolean {
if (!hasLoadedDiagrams) {
addDiagrams();
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Expand Up @@ -62,9 +62,9 @@ export const setSiteConfig = (conf: MermaidConfig): MermaidConfig => {
siteConfig = assignWithDepth({}, defaultConfig);
siteConfig = assignWithDepth(siteConfig, conf);

// @ts-ignore
// @ts-ignore: TODO Fix ts errors
if (conf.theme && theme[conf.theme]) {
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
siteConfig.themeVariables = theme[conf.theme].getThemeVariables(conf.themeVariables);
}

Expand Down Expand Up @@ -216,6 +216,8 @@ export const addDirective = (directive: any) => {
* | conf | base set of values, which currentConfig could be **reset** to. | Dictionary | Required | Any Values, with respect to the secure Array |
*
* **Notes**: (default: current siteConfig ) (optional, default `getSiteConfig()`)
*
* @param config
*/
export const reset = (config = siteConfig): void => {
// Replace current config with siteConfig
Expand Down
3 changes: 2 additions & 1 deletion src/config.type.ts
Expand Up @@ -219,7 +219,8 @@ export interface MindmapDiagramConfig extends BaseDiagramConfig {
padding: number;
maxNodeWidth: number;
}
export interface PieDiagramConfig extends BaseDiagramConfig {}

export type PieDiagramConfig = BaseDiagramConfig;

export interface ErDiagramConfig extends BaseDiagramConfig {
diagramPadding?: number;
Expand Down
4 changes: 2 additions & 2 deletions src/diagram-api/diagram-orchestration.ts
@@ -1,14 +1,14 @@
import { registerDiagram } from './diagramAPI';
import * as mindmapDb from '../diagrams/mindmap/mindmapDb';
import mindmapRenderer from '../diagrams/mindmap/mindmapRenderer';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import mindmapParser from '../diagrams/mindmap/parser/mindmap';
import { mindmapDetector } from '../diagrams/mindmap/mindmapDetector';
import mindmapStyles from '../diagrams/mindmap/styles';

import gitGraphDb from '../diagrams/git/gitGraphAst';
import gitGraphRenderer from '../diagrams/git/gitGraphRenderer';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import gitGraphParser from '../diagrams/git/parser/gitGraph';
import { gitGraphDetector } from '../diagrams/git/gitGraphDetector';
import gitGraphStyles from '../diagrams/git/styles';
Expand Down
22 changes: 11 additions & 11 deletions src/diagram-api/diagramAPI.ts
@@ -1,52 +1,52 @@
import c4Db from '../diagrams/c4/c4Db';
import c4Renderer from '../diagrams/c4/c4Renderer';
import c4Styles from '../diagrams/c4/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import c4Parser from '../diagrams/c4/parser/c4Diagram';
import classDb from '../diagrams/class/classDb';
import classRenderer from '../diagrams/class/classRenderer';
import classRendererV2 from '../diagrams/class/classRenderer-v2';
import classStyles from '../diagrams/class/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import classParser from '../diagrams/class/parser/classDiagram';
import erDb from '../diagrams/er/erDb';
import erRenderer from '../diagrams/er/erRenderer';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import erParser from '../diagrams/er/parser/erDiagram';
import erStyles from '../diagrams/er/styles';
import flowDb from '../diagrams/flowchart/flowDb';
import flowRenderer from '../diagrams/flowchart/flowRenderer';
import flowRendererV2 from '../diagrams/flowchart/flowRenderer-v2';
import flowStyles from '../diagrams/flowchart/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import flowParser from '../diagrams/flowchart/parser/flow';
import ganttDb from '../diagrams/gantt/ganttDb';
import ganttRenderer from '../diagrams/gantt/ganttRenderer';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import ganttParser from '../diagrams/gantt/parser/gantt';
import ganttStyles from '../diagrams/gantt/styles';

import infoDb from '../diagrams/info/infoDb';
import infoRenderer from '../diagrams/info/infoRenderer';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import infoParser from '../diagrams/info/parser/info';
import infoStyles from '../diagrams/info/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import pieParser from '../diagrams/pie/parser/pie';
import pieDb from '../diagrams/pie/pieDb';
import pieRenderer from '../diagrams/pie/pieRenderer';
import pieStyles from '../diagrams/pie/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import requirementParser from '../diagrams/requirement/parser/requirementDiagram';
import requirementDb from '../diagrams/requirement/requirementDb';
import requirementRenderer from '../diagrams/requirement/requirementRenderer';
import requirementStyles from '../diagrams/requirement/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import sequenceParser from '../diagrams/sequence/parser/sequenceDiagram';
import sequenceDb from '../diagrams/sequence/sequenceDb';
import sequenceRenderer from '../diagrams/sequence/sequenceRenderer';
import sequenceStyles from '../diagrams/sequence/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import stateParser from '../diagrams/state/parser/stateDiagram';
import stateDb from '../diagrams/state/stateDb';
import stateRenderer from '../diagrams/state/stateRenderer';
Expand All @@ -55,7 +55,7 @@ import stateStyles from '../diagrams/state/styles';
import journeyDb from '../diagrams/user-journey/journeyDb';
import journeyRenderer from '../diagrams/user-journey/journeyRenderer';
import journeyStyles from '../diagrams/user-journey/styles';
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
import journeyParser from '../diagrams/user-journey/parser/journey';
import { addDetector, DiagramDetector } from './detectType';
import { log as _log } from '../logger';
Expand Down
1 change: 1 addition & 0 deletions src/diagrams/class/classDiagramGrammar.spec.js
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');

import { LALRGenerator } from 'jison';
Expand Down
4 changes: 3 additions & 1 deletion src/diagrams/git/mockDb.js
Expand Up @@ -173,7 +173,9 @@ export const getCommits = () => {
},
};
};
export const clear = () => {};
export const clear = () => {
//no-op
};
export const getBranchesAsObjArray = () => [
{
name: 'master',
Expand Down
10 changes: 6 additions & 4 deletions src/diagrams/info/info.spec.js
@@ -1,12 +1,14 @@
import { parser } from './parser/info';
import infoDb from './infoDb';
describe('when parsing an info graph it', function () {
var ex;
let ex;
beforeEach(function () {
ex = require('./parser/info').parser;
ex.yy = require('./infoDb');
ex = parser;
ex.yy = infoDb;
});

it('should handle an info definition', function () {
var str = `info
let str = `info
showInfo`;

ex.parse(str);
Expand Down
5 changes: 2 additions & 3 deletions src/diagrams/mindmap/mindmap.spec.js
@@ -1,11 +1,10 @@
import { parser as mindmap } from './parser/mindmap';
import * as mindmapDB from './mindmapDb';
import { setLogLevel } from '../../logger';

describe('when parsing a mindmap ', function () {
let mindmap;
beforeEach(function () {
mindmap = require('./parser/mindmap').parser;
mindmap.yy = require('./mindmapDb');
mindmap.yy = mindmapDB;
mindmap.yy.clear();
setLogLevel('trace');
});
Expand Down
3 changes: 2 additions & 1 deletion src/diagrams/sequence/sequenceRenderer.js
Expand Up @@ -96,6 +96,7 @@ export const bounds = {
}
},
updateBounds: function (startx, starty, stopx, stopy) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const _self = this;
let cnt = 0;
/** @param {any} type */
Expand Down Expand Up @@ -653,7 +654,7 @@ export const draw = function (_text, id, _version, diagObj) {
// Draw the messages/signals
let sequenceIndex = 1;
let sequenceIndexStep = 1;
let messagesToDraw = Array();
let messagesToDraw = [];
messages.forEach(function (msg) {
let loopModel, noteModel, msgModel;

Expand Down
4 changes: 2 additions & 2 deletions src/diagrams/sequence/svgDraw.spec.js
@@ -1,5 +1,5 @@
const svgDraw = require('./svgDraw').default;
const { MockD3 } = require('d3');
import svgDraw from './svgDraw';
import { MockD3 } from 'd3';

describe('svgDraw', function () {
describe('drawRect', function () {
Expand Down
4 changes: 3 additions & 1 deletion src/diagrams/state/stateRenderer.js
Expand Up @@ -13,7 +13,9 @@ let conf;

const transformationLog = {};

export const setConf = function () {};
export const setConf = function () {
//no-op
};

/**
* Setup arrow head and define the marker. The result is appended to the svg.
Expand Down
1 change: 1 addition & 0 deletions src/diagrams/user-journey/journeyRenderer.js
Expand Up @@ -151,6 +151,7 @@ export const bounds = {
},
updateBounds: function (startx, starty, stopx, stopy) {
const conf = getConfig().journey;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const _self = this;
let cnt = 0;
/** @param {any} type */
Expand Down
4 changes: 2 additions & 2 deletions src/interactionDb.ts
@@ -1,5 +1,5 @@
let interactionFunctions: (() => {})[] = [];
export const addFunction = (func: () => {}) => {
let interactionFunctions: (() => void)[] = [];
export const addFunction = (func: () => void) => {
interactionFunctions.push(func);
};
export const attachFunctions = () => {
Expand Down
1 change: 1 addition & 0 deletions src/jison/transformer.js
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Generator } = require('jison');

module.exports = {
Expand Down
3 changes: 3 additions & 0 deletions src/logger.ts
@@ -1,3 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
import moment from 'moment-mini';

export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
Expand Down
13 changes: 9 additions & 4 deletions src/mermaid.ts
Expand Up @@ -14,7 +14,8 @@ import { isDetailedError } from './utils';
* Function that goes through the document to find the chart definitions in there and render them.
*
* The function tags the processed attributes with the attribute data-processed and ignores found
* elements with the attribute already set. This way the init function can be triggered several times.
* elements with the attribute already set. This way the init function can be triggered several
* times.
*
* Optionally, `init` can accept in the second argument one of the following:
*
Expand All @@ -39,6 +40,7 @@ const init = function (
config?: MermaidConfig,
// eslint-disable-next-line no-undef
nodes?: string | HTMLElement | NodeListOf<HTMLElement>,
// eslint-disable-next-line @typescript-eslint/ban-types
callback?: Function
) {
try {
Expand All @@ -58,13 +60,14 @@ const initThrowsErrors = function (
config?: MermaidConfig,
// eslint-disable-next-line no-undef
nodes?: string | HTMLElement | NodeListOf<HTMLElement>,
// eslint-disable-next-line @typescript-eslint/ban-types
callback?: 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
// @ts-ignore: TODO Fix ts errors
mermaid.sequenceConfig = config;
}

Expand Down Expand Up @@ -131,7 +134,7 @@ const initThrowsErrors = function (
);
} catch (error) {
log.warn('Catching Error (bootstrap)', error);
// @ts-ignore
// @ts-ignore: TODO Fix ts errors
// TODO: We should be throwing an error object.
throw { error, message: error.str };
}
Expand All @@ -144,7 +147,8 @@ const initialize = function (config: MermaidConfig) {

/**
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the page.
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the
* page.
*/
const contentLoaded = function () {
if (mermaid.startOnLoad) {
Expand Down Expand Up @@ -187,6 +191,7 @@ const parse = (txt: string) => {
const mermaid: {
startOnLoad: boolean;
diagrams: any;
// eslint-disable-next-line @typescript-eslint/ban-types
parseError?: Function;
mermaidAPI: typeof mermaidAPI;
parse: typeof parse;
Expand Down

0 comments on commit f63acea

Please sign in to comment.