Skip to content

Commit a5cc1e8

Browse files
committed
mermaid-js#1031 Adding stricter code checks
1 parent aac5197 commit a5cc1e8

16 files changed

+91
-65
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.spec.js

.eslintrc.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"env": {
33
"browser": true,
4-
"es6": true
4+
"es6": true,
5+
"node": true
56
},
67
"parserOptions": {
78
"ecmaFeatures": {
@@ -10,7 +11,7 @@
1011
},
1112
"sourceType": "module"
1213
},
13-
"extends": ["prettier"],
14+
"extends": ["prettier", "eslint:recommended"],
1415
"plugins": ["prettier"],
1516
"rules": {
1617
"prettier/prettier": ["error"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-env jest */
2+
import { imgSnapshotTest } from '../../helpers/util';
3+
4+
describe('State diagram', () => {
5+
it('should render a flowchart full of circles', () => {
6+
imgSnapshotTest(
7+
`
8+
graph LR
9+
47(SAM.CommonFA.FMESummary)-->48(SAM.CommonFA.CommonFAFinanceBudget)
10+
37(SAM.CommonFA.BudgetSubserviceLineVolume)-->48(SAM.CommonFA.CommonFAFinanceBudget)
11+
35(SAM.CommonFA.PopulationFME)-->47(SAM.CommonFA.FMESummary)
12+
41(SAM.CommonFA.MetricCost)-->47(SAM.CommonFA.FMESummary)
13+
44(SAM.CommonFA.MetricOutliers)-->47(SAM.CommonFA.FMESummary)
14+
46(SAM.CommonFA.MetricOpportunity)-->47(SAM.CommonFA.FMESummary)
15+
40(SAM.CommonFA.OPVisits)-->47(SAM.CommonFA.FMESummary)
16+
38(SAM.CommonFA.CommonFAFinanceRefund)-->47(SAM.CommonFA.FMESummary)
17+
43(SAM.CommonFA.CommonFAFinancePicuDays)-->47(SAM.CommonFA.FMESummary)
18+
42(SAM.CommonFA.CommonFAFinanceNurseryDays)-->47(SAM.CommonFA.FMESummary)
19+
45(SAM.CommonFA.MetricPreOpportunity)-->46(SAM.CommonFA.MetricOpportunity)
20+
35(SAM.CommonFA.PopulationFME)-->45(SAM.CommonFA.MetricPreOpportunity)
21+
41(SAM.CommonFA.MetricCost)-->45(SAM.CommonFA.MetricPreOpportunity)
22+
41(SAM.CommonFA.MetricCost)-->44(SAM.CommonFA.MetricOutliers)
23+
39(SAM.CommonFA.ChargeDetails)-->43(SAM.CommonFA.CommonFAFinancePicuDays)
24+
39(SAM.CommonFA.ChargeDetails)-->42(SAM.CommonFA.CommonFAFinanceNurseryDays)
25+
39(SAM.CommonFA.ChargeDetails)-->41(SAM.CommonFA.MetricCost)
26+
39(SAM.CommonFA.ChargeDetails)-->40(SAM.CommonFA.OPVisits)
27+
35(SAM.CommonFA.PopulationFME)-->39(SAM.CommonFA.ChargeDetails)
28+
36(SAM.CommonFA.PremetricCost)-->39(SAM.CommonFA.ChargeDetails)
29+
`,
30+
{}
31+
);
32+
});
33+
});

src/diagrams/class/classRenderer.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,12 @@ const drawEdge = function(elem, path, relation) {
204204
x = labalPosition.x;
205205
y = labalPosition.y;
206206

207-
let p1_card_x,
208-
p1_card_y,
209-
p1_card_padd_x = conf.padding * 2,
210-
p1_card_padd_y = conf.padding;
211-
let p2_card_x,
212-
p2_card_y,
213-
p2_card_padd_x = conf.padding * 2,
214-
p2_card_padd_y = -conf.padding / 2;
207+
let p1_card_x, p1_card_y;
208+
// p1_card_padd_x = conf.padding * 2,
209+
// p1_card_padd_y = conf.padding;
210+
let p2_card_x, p2_card_y;
211+
// p2_card_padd_x = conf.padding * 2,
212+
// p2_card_padd_y = -conf.padding / 2;
215213
if (l % 2 !== 0 && l > 1) {
216214
let cardinality_1_point = utils.calcCardinalityPosition(
217215
relation.relation.type1 !== 'none',
@@ -258,8 +256,7 @@ const drawEdge = function(elem, path, relation) {
258256
logger.info('Rendering relation ' + JSON.stringify(relation));
259257
if (typeof relation.relationTitle1 !== 'undefined' && relation.relationTitle1 !== 'none') {
260258
const g = elem.append('g').attr('class', 'cardinality');
261-
const label = g
262-
.append('text')
259+
g.append('text')
263260
.attr('class', 'type1')
264261
.attr('x', p1_card_x)
265262
.attr('y', p1_card_y)
@@ -269,8 +266,7 @@ const drawEdge = function(elem, path, relation) {
269266
}
270267
if (typeof relation.relationTitle2 !== 'undefined' && relation.relationTitle2 !== 'none') {
271268
const g = elem.append('g').attr('class', 'cardinality');
272-
const label = g
273-
.append('text')
269+
g.append('text')
274270
.attr('class', 'type2')
275271
.attr('x', p2_card_x)
276272
.attr('y', p2_card_y)

src/diagrams/flowchart/flowDb.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const setClickFun = function(_id, functionName) {
223223
return;
224224
}
225225
if (typeof vertices[id] !== 'undefined') {
226-
funs.push(function(element) {
226+
funs.push(function() {
227227
const elem = document.querySelector(`[id="${id}"]`);
228228
if (elem !== null) {
229229
elem.addEventListener(
@@ -395,7 +395,7 @@ export const addSubGraph = function(_id, list, _title) {
395395
return false;
396396
}
397397
if (type in prims) {
398-
return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);
398+
return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true); // eslint-disable-line
399399
} else {
400400
return objs.indexOf(item) >= 0 ? false : objs.push(item);
401401
}

src/diagrams/flowchart/flowRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export const draw = function(text, id) {
376376
};
377377

378378
// Override normal arrowhead defined in d3. Remove style & add class to allow css styling.
379-
render.arrows().normal = function normal(parent, id, edge, type) {
379+
render.arrows().normal = function normal(parent, id) {
380380
const marker = parent
381381
.append('marker')
382382
.attr('id', id)

src/diagrams/gantt/ganttDb.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,11 @@ const compileTasks = function() {
387387
const task = rawTasks[pos];
388388
let startTime = '';
389389
switch (rawTasks[pos].raw.startTime.type) {
390-
case 'prevTaskEnd':
390+
case 'prevTaskEnd': {
391391
const prevTask = findTaskById(task.prevTaskId);
392392
task.startTime = prevTask.endTime;
393393
break;
394+
}
394395
case 'getStartDate':
395396
startTime = getStartDate(undefined, dateFormat, rawTasks[pos].raw.startTime.startData);
396397
if (startTime) {
@@ -501,7 +502,7 @@ const setClickFun = function(id, functionName, functionArgs) {
501502
* @param callbackFunction A function to be executed when clicked on the task or the task's text
502503
*/
503504
const pushFun = function(id, callbackFunction) {
504-
funs.push(function(element) {
505+
funs.push(function() {
505506
// const elem = d3.select(element).select(`[id="${id}"]`)
506507
const elem = document.querySelector(`[id="${id}"]`);
507508
if (elem !== null) {
@@ -510,7 +511,7 @@ const pushFun = function(id, callbackFunction) {
510511
});
511512
}
512513
});
513-
funs.push(function(element) {
514+
funs.push(function() {
514515
// const elem = d3.select(element).select(`[id="${id}-text"]`)
515516
const elem = document.querySelector(`[id="${id}-text"]`);
516517
if (elem !== null) {

src/diagrams/gantt/ganttRenderer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const draw = function(text, id) {
102102
drawToday(leftPadding, topPadding, pageWidth, pageHeight);
103103
}
104104

105-
function drawRects(theArray, theGap, theTopPad, theSidePad, theBarHeight, theColorScale, w, h) {
105+
function drawRects(theArray, theGap, theTopPad, theSidePad, theBarHeight, theColorScale, w) {
106106
// Draw background rects covering the entire width of the graph, these form the section rows.
107107
svg
108108
.append('g')
@@ -401,7 +401,7 @@ export const draw = function(text, id) {
401401
const hash = {};
402402
const result = [];
403403
for (let i = 0, l = arr.length; i < l; ++i) {
404-
if (!hash.hasOwnProperty(arr[i])) {
404+
if (!hash.hasOwnProperty(arr[i])) { // eslint-disable-line
405405
// it works with objects! in FF, at least
406406
hash[arr[i]] = true;
407407
result.push(arr[i]);

src/diagrams/pie/pieRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const setConf = function(cnf) {
2121
* @param id
2222
*/
2323
let w;
24-
export const draw = (txt, id, ver) => {
24+
export const draw = (txt, id) => {
2525
try {
2626
const parser = pieParser.parser;
2727
parser.yy = pieData;

src/diagrams/sequence/sequenceRenderer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,12 @@ export const draw = function(text, id) {
514514
bounds.newLoop(undefined, msg.message);
515515
bounds.bumpVerticalPos(conf.boxMargin);
516516
break;
517-
case parser.yy.LINETYPE.RECT_END:
517+
case parser.yy.LINETYPE.RECT_END: {
518518
const rectData = bounds.endLoop();
519519
svgDraw.drawBackgroundRect(diagram, rectData);
520520
bounds.bumpVerticalPos(conf.boxMargin);
521521
break;
522+
}
522523
case parser.yy.LINETYPE.OPT_START:
523524
bounds.bumpVerticalPos(conf.boxMargin);
524525
bounds.newLoop(msg.message);

src/diagrams/sequence/svgDraw.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const drawRect = function(elem, rectData) {
1616
return rectElem;
1717
};
1818

19-
export const drawText = function(elem, textData, width) {
19+
export const drawText = function(elem, textData) {
2020
// Remove and ignore br:s
2121
const nText = textData.text.replace(/<br\/?>/gi, ' ');
2222

@@ -374,7 +374,7 @@ const _drawTextCandidateFunc = (function() {
374374

375375
function _setTextAttrs(toText, fromTextAttrsDict) {
376376
for (const key in fromTextAttrsDict) {
377-
if (fromTextAttrsDict.hasOwnProperty(key)) {
377+
if (fromTextAttrsDict.hasOwnProperty(key)) { // eslint-disable-line
378378
toText.attr(key, fromTextAttrsDict[key]);
379379
}
380380
}

src/diagrams/state/shapes.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as d3 from 'd3';
22
import idCache from './id-cache.js';
33
import stateDb from './stateDb';
44
import utils from '../../utils';
5-
import { getConfig, conf } from '../../config';
5+
import { getConfig } from '../../config';
66

77
// let conf;
88

@@ -131,15 +131,15 @@ export const drawDescrState = (g, stateDef) => {
131131
*/
132132
export const addIdAndBox = (g, stateDef) => {
133133
// TODO Move hardcodings to conf
134-
const addTspan = function(textEl, txt, isFirst) {
135-
const tSpan = textEl
136-
.append('tspan')
137-
.attr('x', 2 * getConfig().state.padding)
138-
.text(txt);
139-
if (!isFirst) {
140-
tSpan.attr('dy', getConfig().state.textHeight);
141-
}
142-
};
134+
// const addTspan = function(textEl, txt, isFirst) {
135+
// const tSpan = textEl
136+
// .append('tspan')
137+
// .attr('x', 2 * getConfig().state.padding)
138+
// .text(txt);
139+
// if (!isFirst) {
140+
// tSpan.attr('dy', getConfig().state.textHeight);
141+
// }
142+
// };
143143
const title = g
144144
.append('text')
145145
.attr('x', 2 * getConfig().state.padding)
@@ -148,7 +148,7 @@ export const addIdAndBox = (g, stateDef) => {
148148
.attr('class', 'state-title')
149149
.text(stateDef.id);
150150

151-
const titleHeight = title.node().getBBox().height;
151+
const titleBox = title.node().getBBox();
152152

153153
const lineY = 1 - getConfig().state.textHeight;
154154
const descrLine = g
@@ -159,7 +159,7 @@ export const addIdAndBox = (g, stateDef) => {
159159
.attr('class', 'descr-divider');
160160

161161
const graphBox = g.node().getBBox();
162-
title.attr('x', graphBox.width / 2 - title.node().getBBox().width / 2);
162+
title.attr('x', graphBox.width / 2 - titleBox.width / 2);
163163
descrLine.attr('x2', graphBox.width + getConfig().state.padding);
164164

165165
// White color
@@ -241,7 +241,7 @@ const drawForkJoinState = (g, stateDef) => {
241241
.attr('y', getConfig().state.padding);
242242
};
243243

244-
export const drawText = function(elem, textData, width) {
244+
export const drawText = function(elem, textData) {
245245
// Remove and ignore br:s
246246
const nText = textData.text.replace(/<br\/?>/gi, ' ');
247247

@@ -264,7 +264,7 @@ export const drawText = function(elem, textData, width) {
264264

265265
const _drawLongText = (_text, x, y, g) => {
266266
let textHeight = 0;
267-
let textWidth = 0;
267+
268268
const textElem = g.append('text');
269269
textElem.style('text-anchor', 'start');
270270
textElem.attr('class', 'noteText');
@@ -317,8 +317,7 @@ export const drawNote = (text, g) => {
317317
* @param {*} stateDef
318318
*/
319319

320-
let cnt = 0;
321-
export const drawState = function(elem, stateDef, graph, doc) {
320+
export const drawState = function(elem, stateDef) {
322321
const id = stateDef.id;
323322
const stateInfo = {
324323
id: id,

src/diagrams/state/stateDb.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const setRootDoc = o => {
99
const getRootDoc = () => rootDoc;
1010

1111
const extract = doc => {
12-
const res = { states: [], relations: [] };
12+
// const res = { states: [], relations: [] };
1313
clear();
1414

1515
doc.forEach(item => {
@@ -37,8 +37,8 @@ let documents = {
3737
let currentDocument = documents.root;
3838

3939
let startCnt = 0;
40-
let endCnt = 0;
41-
let stateCnt = 0;
40+
let endCnt = 0; // eslint-disable-line
41+
// let stateCnt = 0;
4242

4343
/**
4444
* Function called by parser when a node definition has been found.

src/diagrams/state/stateRenderer.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import graphlib from 'graphlib';
44
import { logger } from '../../logger';
55
import stateDb from './stateDb';
66
import { parser } from './parser/stateDiagram';
7-
import utils from '../../utils';
8-
import idCache from './id-cache';
9-
import { drawState, addIdAndBox, drawEdge, drawNote } from './shapes';
7+
// import idCache from './id-cache';
8+
import { drawState, addIdAndBox, drawEdge } from './shapes';
109
import { getConfig } from '../../config';
1110

1211
parser.yy = stateDb;
1312

14-
let total = 0;
15-
1613
// TODO Move conf object to main conf in mermaidAPI
1714
let conf;
1815
// {
@@ -28,20 +25,20 @@ let conf;
2825

2926
const transformationLog = {};
3027

31-
export const setConf = function(cnf) {};
28+
export const setConf = function() {};
3229

3330
// Todo optimize
34-
const getGraphId = function(label) {
35-
const keys = idCache.keys();
31+
// const getGraphId = function(label) {
32+
// const keys = idCache.keys();
3633

37-
for (let i = 0; i < keys.length; i++) {
38-
if (idCache.get(keys[i]).label === label) {
39-
return keys[i];
40-
}
41-
}
34+
// for (let i = 0; i < keys.length; i++) {
35+
// if (idCache.get(keys[i]).label === label) {
36+
// return keys[i];
37+
// }
38+
// }
4239

43-
return undefined;
44-
};
40+
// return undefined;
41+
// };
4542

4643
/**
4744
* Setup arrow head and define the marker. The result is appended to the svg.
@@ -90,7 +87,7 @@ export const draw = function(text, id) {
9087
});
9188

9289
const rootDoc = stateDb.getRootDoc();
93-
const n = renderDoc(rootDoc, diagram);
90+
renderDoc(rootDoc, diagram);
9491

9592
const bounds = diagram.node().getBBox();
9693

@@ -129,7 +126,6 @@ const renderDoc = (doc, diagram, parentId) => {
129126
// multigraph: false,
130127
compound: true,
131128
// acyclicer: 'greedy',
132-
rankdir: 'LR',
133129
ranker: 'tight-tree',
134130
ranksep: conf.edgeLengthFactor
135131
// isMultiGraph: false
@@ -159,7 +155,6 @@ const renderDoc = (doc, diagram, parentId) => {
159155

160156
const keys = Object.keys(states);
161157

162-
total = keys.length;
163158
let first = true;
164159

165160
for (let i = 0; i < keys.length; i++) {

src/mermaidAPI.js

-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ const config = {
317317
forkWidth: 70,
318318
forkHeight: 7,
319319
// Used
320-
padding: 5,
321320
miniPadding: 2,
322321
// Font size factor, this is used to guess the width of the edges labels before rendering by dagre
323322
// layout. This might need updating if/when switching font

0 commit comments

Comments
 (0)